none
Thread synchronization in Async and await

    Question

  • Hi

    I have requirement regarding the asynchronous call.

    i have textbox which reads integer to process and label in winform/wpf to display some processed value

    on click of button.

    i am implementing this in async and await method to not to block the UI.

    <winodw>

    <StackPanel>

    <TextBox x:Name="myasyncwaiterText"></TextBox>
                <Label Content="my labele vale" x:Name="mylabel"></Label>
                <Button Content="check async" Click="Button_Click"></Button>

    </StackPanel>

    </winodw>

     private async void Button_Click(object sender, RoutedEventArgs e)
            {
                long mytextvalue = Convert.ToInt64(myasyncwaiterText.Text);
                long myvalue= await Task.Run(()=>Timermethod(mytextvalue));
                mylabel.Content = myvalue.ToString();
            }


            private long Timermethod(long mytextvalue)
            {          
                for (int i = 0; i < mytextvalue; i++)
                {
                    Thread.Sleep(1000);
                }
                return mytextvalue;
            }

    my question is here,

    if in i enter 10 as value in text box and click the button, and immediately enter 2 and click the button,

    if u see the label: its shows 10 because , that took long time to execute and that is the last person who updates the label.

    i want to avoid this: how to achieve this.. i want to show the which one i am executing the same one..

    how to achieve the same with async and await or other threading concepts please help me.

    • Moved by CoolDadTxMVP Tuesday, January 02, 2018 2:48 PM WPF related
    Tuesday, January 02, 2018 7:01 AM

All replies

  • Hi,

    I am not sure what you are actually trying to achieve.

    You've used "async and await " well, the rest which is bothering you may only be a logical issue.

    In fact in a commercial app, there are many factors to considerand,In my experience, you're showing is relatively rare. Data validation is the first issue to consider,in addition, too much threads would reduce the performance of your program, unless your user is runing a long task.

    If you describe the overall requirement completely then we can give good specific advice.

    Sincerely,

    Bob



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 03, 2018 1:48 AM
    Moderator
  • If you want to avoid a delay then just set the text and forget any asynchronous await.

    Beyond that, I don't follow what behaviour you want and what you do not want.

    .

    Most threaded processes are used for very expensive operations. Way more complicated than set some text and delays are expected.  Asynchronous is often used for service calls where network latency is likely to introduce a delay.

    On the face of it, you introduced a delay but now you don't want to see a delay????


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Wednesday, January 03, 2018 6:36 PM
    Moderator