Click here to Skip to main content
15,887,945 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI! We are developing a rather complex multi-threaded application WinForms C#.
To synchronize the execution, we use the synchronization context object. It works as expected, but for one specific case.
The delegate scheduled to be executed by the Post() method, called at some point, is not executed until "something" forces it to execute.

More precisely, just for testing, I added a button to the main form, and inside the button's Click method, I run the redundant Post() method in the same synchronization context.
As a result, all previously scheduled delegates are executed, which means that they were stored somewhere and were not lost.

Can someone explain why the delegates are hanging and what needs to be done to make them work without extra effort?

The following is a description of the detailed scenario.
First, the application creates the main window. In its Onload(), we save the current synchronization context (SC) and call the Post() function for testing. This works, and the corresponding delegate runs "immediately", as expected.
A child window is created in the main thread (inside the main OnLoad window) (more precisely, this is the docking window. We use the WeifenLuo dock library). This child window stores the main window's synchronization context (SC).
Another working thread simultaneously creates an object, let's call it publisher (P).
Immediately after the child window creating (SC is already saved at the moment), it subscribes to the publisher (P) event. If the event occurs, in its handler we call Post() for the saved context of the main window (SC).
In this Post() we tried to schedule the execution of the child window method.

In half the cases this works. in the other half, nothing happens. If nothing happens, I click the redundant button in the main window and thereby call another redundant Post() function with an "empty" delegate.
As a result, all previously scheduled delegates are executed, and finally, the last “empty” one.

Initially, I thought that the Synchronization Context (SC) was not yet initialized in any way. But, apparently, this is not so, since the very first Post() works.
The second thought was that something was not initialized in the object referenced by the delegate. But it is also strange because its constructor is complete.

I would be very pleased if someone could clarify the situation and give me a suggestion on how to fix this.

What I have tried:

Need help in resolving an issue with SynchronizationContext
Posted
Comments
[no name] 19-Jun-20 19:05pm    
Try it with synchronous first. Unjustified complexity? Maybe you have to "do events" after each post. The sync context is lazy, says the docs.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900