-3

I have a method like this:

public void ButtonClicked()
{
     var MyResult=MyTimeConsumingTask(MyClassProperty); 
 }

As you can see, it blocks UI thread.

I can create a backgroundWorker and run this method on that background thread, but I am looking to see if using Async and Await would help me to simplify this.

This is not working:

public async void ButtonClicked()
{
    await var MyResult=MyTimeConsumingTask(MyClassProperty); 
}

How can I do this?

I like to know the general solution, but also noting that MyimeConsumingTask is waiting for some data on network, how can I solve the problem?

mans
  • 17,104
  • 45
  • 172
  • 321
  • 2
    `var MyResult = await Task.Run(() => MyTimeConsumingTask(MyClassProperty));` – EZI Jul 10 '14 at 15:17
  • 3
    "This is not working" is never enough information. I suspect you just want `var myResult = await MyTimeConsumingTask(MyClassProperty);` but you should *always* specify the error in the question. – Jon Skeet Jul 10 '14 at 15:19
  • possible duplicate of [How to call any method asynchronously in c#](http://stackoverflow.com/questions/5563299/how-to-call-any-method-asynchronously-in-c-sharp) – user247702 Jul 10 '14 at 15:19
  • What do you want to happen when the button is clicked? I assume that something should happen on the UI, when the `MyTimeConsumingTask` has finished? I'm also assuming that the UI should not block? – Jonny Jul 10 '14 at 15:21
  • @Jonny nothing, since it is not compilable... – EZI Jul 10 '14 at 15:22
  • @Stijn I don't believe that is a valid duplicate as this question explicitly asks (badly) about `async/await` which that question doesn't cover (as it was asked before the feature became available). – Daniel Kelley Jul 10 '14 at 15:23
  • @EZI Thanks for answer. What I was missing was the inclusion of Task.Run.. which is working now. Please add it as an answer and I will accept it. – mans Jul 10 '14 at 15:32
  • @DanielKelley It does answer the question with the [bottom answer](http://stackoverflow.com/a/24241619/247702). The answer doesn't use `await`, but that's not necessary here I believe. – user247702 Jul 10 '14 at 15:33
  • @Stijn That question is not similar as my question was how to use async and await, but the other question was how to use any method to run async. – mans Jul 10 '14 at 15:34
  • Just another question: In other question it say that async and await would be in c#5, but as far as I know it is available in c#4.5 am I wrong? – mans Jul 10 '14 at 15:36
  • @mans sure you asked for `async` and `await` because you thought that was the way to solve it. But really you're asking how to run your method asynchronous. – user247702 Jul 10 '14 at 15:36
  • @Stijn I also mentioned that I can use backgroundworker to do it, but my question is specifically how to do it using async and await – mans Jul 10 '14 at 15:38
  • @mans fair enough, I'll remove the close vote. Regarding your previous comment, C# 4.5 does not exist. The framework supports it in .NET 4.5 and C# supports the syntax with C# 5 ([see here](http://stackoverflow.com/questions/13179923/which-net-version-for-c-sharp-5-async-features)). – user247702 Jul 10 '14 at 15:38
  • Sounds like you simply need to go look at some basic tutorials on the topic. – Servy Jul 10 '14 at 19:43

1 Answers1

4

To be able to await MyTimeConsumingTask it must be declared to return a Task.

public async Task<SimeType> MyTimeConsumingTask()

Since you said it does some network IO, you can rewrite it using async NW IO methods and then await it as

var MyResult = await MyTimeConsumingTask(MyClassProperty);

But in your case the simplest approach seems to be using Task.Run

var MyResult = await Task.Run(() => MyTimeConsumingTask(MyClassProperty)); 

Comparing these two approaches:

1.

public async Task<string> GetHtmlAsync(string url)
{
    using (var wc = new Webclient())
    {
        var html = await wc.DownloadStringTaskAsync(url);
        //do some dummy work and return
        return html.Substring(1, 20);
    }
}

var str1 = await GetHtmlAsync("http://www.google.com");

2.

public string GetHtml(string url)
{
    using (var wc = new Webclient())
    {
        var html = wc.DownloadString(url);
        //do some dummy work and return
        return html.Substring(1, 20);
    }
}

var str2 = await Task.Run(()=>GetHtml("http://www.google.com"));

I would definitely prefer the first one, but the 2nd one is easier to use if it is already working and hard to change method.

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
EZI
  • 15,209
  • 2
  • 27
  • 33