Can't change text of EditText in backgroundworker ProgressChanged event

hoaohoao Member ✭✭

Hello everybody,
here is my code

private void bk_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    string target = e.UserState.ToString().Replace("</order>", "").Replace("<order>", "").ToUpper();            
    Toast msg = Toast.MakeText(this, target, ToastLength.Short);
    msg.Show();    
    if (target != "OK")
    {
        RunOnUiThread(() => {            
            txtIP.Text = target;
        });

    }
}

And my question is ,my backgroundworker works fine,
and when i call progressReport, it do Toast and success show on my android phone,
but it can't assign text to my EditText , i use debug tool and watch if the command goes wrong,
but it be excuted ok, and in ProgressChanged method , the EditText txtIP.Text changed,
when the method finish, the EditText txtIP.Text become the original value.

Am I missing something ?

Thanks

Best Answer

  • hoaohoao ✭✭
    Accepted Answer

    OK, finally i found the solution,
    although ProgressChanged event of backgroundworker is on UI Thread,
    but i still can't assgin Edittext , And i try In WorkComplete event, it works.

    I found if i declare static variable in activity class,
    like [ static EditText txtIP;] , i can assign value to the control in ProgressChanged event of backgroundworker.

    that's all.

Answers

  • hoaohoao Member ✭✭

    i just found that,
    all button click event was run in the same thread , and in the same 'frame stack?',
    like thread:1 and frame stack: 0x1
    but doWork event of backgroundworker is like below,
    thread:3 and frame stack:0xF6
    And ProgressChanged event of backgroundworker is like below,
    thread:1 and frame stack: 0x57

    I don't know what [frame stack] is.
    Is it the key to my question?
    Am I missing something?

    Thanks.

  • hoaohoao Member ✭✭
    Accepted Answer

    OK, finally i found the solution,
    although ProgressChanged event of backgroundworker is on UI Thread,
    but i still can't assgin Edittext , And i try In WorkComplete event, it works.

    I found if i declare static variable in activity class,
    like [ static EditText txtIP;] , i can assign value to the control in ProgressChanged event of backgroundworker.

    that's all.

Sign In or Register to comment.