Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Weird behaviour with Async method call

Weird behaviour with Async method call

Scheduled Pinned Locked Moved C#
debugginghelpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kensai
    wrote on last edited by
    #1

    I'm trying to learn async threading and I stumbled on the first piece of code. It works but with weird results. I'd be glad if you could point out what's wrong with it. Here's the code I'm trying:

    private static void LongProcess(int waitTime)
    {
    Trace.WriteLine("LongProcess started");
    Thread.Sleep(waitTime);
    Trace.WriteLine("LongProcess finished");
    }

    private delegate void LongProcessDelegate(int waitTime);

    private void Start()
    {
    LongProcessDelegate lp = LongProcess;
    IAsyncResult ar = lp.BeginInvoke(2000, null, null);
    while(!ar.Completed)
    {
    Trace.WriteLine("Not finished yet");
    Thread.Sleep(100);
    }
    lp.EndInvoke(ar);
    Trace.WriteLine("All completed");
    }

    The problem is with the results. This is what I expected:

    LongProcess started
    Not finished yet
    ..
    Not finished yet
    LongProcess finished
    All completed

    But here's the Trace log when I set it to 1000ms:

    LongProcess finished
    Not finished yet
    ..
    Not finished yet
    LongProcess finished
    All completed

    At 2000ms:

    Not finished yet
    LongProcess finished
    Not finished yet
    ..
    Not finished yet
    LongProcess finished
    All completed

    What's wrong with this? Am I understanding async calls wrong?

    G 1 Reply Last reply
    0
    • K kensai

      I'm trying to learn async threading and I stumbled on the first piece of code. It works but with weird results. I'd be glad if you could point out what's wrong with it. Here's the code I'm trying:

      private static void LongProcess(int waitTime)
      {
      Trace.WriteLine("LongProcess started");
      Thread.Sleep(waitTime);
      Trace.WriteLine("LongProcess finished");
      }

      private delegate void LongProcessDelegate(int waitTime);

      private void Start()
      {
      LongProcessDelegate lp = LongProcess;
      IAsyncResult ar = lp.BeginInvoke(2000, null, null);
      while(!ar.Completed)
      {
      Trace.WriteLine("Not finished yet");
      Thread.Sleep(100);
      }
      lp.EndInvoke(ar);
      Trace.WriteLine("All completed");
      }

      The problem is with the results. This is what I expected:

      LongProcess started
      Not finished yet
      ..
      Not finished yet
      LongProcess finished
      All completed

      But here's the Trace log when I set it to 1000ms:

      LongProcess finished
      Not finished yet
      ..
      Not finished yet
      LongProcess finished
      All completed

      At 2000ms:

      Not finished yet
      LongProcess finished
      Not finished yet
      ..
      Not finished yet
      LongProcess finished
      All completed

      What's wrong with this? Am I understanding async calls wrong?

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      kensai wrote:

      Am I understanding async calls wrong?

      Yes. Obviously you expect the code in the LongProcess to start immediately when you call the BeginInvoke method, but that doesn't happen. A new thread is started to run the method, and you can't predict exactly when that thread starts to run. Unless you have a multi-core processor, the thread running the LongProcess method will rarely start before you call Tread.Sleep in the main thread.

      Despite everything, the person most likely to be fooling you next is yourself.

      K 1 Reply Last reply
      0
      • G Guffa

        kensai wrote:

        Am I understanding async calls wrong?

        Yes. Obviously you expect the code in the LongProcess to start immediately when you call the BeginInvoke method, but that doesn't happen. A new thread is started to run the method, and you can't predict exactly when that thread starts to run. Unless you have a multi-core processor, the thread running the LongProcess method will rarely start before you call Tread.Sleep in the main thread.

        Despite everything, the person most likely to be fooling you next is yourself.

        K Offline
        K Offline
        kensai
        wrote on last edited by
        #3

        Alright, thanks for the info.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups