heavy process control  
Author Message

PostPosted: Visual C# General, heavy process control Top


I have a heavy process on my application. and when application is working on it, nothing works in the application and it seems that the application is****. if the user try to do anything, for example to move the form, everything will disappear untill the process finish.

I want to inform user that the application is in progress. but progressbar can not work untill the heavy process is working (this process is from separate class and class doesn't have any access to progressbar object)

I think I should use threading to solve this problem but I am not sure that it is the best way to do.

Visual C#17  

PostPosted: Visual C# General, heavy process control Top

If you are using v2.0 then the BackgroundWorker component was designed for this process. You create an instance in your UI, associate a delegate to do the work and then tell it to run. You can configure the component to support both progress notification and cancellation support. In both cases your underlying algorithm must support this and it must periodically tell the BCW to raise the events to the UI layer. The UI can then update a progress bar or simply allow the user to continue working.

Here is the basic code for v2.0:

//In form handler to start process:
private BackgroundWorker bcw = new BackgroundWorker();

private void StartLongProcess ( ... )
bcw.WorkerSupportsCancellation = true; //Only if supported
bcw.WorkerReportsProgress = true; //Only if supported
bcw.DoWork = //Delegate to invoke
bcw.RunWorkerCompleted = ; //If you are interested in the results


//For progress notifications (will occur on the UI thread)
private void OnWorkerProgress ( object sender, ProgressChangedEventArgs e )
//Update progress bar

//To support cancellation (Invoked through button click or something)
private void CancelWorker ( object sender, EventArgs e )

//Called when the BCW completes
private void OnWorkCompleted ( object sender, RunWorkerCompletedEventArgs e )
//Process results

If you are using v1.x then you must create a separate thread (or use the thread pool). You are responsible for setting up your own progress notification events and, again, your algorithm must periodically raise the event. Since the event is raised in a non-UI thread (not the case for BCW) you'll have to marshal the call back to the UI thread. I'd recommend handling the details in the event handler on the UI side. In this case you need to use InvokeRequired to see if you are running on the UI thread. If you are not then you would marshal the call to the UI thread. Then you can update your progress indicator.

Here is the basic code for v1.x (note again that you have to create the event handler and arguments yourself, this is just an example):

//Implemented in a UI element like the main form
private void OnProgressChanged ( object sender, ProgressChangedEventArgs e )
if (this.InvokeRequired)
//Need to marshal to the UI thread
this.Invoke(new ProgressChangedEventHandler(OnProgressChanged), new object[] { sender, e });
} else
//Running on UI thread so do the work

Michael Taylor - 12/4/06


PostPosted: Visual C# General, heavy process control Top


thank you for your complete answer