Alternatives to BackgroundWorker for Windows Forms

Can I do something similar in a Windows Forms application?

I am trying to find other ways to update the user interface, rather than using BackgroundWorker all the time. Maybe something like this ?:

public List<String> results = new List<String>();

private void button1_Click(object sender, EventArgs e)
{
    When(SomeLongRunningMethod("get") == true)
    {
        // LongRunningMethod has completed.
        // display results on Form.
        foreach(string result in results)
        {
            this.Controls.Add(new Label() { Text = result; Location = new Point(5, (5 * DateTime.Now.Millisecond)); });
        }
    }
}

public void SomeLongRunningMethod(string value)
{
    if(value == "get")
    {
        // Do work.
        results.Add("blah");
    }
}

It basically says, "Do it, and when you're done, add the results to the form."

+4
source share
3 answers

I recommend using async/ awaitwith Task.Run. Note that returning results is cleaner:

private void button1_Click(object sender, EventArgs e)
{
  var results = await Task.Run(() => SomeLongRunningMethod("get"));
  foreach(string result in results)
  {
    this.Controls.Add(new Label() { Text = result; Location = new Point(5, (5 * DateTime.Now.Millisecond)); });
  }
}

public List<string> SomeLongRunningMethod(string value)
{
  var results = new List<string>();
  if(value == "get")
  {
    // Do work.
    results.Add("blah");
  }
  return results;
}

I have a series of blog posts on my blog that describe how it Task.Runacts as a replacement forBackgroundWorker .

+6

, , async/await:

private async void button1_Click(object sender, EventArgs e)
{
    string result = await SomeLongRunningMethod("www.stackoverflow.com");

    // LongRunningMethod has completed.
    ....
}

public Task<string> SomeLongRunningMethod(string uri)
{
    // Example
    return WebClient.DownloadStringAsync(new Uri(uri));
}

:

public Task<string> SomeLongRunningMethod()
{
  return Task.Factory.StartNew(() => 
  {
     // Perform work which requires some time to complete ...
     return "your result";
  });   
}

. :

http://msdn.microsoft.com/en-us/library/hh191443.aspx

+1

, async/wait. , , .

public List<String> results = new List<String>();

private async void button1_Click(object sender, EventArgs e)
{
    var results = await SomeLongRunningMethodAsync("get");
    //Flow continues here once the long running operation has completed
    foreach(string result in results)
    {
      this.Controls.Add(new Label() { Text = result; Location = new Point(5, (5 * DateTime.Now.Millisecond)); });
    }
}

public async Task<List<string>> SomeLongRunningMethodAsync(string value)
{
    //This is the long running operation
    //Mind you, this executes on a separate thread. 
    return await Task.Run(() =>
    {
      List<string> results = new List<string>();
      if(value == "get") 
      {
        //do work
      }
      return results;
    });
}

.

, Async . , Task.Run Task.TaskFactory.StartNew - ( , ).

+1

All Articles