26

i was doing some processor heavy task and every time i start executing that command my winform freezes than i cant even move it around until the task is completed. i used the same procedure from microsoft but nothing seem to be changed.

my working environment is visual studio 2012 with .net 4.5

private async void button2_Click(object sender, EventArgs e)
{
    Task<string> task = OCRengine();          
    rtTextArea.Text = await task;
}

private async Task<string> OCRengine()
{
    using (TesseractEngine tess = new TesseractEngine(
           "tessdata", "dic", EngineMode.TesseractOnly))
    {
        Page p = tess.Process(Pix.LoadFromFile(files[0]));
        return p.GetText();
    }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Serak Shiferaw
  • 993
  • 2
  • 11
  • 32
  • 1
    Looks like you are not yet familiar with what await does. I suggest you read some basic introductions. Await does not schedule threads, for example. – usr Feb 19 '13 at 16:56

3 Answers3

54

Yes, you're still doing all the work on the UI thread. Using async isn't going to automatically offload the work onto different threads. You could do this though:

private async void button2_Click(object sender, EventArgs e)
{
    string file = files[0];
    Task<string> task = Task.Run(() => ProcessFile(file));       
    rtTextArea.Text = await task;
}

private string ProcessFile(string file)
{
    using (TesseractEngine tess = new TesseractEngine("tessdata", "dic", 
                                                      EngineMode.TesseractOnly))
    {
        Page p = tess.Process(Pix.LoadFromFile(file));
        return p.GetText();
    }
}

The use of Task.Run will mean that ProcessFile (the heavy piece of work) is executed on a different thread.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 4
    +1. One more link with detailed explanation of the similar sample - http://visualstudiomagazine.com/articles/2012/08/01/performance-tips.aspx – Alexei Levenkov Feb 19 '13 at 17:02
  • shouldn't this line `Task task = Task.Run(() => ProcessFile(file));` be like this: `Task task = Task.Run(() => return ProcessFile(file));`? – Prokurors Nov 14 '15 at 19:25
  • 3
    @Prokurors: Nope. Expression-bodied lambda expressions don't have return statements. – Jon Skeet Nov 14 '15 at 19:43
  • Ok, thanks! But what about this way: `Task task = Task.Run(() => {return ProcessFile(file);});`? Compiler accepts this way too, but what is the difference between these two? – Prokurors Nov 14 '15 at 20:04
  • 2
    @Prokurors: Basically no difference in this case. They're both lambda expressions with the same result. You can't convert a statement-bodied lambda into an expression tree, that's all - irrelevant in this case. – Jon Skeet Nov 14 '15 at 20:29
2

You can also do this by starting your task in new thread. Just use Thread.Start or Thread. ParameterizedThreadStart

See these for your reference:

http://msdn.microsoft.com/en-us/library/system.threading.parameterizedthreadstart.aspx

Start thread with parameters

Community
  • 1
  • 1
David Smithers
  • 2,354
  • 1
  • 21
  • 13
0

You could use BackgroundWorker component.

Ahmed Hafiz
  • 67
  • 1
  • 9