C# using BackgroundWorker and UI still freezes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fanjomtl
    New Member
    • Apr 2010
    • 3

    #1

    C# using BackgroundWorker and UI still freezes

    Hello guys,

    I have a problem of UI freeze with my application in C# which basically fetch datas, compute stats on these datas, wait for 4 seconds and do it again and again.

    Following http://msdn.microsoft.com/en-us/libr...undworker.aspx

    I used 2 background workers, 1 to fetch and the other one to compute stats. It then returns to the main thread which shows computed stats in a datagridview ( but I have the same problem with a textBox). In _RunWorkerCompl eted functions, I update the UI.

    The problem is that UI freezes during the fetch and computing, then un-freezes during the 4 seconds ( I can barely move the window but computed stats are shown) and it freezes again and again.. I was looking to solve this problem with background workers...

    What do I do wrong ? Can you help me ? Here is the important portion of code :

    *************** *************
    Code:
    while (true)
     {
    
           backgroundWorkerFetchData.RunWorkerAsync((String)one);
    
                    while (backgroundWorkerFetchData.IsBusy)
                    {
                       Application.DoEvents();
                    }
    
                    backgroundWorkerComputeStats.RunWorkerAsync((int)10);
                   
                    while (backgroundWorkerComputeStats.IsBusy)
                    {
                        Application.DoEvents();
                    }
    
                    Thread.Sleep(4000);
    
      }
    *************** *****

    Thanks !
    Last edited by Shashi Sadasivan; Apr 5 '10, 04:56 AM. Reason: Code goes in [CODE] tags
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    Code:
    backgroundWorkerFetchData.RunWorkerAsync((String)o ne);
    
    while (backgroundWorkerFetchData.IsBusy)
    {
    Application.DoEvents();
    }
    Why do you have a wile statement there ?
    That is most likely the cause of the UI freezing.
    You can use the background worker finished event to call the method DoEvents(cant remember from the top of my head what the finished event is called)

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      Thread.sleep is evil. Don't use it if you don't actually *know* you *need* it. It sleeps everything on that thread, including UI

      Comment

      • fanjomtl
        New Member
        • Apr 2010
        • 3

        #4
        Hi and thanks a lot for your quick answers !


        1 ) I wrote the 2 " while " statement in order to force the order : Fetch then Compute then Show.

        Before that, I had an exception telling me that backgroundWorke rFetchData was already working when it came back to the beginning of the loop.


        Code:
         
          while (backgroundWorkerFetchData.IsBusy)
         {
         Application.DoEvents();
         }

        Maybe there is a better way of doing this ?


        2) As for Thread.sleep , I actually want to slow down the main loop as I don't want to fetch datas every 1 second or less ( for now, fetch + compute takes less than 1 second).

        If it sleeps everything, where should I put it ?

        Thanks !

        Comment

        • fanjomtl
          New Member
          • Apr 2010
          • 3

          #5
          Hello all,

          I put the Thread,Sleep(40 00) in one my background worker and now the UI is working like a charm.

          As tlhintoq said, "Thread.sle ep is evil. It sleeps everything on that thread, including UI ".

          Comment

          Working...