Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I implemented a couple generic binding collections that will probably be used later as datasources in a WinForm application.

My problem is that those collections may not be accessed by the same thread as the one of the control they are bounded to, this would lead to cross-thread exceptions or maybe other issues.
I am trying to marshal this thread crossing operations, considering that I would rather not have any reference to the control outside the UI thread, so I will not use neither the Control.Invoke method nor the BeginInvoke one.

I saw that with WPF it is possible to invoke actions thanks to a dispatcher, but it should not be used with WinForms so it is not really adapted to my situation.
So right now I am looking for a solution to marshal UI thread and background ones updating the datasources of controls without reference to the control itself.
Posted
Updated 12-Jul-15 20:35pm
v3

1 solution

What you say does not make any sense. Both Control's and Dispatcher's methods Invoke and BeginInvoke invoke delegate instance on the UI thread, so, these methods do the same. Also, Dispatcher can well be used on Forms; you can easily check it up.

These method won't work if you want to delegate instances from one thread to another thread which is not a UI thread. However, it's easy to create a similar mechanism by yourself, but only for threads for which the similar mechanism is usable; that is, such thread should have a loop handling all delegate instanced submitted for invocation. I explained how it works in detail in my article Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

Note that you don't have to use my queue class, as you can use System.Collections.Concurrent.BlockingCollection<>. But with my article complete with usage samples, you can see how to use it for implementation of the invocation of the delegate instance on a different thread.

See also my past answers:
Control.Invoke() vs. Control.BeginInvoke(),
Problem with Treeview Scanner And MD5,
.NET event on main thread,
How to get a keydown event to operate on a different thread in vb.net,
Control events not firing after enable disable + multithreading.

—SA
 
Share this answer
 

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