473,473 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C# using BackgroundWorker and UI still freezes

3 New Member
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 _RunWorkerCompleted 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 :

****************************
Expand|Select|Wrap|Line Numbers
  1. while (true)
  2.  {
  3.  
  4.        backgroundWorkerFetchData.RunWorkerAsync((String)one);
  5.  
  6.                 while (backgroundWorkerFetchData.IsBusy)
  7.                 {
  8.                    Application.DoEvents();
  9.                 }
  10.  
  11.                 backgroundWorkerComputeStats.RunWorkerAsync((int)10);
  12.  
  13.                 while (backgroundWorkerComputeStats.IsBusy)
  14.                 {
  15.                     Application.DoEvents();
  16.                 }
  17.  
  18.                 Thread.Sleep(4000);
  19.  
  20.   }
  21.  
********************

Thanks !
Apr 5 '10 #1
4 13725
Shashi Sadasivan
1,435 Recognized Expert Top Contributor
Expand|Select|Wrap|Line Numbers
  1. backgroundWorkerFetchData.RunWorkerAsync((String)o ne);
  2.  
  3. while (backgroundWorkerFetchData.IsBusy)
  4. {
  5. Application.DoEvents();
  6. }
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)
Apr 5 '10 #2
tlhintoq
3,525 Recognized Expert Specialist
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
Apr 5 '10 #3
fanjomtl
3 New Member
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 backgroundWorkerFetchData was already working when it came back to the beginning of the loop.


Expand|Select|Wrap|Line Numbers
  1.  
  2.   while (backgroundWorkerFetchData.IsBusy)
  3.  {
  4.  Application.DoEvents();
  5.  }
  6.  
  7.  

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 !
Apr 5 '10 #4
fanjomtl
3 New Member
Hello all,

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

As tlhintoq said, "Thread.sleep is evil. It sleeps everything on that thread, including UI ".
Apr 5 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Tim Anderson | last post by:
I've been experimenting with the BackgroundWorker class. As a test, I've built an application that searches for files matching a RegEx and populates a listbox with the results. My approach is to...
3
by: chris | last post by:
hello, I can't seem to make this work: VS2005 I have a simple program that uses a backgroundworker control to execute a long process (webservice call) if that webservice call fails, i want to...
0
by: genojoe | last post by:
I am running an application that, when not used, just sits there firing a BackgroundWorker every 20 seconds. Every now and then, the BackgroundWorker freezes between the DoWork and...
5
by: Rob R. Ainscough | last post by:
I'm using a BackgroundWorker to perform a file download from an ftp site. Per good code design practices where I separate my UI code from my core logic code (in this case my Download file method in...
5
by: Michael M. | last post by:
I have the following code (listed at bottom of post) that pings a small range of IP address to see which ones are alive. To speed things up a little I am trying to use more than one thread,...
9
by: RvGrah | last post by:
I'm completely new to using background threading, though I have downloaded and run through several samples and understood how they worked. My question is: I have an app whose primary form...
0
Plater
by: Plater | last post by:
So it occured to me that I told people to use this to solve some thread-safeing issues, but that I had not been using it myself (I had been using a delegate with invokerequired which seemed to lock...
2
by: =?Utf-8?B?SGFycnkgS2Vjaw==?= | last post by:
I have introduced a component to my solution that is throwing an exception about needing to be run in single threaded apartment mode. This component is created in an async call by a...
4
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post by:
Hi misters, I have winForm that needs update Panel (add controls to Panel of SplitContainer dynamically). I have a Backgroundworker, and DoWork event I want to add controls but I get this...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.