Form created on BackgroundWorker thread hangs in ShowDialog()

Hello all,

I have a form which is being created by a factory. The creation takes place in the DoWork() method of a background worker thread. I pass the created form from the DoWork() method to the RunWorkerCompleted() method using the EventArgs.Result property (I've also tried making the form a class variable and initializing it in the DoWork() method and using it in the RunWorkerCompletedEventArgs() method). The form seems to create OK but when I try to call its ShowDialog() method the execution simply hangs. I've tried to reproduce with smaller empty forms but cannot which makes me think its something to do with the form I'm creating. I could create the form outside of the BackgroundWorker with no problems so this must be related to the BackgroundWorker class.

Has anyone ever seen this before or know what could cause this



Answer this question

Form created on BackgroundWorker thread hangs in ShowDialog()

  • AshishGupta

    Hi,

    are you using .Net 1.1 What do you mean by hang, does the form not render correctly then your app is unuseable The problem you probably are having is that a control can only be accessed by the thread that created it. In .Net 2.0 you would get an exception if you tried but in 1.1 it is just pot luck whether it will work or not. In order to update a control you need to be on the thread that created the control, you can do this by using the Control.Invoke method. However I would suggest refactoring your code to create all controls on your main UI thread.

    Mark.



  • svenderuyter

    Hi Mike

    Thanks for the response. I'm sorry, I believe I should have been a little bit more clear. What's actually happening is that once I've made the call to ShowDialog() the execution halts but the dialog never actually appears. I actually have the same behavior with Show() as well, essentially anything that launches this dialog seem to cause it in this case. The weird thing is that I'm actually using this same component in several other locations in the same manner and it behaves as normal.

    Since I posted earlier I've actually rearranged the code a bit so that only my most intensive operation is handled on the background thread but and everything else is not. Essentially I had an arrangement like before:

    Start Parent Form

    Start BackgroundWorker and ProgressBar

    Start Group of Intensive Operations

    Start Slightly Intensive Operation

    Start More Intensive Operation

    Start Most Intensive Operation

    End Group of Intensive Operations

    Close BackgroundWorker and Progress Bar

    Show resulting Dialog (this is where the problem is)

    Now my arrangment is more similar to this

    Start Parent Form

    Start Group of Intensive Operations

    Start Slightly Intensive Operation

    Start More Intensive Operation

    Start BackgroundWorker and ProgressBar

    Start Most Intensive Operation

    Close BackgroundWorker and Progress Bar

    End Group of Intensive Operations

    Show resulting Dialog

    Although this doesn't quite have the desired effect (this is a noticeable period of inactivity on either end of the most intensive operation when the progress bar isn't up, it doesn't cause the same threading issue that I was seeing before. So i''m planning on working with this in place until I can come up with some a little better.

    Thanks again for your response, you're help was greatly appreciated!

    Jeremy



  • ehsan sadeghi

    Hi Mark

    That's a great diagnosis. I've actually been dealing the CrossThread errors a lot since I've started incorporating this work. I suppose that should have been an early hint.

    I've managed to realign the code to get it safely functional for an upcoming VC demo using the Control.Invoke pattern but immediately afterwards I'm planning on coming and refactoring all of this code. This was 'inherited' code which was never really intended to be broken up so it could be multithreaded which seems to be where most of my headaches are coming from.

    Thanks for everyones great responses, you've definitely given me a lot of great ideas for the upcoming refactor-fest!

    Thanks!
    Jeremy


  • Vaish

    The difference between "ShowDialog" and "Show" is that show dialog haults the thread until the window is closed. That is why you are getting your issue. Just use the show method and it should all be fixed

    Take Care
    Mike


  • KeithFranklin

    You should avoid creating forms or controls in a BackgroundWorker object's DoWork() method. That method is run on a background thread and cannot communicate with other forms/controls created in the main thread.

  • Form created on BackgroundWorker thread hangs in ShowDialog()