Two asynchronous threads with async and await

Kilitli Two asynchronous threads with async and await

  • 16 Şubat 2012 Perşembe 17:04
     
     

    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?






    • Düzenleyen gandalfne0 16 Şubat 2012 Perşembe 17:24
    •  

Tüm Yanıtlar

  • 16 Şubat 2012 Perşembe 18:51
     
      Kod İçerir
    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();
            }

  • 17 Şubat 2012 Cuma 10:46
     
     
    someone? :(
  • 22 Şubat 2012 Çarşamba 13:38
     
     Yanıt Kod İçerir

    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

  • 27 Şubat 2012 Pazartesi 12:36
     
      Kod İçerir

    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?
  • 27 Şubat 2012 Pazartesi 13:36
     
     

    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

  • 27 Şubat 2012 Pazartesi 13:50
     
      Kod İçerir

    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();
            }


    • Düzenleyen gandalfne0 27 Şubat 2012 Pazartesi 13:51
    •  
  • 24 Aralık 2012 Pazartesi 15:36
     
     
    Cheers mate, this really helped me out. Was scratching my head for a while when thread sleep caused my app to simply freeze.