473,587 Members | 2,263 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# using BackgroundWorke r 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 _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 :

*************** *************
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 13745
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 backgroundWorke rFetchData 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(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 ".
Apr 5 '10 #5

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

Similar topics

3
6164
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 search directories recursviely andcall ReportProgress once per directory or in batches of 10 matching files if there are a lot of results. I pass...
3
12101
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 raise an exception and display a messagebox. however no matter where i put the Try block to trap the error, it does not work.
0
2684
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 RunWorkerCompleted procedures. My simplified procedures are shown below. They endlessly write messages to a text file. At some point it never moves from DoWork...
5
14119
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 my FileIO class) I've established Public Event in my core logic classes along with RaiseEvents (that will updated a progress bar on the UI side). ...
5
5546
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, problem is instead of returning: 192.168.0.1 online
9
18011
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 will almost always lead to the user opening a certain child window that's fairly resource intensive to load. Is it possible to load this form in a...
0
1069
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 up program on closing) Anyway, I've been using the example on msdn for using backgroundworker to do async stuff. The problem is IT STILL claims to...
2
11839
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 BackgroundWorker object, which seems to be on a MTA thread. Is there a way for me to get the BackgroundWorker thread to run in STA mode, or do I have to remove...
4
4794
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 error in Spanish (I don't get translation: Los controles creados en un subproceso no pueden tener controles primarios en un control en un subproceso...
0
7915
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7843
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7967
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6619
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5712
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3840
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.