locked
Two asynchronous threads with async and await RRS feed

  • Question

  • Hi !!

    Using Async CTP and Await

    I need two threads running in parallel when I run the other one is independent so that I can do what I want on the screen:

    ex: I have an asynchronous method that has a Thread.Sleep 10 seconds for example, and also have a small form where I can update in quanquelr point during those ten seconds.

    In short I have a screen with two buttons, one performs an asynchronous method of sleep while the other can update the form as many times as you want to sleep after the end a message to the user who finished speaking.

    Someone help me please?






    • Edited by gandalfne0 Thursday, February 16, 2012 5:24 PM
    Thursday, February 16, 2012 5:04 PM

Answers

  • I don't think you need two threads. You can start two asynchronous operations and have them both running at the same time.

    When exploring Async, you don't ever want to use Thread.Sleep. Use Task(Ex).Delay instead. Also, it's a good idea to write "async Task" methods when possible, and only use "async void" for event handlers.

    Try this code:

    private async Task sleep()
    {
      await TaskEx.Delay(5000);
    }
    
    private async Task ExecutarAssincronos()
    {
      txtAssincrono.Text = "teste";
      await sleep();
      await PassoTresAssincrono();
    }
    
    private async Task PassoTresAssincrono()
    {
      await TaskEx.Delay(1000);
      lblResult.Text = "Assíncrono";
    }
    
    protected async void btnIniciar_Click(object sender, EventArgs e)
    {
      await ExecutarAssincronos();
    }

          -Steve


    Programming blog: http://nitoprograms.blogspot.com/
      Including my TCP/IP .NET Sockets FAQ
      and How to Implement IDisposable and Finalizers: 3 Easy Rules
    Microsoft Certified Professional Developer

    How to get to Heaven according to the Bible

    Wednesday, February 22, 2012 1:38 PM

All replies

  • Code:
    private void sleep()
            {
                Thread.Sleep(5000);
            }
            private async void ExecutarAssincronos()
            {
                txtAssincrono.Text = "teste";
                await TaskEx.Run(() => sleep());
                await TaskEx.Run(() => PassoTresAssincrono());
            }
            private void ExecutarSincronos()
            {
                txtSincrono.Text = txtAssincrono.Text;
            }
            private void PassoTresAssincrono()
            {
                Thread.Sleep(1000);
                lblResult.Text = "Assíncrono";
            }
    
            protected void btnIniciar_Click(object sender, EventArgs e)
            {
                ExecutarAssincronos();
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                ExecutarSincronos();
            }

    Thursday, February 16, 2012 6:51 PM
  • someone? :(
    Friday, February 17, 2012 10:46 AM
  • I don't think you need two threads. You can start two asynchronous operations and have them both running at the same time.

    When exploring Async, you don't ever want to use Thread.Sleep. Use Task(Ex).Delay instead. Also, it's a good idea to write "async Task" methods when possible, and only use "async void" for event handlers.

    Try this code:

    private async Task sleep()
    {
      await TaskEx.Delay(5000);
    }
    
    private async Task ExecutarAssincronos()
    {
      txtAssincrono.Text = "teste";
      await sleep();
      await PassoTresAssincrono();
    }
    
    private async Task PassoTresAssincrono()
    {
      await TaskEx.Delay(1000);
      lblResult.Text = "Assíncrono";
    }
    
    protected async void btnIniciar_Click(object sender, EventArgs e)
    {
      await ExecutarAssincronos();
    }

          -Steve


    Programming blog: http://nitoprograms.blogspot.com/
      Including my TCP/IP .NET Sockets FAQ
      and How to Implement IDisposable and Finalizers: 3 Easy Rules
    Microsoft Certified Professional Developer

    How to get to Heaven according to the Bible

    Wednesday, February 22, 2012 1:38 PM
  • still did not workwhenI clickthe buttoniniciarAssincronahe makes hiscasemore if Iclick oniniciarSsincrona before ithas finished runningaboveit is interruptedtothe implementation of thesecond click,I needthemdiquemindependent of each other

    private async Task sleep()
            {
                await TaskEx.Delay(5000);
            }
    
            private async Task ExecutarAssincronos()
            {
                txtAssincrono.Text = "teste";
                await sleep();
                await PassoTresAssincrono();
                
            }
    
            private async Task PassoTresAssincrono()
            {
                await TaskEx.Delay(1000);
                lblResult.Text = "Assíncrono";
            }
    
            protected async void btnIniciarAssincrono_Click(object sender, EventArgs e)
            {
                await ExecutarAssincronos();
            }

    Help me plis?
    Monday, February 27, 2012 12:36 PM
  • 1) What do you expect to see?

    2) What are you seeing?

          -Steve


    Programming blog: http://nitoprograms.blogspot.com/
      Including my TCP/IP .NET Sockets FAQ
      and How to Implement IDisposable and Finalizers: 3 Easy Rules
    Microsoft Certified Professional Developer

    How to get to Heaven according to the Bible

    Monday, February 27, 2012 1:36 PM
  • 1- after the click of a button IniciarAssincrona sleep will turn and me an while I click the button to fill my IniciarSincrona label with the string "Síncrono"

    2- Currently IniciarAssincrona I click the button and then click the button IniciarSincrona the first execution is halted for the second run, they need to be independent.

    Code:

    protected void btnIniciarSincrono_Click(object sender, EventArgs e)
            {
                ExecutarSincronos();
            }
    
            private void ExecutarSincronos()
            {
                txtSincrono.Text = "Síncrono";
            }
    
            private async Task sleep()
            {
                await TaskEx.Delay(5000);
            }
    
            private async Task ExecutarAssincronos()
            {
                txtAssincrono.Text = "teste";
                await sleep();
                await PassoTresAssincrono();
                
            }
    
            private async Task PassoTresAssincrono()
            {
                await TaskEx.Delay(1000);
                lblResult.Text = "Assíncrono";
            }
    
            protected async void btnIniciarAssincrono_Click(object sender, EventArgs e)
            {
                await ExecutarAssincronos();
            }


    • Edited by gandalfne0 Monday, February 27, 2012 1:51 PM
    Monday, February 27, 2012 1:50 PM
  • Cheers mate, this really helped me out. Was scratching my head for a while when thread sleep caused my app to simply freeze. 
    Monday, December 24, 2012 3:36 PM