Sync wpf dispatcher.invoke from multiple background threads

Do I need to synchronize calls with Dispatcher.Invoke when accessing multiple background threads?

I know Dispatcher.BeginInvoke will automatically synchronize calls for the silverlight application ( http://msdn.microsoft.com/en-us/library/z8chs7ft(v=vs.95).aspx )

This is also true for the wpf application or should I uncomment the lock statement below.

public MainWindow() { InitializeComponent(); Dispatcher.BeginInvoke(new Action(() => { const int MAX_NR_THREADS = 1000; for (int i = 0; i < MAX_NR_THREADS; ++i) { int j = i+1; new Thread(()=>ThreadMethod(j)).Start(); } })); } private void ThreadMethod(int threadId) { const int ONE_MILLION = 1000000; for (int i = 0; i < ONE_MILLION/threadId; ++i) { //lock(lockObj) //{ this.Dispatcher.Invoke(new Action(() => { int sum = 0; for (int j = 0; j < ONE_MILLION/10000; ++j) { sum += j; } int a = sum; concurrentQueue.Enqueue(threadId*ONE_MILLION); })); //} } } 
+4
source share
1 answer

There is no need to synchronize these calls. Dispatcher.Invoke effectively acts as a queue for your calls already and does not require synchronization.

+6
source

All Articles