Inspired by my experience with multi-threaded Winforms applications, as well as issues such as
- Avoid Invoke / BeginInvoke issues in event handling with end-to-end WinForm threads?
- Avoid Invoke Calls When Placing a Control
I came up with a very simple template whose core I want to test.
Basically, I create (and execute all my life throughout the entire application) BGW, whose sole purpose is to synchronize call requests. Consider:
public MainForm() { InitializeComponent(); InitInvocationSyncWorker(); } private void InitInvocationSyncWorker() { InvocationSync_Worker.RunWorkerAsync(); } private void InvocationSync_Worker_DoWork(object sender, DoWorkEventArgs e) { Thread.Sleep(Timeout.Infinite); } void InvokeViaSyncWorker(Action guiAction) { InvocationSync_Worker.ReportProgress(0, guiAction); } private void InvocationSync_ProgressChanged(object sender, ProgressChangedEventArgs e) { if (IsDisposed) return;
Of course, this is not the most economical of the approaches (supporting the stream in the same way), but if it works and I did not miss anything, this is by far the easiest I have seen.
Feedback is greatly appreciated!
Ohad Schneider Nov 16 '10 at 0:56 2010-11-16 00:56
source share