0

I made a short program which has just a button. When the button is pressed, functionA is executed, which also uses functionB and functionC. Inside functionA is a loop which executes functionB and functionC X amount of times. At the end of each loop, the progressbar gets incremented by 1. At the beginning of functionA, before the loop, there's a webservice which pulls data from a website, and passes that onto B and C for processing (data file manipulation and saving to disk).

My problem is that everything works fine, but while functionA is still running, the GUI is stuck, so I can't close/minimize/drag the window around, I have to wait until A is done. I researched and they say I should use BackgroundWorker, but as being a new programmer, I've no idea on how to use it. Can someone give me a simple way to use it?

The progressbar loads fine, but it's just that while the function is running, the whole window is frozen, and I want it so I can move the window around, etc while the program is running, instead of waiting until the function is complete.

Thank you!

User13888
  • 77
  • 1
  • 5
  • Start by posting the code you have already. Describing it is not really what's expected here. – Nasreddine Jul 22 '15 at 18:55
  • possible duplicate of [How to update the GUI from another thread in C#?](http://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c) – Palu Macil Jul 22 '15 at 18:58
  • Go read [BackgroundWorker](https://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx) or [How to: Use a Background Worker](https://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx)? – Idle_Mind Jul 22 '15 at 19:01
  • There are plenty of tutorials out there. This question format is not appropriate for SO. SO is appropriate for more specific questions with code segments. Try this: http://www.dotnetperls.com/backgroundworker – Frank V Jul 22 '15 at 19:02
  • You may use the [SxProgress class](https://stackoverflow.com/questions/30829395/an-easy-to-use-progress-bar-for-winforms). You must just isolate the code of function A loop in a procedure. If you want, you may minimize your form before calling SxProgress, then the progress pop-up form will be the only visible part of your app. – Graffito Jul 22 '15 at 19:03

2 Answers2

1

Call your function asynchronously like the following and it will not freeze the UI.

private async void BeginProcessingAsync(Data d)
{
    //Execute the long running task asynchronously
    await Task.Run(() =>  functionA(d));

    //Anything after the await line will be executed only after the task is finished.
    anotherFunction(d); // if you have one..
}

To run your task, simply call BeginProcessingAsync(d);. Also, please note: If you're using newer versions of .NET, you might have to use await Task.Factory.StartNew(() => functionA(d)); instead of the above

sparta93
  • 3,684
  • 5
  • 32
  • 63
0

Overall, you'll want to make sure your GUI doesn't get updated from another thread. Instead, the messages should go to a threadsafe location. For instance, you could have the thread building into something like a database and have the GUI using a timer to look for updated data flags.

There is a question with a lot more detail using delegates here.

Marc's answer was the simplest and best, in my opinion:

///...blah blah updating files
string newText = "abc"; // running on worker thread
this.Invoke((MethodInvoker)delegate {
    someLabel.Text = newText; // runs on UI thread
});
///...blah blah more updating files

From Dotnet Perls:

A Background Worker makes threads easy to implement in Windows Forms. Intensive tasks need to be done on another thread so the UI does not freeze. It is necessary to post messages and update the user interface when the task is done.

Also, from MSDN, look at Task-based Asynchronous Pattern (TAP) if you're using C# 5.

The Task-based Asynchronous Pattern (TAP) is based on the System.Threading.Tasks.Task and System.Threading.Tasks.Task types in the System.Threading.Tasks namespace, which are used to represent arbitrary asynchronous operations. TAP is the recommended asynchronous design pattern for new development.

Community
  • 1
  • 1
Palu Macil
  • 1,708
  • 1
  • 24
  • 34