c# - BackgroundWorker OnWorkCompleted throws cross-thread exception -
I have a simple user control for database paging, which uses a controller to make a real DAL call. I use a BackgroundWorker to do the big task, and on the OnWorkCompleted event I re-enable some buttons, change a textbox.text Raise an event for property and parent form.
Form A keeps my user strings. When I click on a button that opens Form B, even if I do not do "there" and just close it, and from my database Trying to bring the next page, I call onWorkCompleted on the worker thread (and not my main thread), and throws an cross-thread exception.
At the moment I have added a check to the Codex on the handlebars, but the main code of the main code is called OnWorkCompleted ? Why would it not work as expected?
Edit:
I have been able to reduce the problem and the problem BackgroundWorker I have the following solution, which is called a command for the archmap , Which opens a simple form1 with two buttons.
The first button runs a BackgroundWorker that flows to 500 mms and updates a counter RunWorkerCompleted in the form it InvokeRequired , and updates the title to show that the method was originally going to run inside the main thread or worker thread. The second button just opens Form2 , which has nothing in it.
First of all, all calls to RunWorkerCompletedare are built inside the main thread (as expected - the whold point of RunWorkerComplete method, thats the least that what I BackgroundWorker )
After opening and closing Form2 , by understanding on the code, containing the Runverker always being called on the worker thread I want to add that I can skip this problem with this problem ( InvokeRequired for RunWorkerCompleted method Check it out), but I should understand why my expectations are against it. In my "real" code, I would always like to know that the Runaworker Compulsive method is being called on the main code.
I got this problem to pin form.Show (); In my BackgroundTesterBtn - If I use ShowDialog () instead, I can not find any problem ( RunWorkerCompleted always on the main thread Moves). I need to use the Show in my ArcMap project, so that the user will not be bound to the form.
I also tried to reproduce the bug on the usual WinForms project. I added a simple project that opens the first form without arcaxes, but in that case I could not reproduce the bug - the main code was running run worker compliant , even if I Shown () or ShowDialog () before and after Form2 . I tried to add a third form to work as a main form before my Form1 , but its results did not change.
is my simple SLN (VS2005 SP1) - it's
ESRI.ArcGIS.ADF (9.2.4.1420)
ESRI.ArcGIS.ArcMapUI (9.2.3.1380 )
ESRI.ArcGIS.SystemUI (9.2.3.1380)
This is a bug I suggest using bullet-proof: (i)
Code> if (control.InvokeRequired) control.Invoke (action); Else Action ()
Comments
Post a Comment