-2

I have a problem with my winform app in C#. In program, i have a label as statusbar and a method like this:

private void btnProcess_Click(object sender, EventArgs e)
{
    lblStatus.Text = "Please wait...";
    /*
    Code here
    */
}

my code process taking a few second but after processing the code, text of label will change and i want it happening before that.

Note: I am an amateur in programming and i cant understand english very well, please explain your solution simply. thank you.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
Diego
  • 11

4 Answers4

3

You may be able to hack this using a repaint event by calling Update or Refresh, but the real solution is using a BackgroundWorker for longer operations.

This is not easy stuff, you should find a tutorial for it in a language you understand perfectly.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
0

In winform application (but also in all GUI application) it's not recommended execute long task in the GUI thread. For solving your problem you must use Thread(here you can find the complete mdsn reference).
If you want update the Label from another Thread you' ll do a Cross threading operation. For more information take a look to this question

Community
  • 1
  • 1
Tinwor
  • 7,765
  • 6
  • 35
  • 56
0

All the code is always ran and completed before any changes are made to the UI. This is just the basic logic of WinForms and WPF.

You can use "BackgroundWorker", so the longer code is run on another thread. This way you can freely edit the UI-elements while the longer code is still running.

W0lfw00ds
  • 2,018
  • 14
  • 23
0

If you are using .Net 4.5, you can use await and async rather than BackgroundWorker.

You would create an async method that returns a Task, and inside that method use Task.Run() to start the background work.

Here's an example. You'd do all the slow work in the lambda passed to Task.Run() where I've commented "Do all your slow work here":

private async void btnProcess_Click(object sender, EventArgs e)
{
    lblStatus.Text = "Please wait...";

    await doSomeWorkAsynchronously();

    lblStatus.Text = "Work completed";
}

private async Task doSomeWorkAsynchronously()
{
    await Task.Run(()=>
    {
        // Do all your slow work here.
        Thread.Sleep(5000); // Simulate slow work.
    });
}

I think this is a bit easier than using BackgroundWorker.

Note that if all your "slow" methods were already async (e.g. if you are using asynchronous file I/O) then you might just be able to await each of your async operations rather than having to create your own task via Task.Run().

You can also return a value from the background task. Suppose you wanted to return a string with which to update the label when the background task had completed. You could do that as follows:

private async void btnProcess_Click(object sender, EventArgs e)
{
    lblStatus.Text = "Please wait...";
    lblStatus.Text = await doSomeWorkAsynchronously();
}

private async Task<string> doSomeWorkAsynchronously()
{
    return await Task.Run(()=>
    {
        // Do all your slow work here.
        Thread.Sleep(5000); // Simulate slow work.
        return "The task completed.";
    });
}
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276