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

All Courses

From novice to tech pro — start learning today.