thread requesting invalidate on ui thread c#

thready
thready used Ask the Experts™
on
hi experts,
I have a thread that is moving objects around in a canvas by changing their coordinates when events happen.  normally if I was doing all this on the ui thread, there's no problem to call invalidate whenever I want to redraw.  but of course this blows up from the thread if invalidate is called from it.

could someone show me a little snippet that would show how to use the synchronization context to call invalidate from my thread?  this worker was started with task.factory.startnew(MoveObjectsAround)

Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Hi  thready;

You will need to use the Dispatcher to access the controls of the UI thread. Please read this article on how to do this in your application.

How to Use Dispatcher in WPF to Build Responsive Applications

Author

Commented:
Thanks Fernando but I'm not using wpf. I'm using Xamarin, so I think my only option is vanilla synchronization context.
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Not stated in your question.
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Author

Commented:
Sorry about that, but I did ask for synchronization context at least :-)
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
You can do it by using Invoke() against the Form:
    public partial class Form1 : Form
    {

        private Random rnd = new Random();

        public Form1()
        {
            InitializeComponent();
            Task.Factory.StartNew(MoveObjectsAround);
        }

        private void MoveObjectsAround()
        {
            while (true)
            {
                // ... do some "work" ...
                Thread.Sleep(1000);

                // ... do some "calculations" ...

                this.Invoke((MethodInvoker)delegate {
                    // ... update the UI in here ...
                    label1.Location = new Point(rnd.Next(this.ClientSize.Width - label1.Width), rnd.Next(this.ClientSize.Height - label1.Height));
                });
            }
        }

    }

Open in new window


...or via SynchronizationContext, as requested:
    public partial class Form1 : Form
    {

        private SynchronizationContext sc;
        private SendOrPostCallback callback;

        private Random rnd = new Random();
        
        public Form1()
        {
            InitializeComponent();
            this.Shown += Form1_Shown;
        }

        void Form1_Shown(object sender, EventArgs e)
        {
            callback = new SendOrPostCallback(Update);
            sc = System.Windows.Forms.WindowsFormsSynchronizationContext.Current;

            Task.Factory.StartNew(MoveObjectsAround);
        }

        private void MoveObjectsAround()
        {
            int counter = 0;
               
            while (true)
            {
                // ... do some "work" ...
                Thread.Sleep(1000);

                // ... do some "calculations" ...
                counter++; 

                // update the UI:
                sc.Post(callback, counter.ToString("00000")); // you can pass what you want in the second parameter, just cast it back in the callback
            }
        }

        private void Update(object obj)
        {
            string msg = obj.ToString();
            label1.Text = msg;
            label1.Location = new Point(rnd.Next(this.ClientSize.Width - label1.Width), rnd.Next(this.ClientSize.Height - label1.Height));
        }

    }

Open in new window

Author

Commented:
Thank you!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial