Results 1 to 5 of 5

Thread: Await with sync method.

  1. #1

    Thread Starter
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Await with sync method.

    Hello, I'm trying to sort the things out with the new multithreading model using the await and async keywords.
    Now I have a project where I have a lengthly method that I'd preferred to call asyncronously:

    Code:
    private void StoreData(List<string> data)
    {
       ...
    }
    Now, I want this method to be called both syncronously and asyncronously depending on certain logic.
    I've made another method
    Code:
    private async void StoreDataAsync(List<string> data)
    {
       await ... // ???
    }
    Now, what should I put into the StoreDataAsync to call the StoreData logic asyncronously?
    I certainly can make it work using the old multithreading model, but is it possible to make it work the way I described?

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,686

    Re: Await with sync method.

    Hello,

    Here is a good reference which may help.

    Async/Await FAQ

  3. #3
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: Await with sync method.

    I'm guessing you haven't quite grasped how async methods work so I'll provide a small example:-
    C Code:
    1. //
    2.         private async void button1_Click(object sender, EventArgs e)
    3.         {
    4.             int i=0;
    5.  
    6.             while(true)
    7.             {
    8.                 i++;
    9.                 listBox1.Items.Add(i);
    10.  
    11.                
    12.                 await Task.Delay(1);
    13.             }
    14.  
    15.  
    16.  
    17.         }

    That code for a Button's Click event. It adds items to a ListBox asynchronously. The await keyword yields to the calling method so it doesn't lock up the UI. Also for await to work, the method in which its being used must have the async modifier in its declaration.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  4. #4

    Thread Starter
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Await with sync method.

    That's true, I haven't quite grasped this new feature.
    Basically, what I need is to call my sync method asyncronously.
    I have a sync method Foo(object param), what I need is to write an async wrapper around it in order to be able to call it asyncronously (without changing the Foo body. Is this possible with async/await or it's easier to use it the old way?

  5. #5
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: Await with sync method.

    Well to be honest, I can't say which is better. If this method Foo returns a value then you can handle getting this return value much more elegantly with the new way but it will require you to alter the method itself, at least to yield via Task.Delay if there's a loop in there somewhere. If it doesn't return a value then it might just be easier to thread it the old way as it wouldn't require you to change your method. You see this new way of writing asynchronous methods takes away all the ugliness of using callbacks as a way of getting notification when an asynchronous task is finished. Essentially you're writing asynchronous code in a synchronous style.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width