Forum Cross Platform with Xamarin

Updating GUI from background Threads

Hello,

I'm hoping someone can help me with some sample code and forgive me if this is the wrong section, since i'm looking for help with both iOS and Android i figured it should go here. I'm use to using Delegate Events when working with C#. What i'm hoping is someone could hopefully provide a little code similar to this for android and iOS. I know all the platforms do not have cross thread GUI updates. So i'm trying to learn how Android and iOS do these sorts of updates.

Here is a example of what I would normally do inside of of a normal .Net application. Basicly what this does is you click the button on the form and it will launch a new thread firing off the function in the TestEvent class. Which in turns fires off events back to the form who then invokes a UI update.

Any help would be greatly appreciated.

public partial class Form1 : Form
{
    private TestEvent _testClass;

    public Form1()
    {
        InitializeComponent();

        _testClass = new TestEvent();
        _testClass.OnUpdateStatus += new StatusUpdateHandler(UpdateStatus);
    }

    private void UpdateStatus(object sender, ProgressEventArgs e)
    {
        if (this.InvokeRequired)
            this.Invoke(new MethodInvoker(delegate() { UpdateStatus(sender, e); }));
        else
            label1.Text = e.Status;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        new Thread(_testClass.Func).Start();
    }
}

delegate void StatusUpdateHandler(object sender, ProgressEventArgs e);

class TestEvent
{
    public event StatusUpdateHandler OnUpdateStatus;

    public void Func()
    {
        UpdateStatus("Sleeping");
        System.Threading.Thread.Sleep(5000);
        UpdateStatus("Hello World");
    }

    private void UpdateStatus(string status)
    {
        // Make sure someone is listening to event
        if (OnUpdateStatus == null) return;

        if (OnUpdateStatus != null)
        {
            ProgressEventArgs args = new ProgressEventArgs(status);
            OnUpdateStatus(this, args);
        }
    }
}

class ProgressEventArgs
{
    public string Status { get; private set; }

    public ProgressEventArgs(string status)
    {
        Status = status;
    }
}

Posts

  • HPAPHPAP USMember

    After a lot of research and playing with the code. I did get it to work with android.

    public class Activity1 : Activity
    {
        private TestEvent _testClass = new TestEvent();
        public Button myButton;
        public TextView myLabel;
    
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
    
            _testClass.OnUpdateStatus += new StatusUpdateHandler(UpdateStatus);
    
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
    
    
            // Get our button from the layout resource,
            // and attach an event to it
            myButton = FindViewById<Button>(Resource.Id.MyButton);
            myLabel = FindViewById<TextView>(Resource.Id.textView1);
    
            myButton.Click += delegate 
            {
                myLabel.Text = "Running Login";
    
                new Thread(_testClass.Func).Start();
            };
        }
    
        private void UpdateStatus(object sender, ProgressEventArgs e)
        {
            Action action = delegate { myLabel.Text = e.Status; };
            myLabel.Post(action);
        }
    }
    

    I'm still trying to figure out how to do it with iOS.

  • SKallSKall USMember ✭✭✭✭

    You should also consider whether async method makes more sense.

        public class TestEvent
        {
            public async Task Func(IProgress<string> progress)
            {
                progress.Report("Sleeping");
                await Task.Delay (5000);
                progress.Report("Hello World");
            }
        }
    
  • thowathowa DEMember ✭✭

    @rmacias said:
    On your can just call

    RunOnUiThread(() => mylabel.Text = "Updated from other thread");

    This works like a charm ;-)
    Thank you very much !!!

  • tanglaitanglai TWMember

    rmacias Thank you very much !!!

Sign In or Register to comment.