thread requesting invalidate on ui thread c#

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!
LVL 1
threadyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
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
0
threadyAuthor Commented:
Thanks Fernando but I'm not using wpf. I'm using Xamarin, so I think my only option is vanilla synchronization context.
0
Fernando SotoRetiredCommented:
Not stated in your question.
0
Cloud Class® Course: 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.

threadyAuthor Commented:
Sorry about that, but I did ask for synchronization context at least :-)
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
threadyAuthor Commented:
Thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.