CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Where did my background worker go?

  1. #1
    Join Date
    Apr 2011

    Where did my background worker go?

    He is not doing his job. I had some functions to run when my program is open and I noticed that the GUI was not coming up until the functions were completed.

    So I looked up an example of how to fix it by using a background worker. So I followed the code exactly and all syntax checks out and my program opens nice and fast. However the functions are never executed

    Here is the code:

    public MainWindow()

    BackgroundWorker OnLoad = new BackgroundWorker();

    OnLoad.DoWork += (s, e) =>






  2. #2
    Join Date
    Jan 2002
    Scaro, UK

    Re: Where did my background worker go?

    I'm guessing but I think your problem is due to the fact that you can't update UI controls from a thread other than the UI thread. This is a common problem.

    Have a look at Control.Invoke.

    However creating the update thread in your form's constructor may result in a race condition - you can only do the Control.Invoke when your form's window handle has been created and if you try to call Control.Invoke before the form's window has been created this will also throw an exception.

    You need to put your event handling code into a handler for the Load event of your form or alternatively override the OnLoad method of your form and put the thread creation in there.

    Then all should be fine.

    I'd suggest making the UI controls (list boxes, combo boxes etc) on your form disabled initially (i.e. set Enabled to false in the windows form designer) and then enable them from your thread.

    This will indicate to the user that the form is loading.

    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  3. #3
    Join Date
    Apr 2011

    Re: Where did my background worker go?

    Oh, I guess I need to mention I am not generating any elements on C#. The GUI is in a .xaml file and it is initialized in the InitializeComponent(); function. So the GUI is ready to go before I call updateListDatabases(); etc, however it remains hidden until those functions complete.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)