Psassing SynchronizationContext through User control on main form

I have a Form that contains a User Control.
The user control contains a class that starts a separate thread that in turn uses a TCP Client to listen for automatic updates.
Inside that thread I need to perform operations on the user control.

For the cross thread communication I want to use the SynchronizationContext Context from the main form so I can use a call like

Context.Post(new SendOrPostCallback(MyFunctionHere(XmlDocument updates), null);

How can I pass the Context through the User Control constructor to the Class and so into the update thread to perform my updates?
LVL 1
jetbetAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

sarabandeCommented:
you may do it simple. i assume the thread runs an infinite loop (which shouldn't block but work with timeouts such that the main thread could terminate the thread on user request). when the thread has received a complete xml-update, it should push that document into a queue which was provided by the control when the thread was started. for thread-safety the push should run with a lock on the queue (done by entering a critical section or a fast mutex lock). the control which runs in the context of the main form should check the queue periodically (for example by a timer). if the queue is not empty, the control would lock the queue and pop the document from the queue. now, it could safely update the main form within the context of the main thread.

Sara
0
jetbetAuthor Commented:
This is a good suggestion and I use synchronized queues in a less time critical update scenario.

This is part of a more complex scenario where GPS co-ordinates are captured by runners in real time (from an untrusted source) and the data collated (takes 600ms) and is then pushed via UDP to a server.
This server in turn processes and validates the data and then sends it via TCP to a secure zone where it is used to update runner positions on live TV.
If the delay is too great then the data becomes more or less useless.

The application that uses the data to update a graphics engine already has a fair degree of complexity and number of interacting threads and the idea is to keep the automatic update mechanism (only used occasionally) encapsulated in the new class I have added.
0
jetbetAuthor Commented:
I have tried just getting the Context on my User Control and passing that to the constructor of my class
public class Update_Live
    {
        
        private readonly System.Threading.SynchronizationContext context;
        public System.Threading.SynchronizationContext Context
        {
            get { return this.context; }
        }


        public Update_Live(SynchronizationContext _context)
        {
            this.context = _context;
        }

        private void UpdateRunnersOnGui()
        {
            this.Context.Post(new SendOrPostCallback(AutomaticUpdateRunnerNumbers), null);
        }

        public void AutomaticUpdateRunnerNumbers(object state)
        {
            
        }

Open in new window


The problem here is that I am unsure how to use the final call to find the User Control (runningNumbersProvisionalPlacings) and run the code specified there.
0
jetbetAuthor Commented:
In the end I have just passed a reference to the User control (stored as runnerNumbers) into the class so I can call the needed function.
private void UpdateRunnersOnGui()
        {
            this.Context.Post(new SendOrPostCallback(AutomaticUpdateRunnerNumbers), null);
        }

        public void AutomaticUpdateRunnerNumbers(object state)
        {
            runningNumbers.AutomaticUpdateRunnerNumbers(positionUpdates, currentRunners);

            int count = positionUpdates.Count;
            for (int i = 0; i < count; i++)
            {
                currentRunners[i] = positionUpdates[i];
            }
        }

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
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
Programming

From novice to tech pro — start learning today.