none
When to use "async"?When to use "Thread" RRS feed

  • Question

  • Just wanna ask several questions:

    Is "async method" (I mean since 4.5 version of net framework) the same as "Thread+back-caller"?

    1) If yes, a thread will execute the codes at code-behind, but the async method will be blocked (but not effect on UI badly) and it will be automatically called back when fetching values. How to explain this?

    2) If no, plz tell me when to use async methods? when to use Thread+back-caller?

    Thx anyway!

    Thursday, February 28, 2013 5:48 AM

Answers

  • Solved here:

    http://forums.asp.net/t/1887274.aspx/1?When+to+use+async+When+to+use+Thread+

    • Edited by ToughMan Tuesday, March 5, 2013 12:45 PM
    • Marked as answer by ToughMan Tuesday, March 5, 2013 12:45 PM
    Tuesday, March 5, 2013 6:59 AM

All replies

  • async and thread are equivalent methods.  I prefer using async because it is easier to to add communications between the two a classes (or main thread and an sync class) than to add communications between two threads (or a main application and a thread).

    Threads and async events both run off the timer tick services and when incoming data packets are received will arrive around the same time using either method.  I'm not sure but I suspect a thread will run at the same priority level as the application that started the thread while an Async Event will run at a higher priority.

    When an  application starts a connection it registers the port number(s) it is using with the ethernet driver.  Incoming packets are processed by the ethernet driver and forwarded using the port number to the application that registered for the port.  The timer tick services handles the scheduling and moving of the data between the ethernet driver and the application.  The Timer Tick Service doesn't distinguish between async method or a thread.  The moving of the data will be the same, but the scheduling may be slightly different.


    jdweng

    Thursday, February 28, 2013 10:43 AM
  • Hi again,

    So u mean Async is using Thread as a background?

     If yes, a thread will execute the codes at code-behind, but the async method will be blocked (but not effect on UI badly) and it will be automatically called back when fetching values. How to explain this?

    Friday, March 1, 2013 5:53 AM
  • Hi ToughMan,

    >>How to explain this?

    Which point is unclear? Would you like to point your concerns about it directly? Thank you.

    I try to retell it here: 

    Let's assume there is a UI thread, and it starts a new thread. Now, the two threads keep executing "parallel" (Not really parallel, I don't mean http://msdn.microsoft.com/en-us/library/dd460693.aspx).

    And then, UI thread is hard to morniter the new thread. If the UI thread need something back from the new thread, it has to wait for the new thread finish, then the user cannot do anything any more. Because the UI thread is blocked. 

    If use asyn, it can notice the UI thread, but without needing to block the UI thread, so the users can take other actions.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 1, 2013 9:44 AM
    Moderator
  • OK Mike,

    Thanks

    What I want to know is:

    1) What's the original codes and functions of async?

    2) Since Async fun doesn't effect UI main thread, but a background thread won't effect the Main thread, either. What's the MOST difference?

    How can I differ them from each other??

    Saturday, March 2, 2013 6:05 AM
  • Can u help me, Mike???
    Monday, March 4, 2013 11:53 AM
  • There isn't much difference between a thread and a async method except a thread will take slightly longer to notify the main thread when data is received (approximately 1 timer tick at most).  Using a thread the communication between the thread amd main function is performed using a pipe which creates the extra delay.

    jdweng

    Monday, March 4, 2013 12:40 PM
  • But the processing steps are quite different:

    1) Thread th = new Thread(new ThreadStart(SomFun());

        th.Start();

        MessageBox.Show("OK");

    2) If SomeFun is an async method:

        await SomFun();

         MessageBox.Show("OK");

    If SomeFun has a long task to do, for 1), Msgbox will pop up before SomFun ends, but for 2), msgbox won't pop-up until SomFun ends.

    So you aren't right, Joel. But thanks all the same.

    Tuesday, March 5, 2013 1:27 AM
  • But the processing steps are quite different:

    1) Thread th = new Thread(new ThreadStart(SomFun());

        th.Start();

        MessageBox.Show("OK");

    2) If SomeFun is an async method:

        await SomFun();

         MessageBox.Show("OK");

    If SomeFun has a long task to do, for 1), Msgbox will pop up before SomFun ends, but for 2), msgbox won't pop-up until SomFun ends.

    So you aren't right, Joel. But thanks all the same.

    Hi there,

    I agree with your test result. But I don't agree with your conclusion. Joel is right.

    The code 

    await SomFun();

    in your second sample can be translated to this:

    th.Jion();

    in the first sample.

    So you can try to remove the await keyword, and you will get the similar result.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, March 5, 2013 5:46 AM
    Moderator
  • Well……Mike

    If according to what you say, then——

    Why Microsoft invented this technology?

    As far as I see, Async does nothing to do with thread at all, it pushes the delayed time to the main thread as a pool of windows to delay the task and execute it sometime later.

    Any ideas???

    • Edited by ToughMan Tuesday, March 5, 2013 6:12 AM
    Tuesday, March 5, 2013 6:07 AM
  • Solved here:

    http://forums.asp.net/t/1887274.aspx/1?When+to+use+async+When+to+use+Thread+

    • Edited by ToughMan Tuesday, March 5, 2013 12:45 PM
    • Marked as answer by ToughMan Tuesday, March 5, 2013 12:45 PM
    Tuesday, March 5, 2013 6:59 AM