I have a method Update() that's called by a AsyncOperation through .Post(). When I place a MessageBox in that function, I get multiple messageboxes, and I'm trying to understand why that happens.
The Messagebox should halt execution of Update() untill 'OK' is clicked, but because of .Post() I get re-entrance in Update(), causing the next messagebox to appear before the previous one is closed.
My first thought was to place SyncLock in Update(), so that it never can run simultanously, but it had no effect. When I output the .ManagedThreadID in the MessageBox, all calls have the same thread ID, so it seems logical that SyncLock has no effect, because it only blocks different threads.
So can someone explain what's going on here? How can a single thread execute the same function simultanously?
A message box is only modal on the thread it is displayed on. The UI thread in your case, as it should. It cannot prevent the worker threads from calling Post(), you'll thus continue to get more message boxes. SyncLock cannot work either, it is re-entrant on the same thread. A simple boolean flag that you set to true before the MessageBox.Show() call and to false after will do the job, don't call Show again if it is set to true.
Or just not using MessageBox, Debug.Print() is a good way to trace. Text goes to the Output window by default. A debugger breakpoint can do wonders too.