none
BeginInvoke and SynchronizationContext both does the same thing RRS feed

  • Question

  • I am new in TPL, Async/Await. so many things are not getting clear. so posting a exmple code here. please guide me without negative marking for my post.

    My first example of BeginInvoke code given here.

    private async void button1_Click(object sender, EventArgs e)
            {
                button1.Enabled = false;
                var count = 0;
    
                await Task.Run(() =>
                {
                    for (var i = 0; i <= 500; i++)
                    {
                        count = i;
                        BeginInvoke((Action)(() =>
                        {
                            label1.Text = i.ToString();
    
                        }));
                       Thread.Sleep(100);
                    }
                });
    
                label1.Text = @"Counter " + count;
                button1.Enabled = true;
            }

    Second example where i used SynchronizationContext to update UI.

    private readonly SynchronizationContext synchronizationContext;
    private DateTime previousTime = DateTime.Now;
    
    public Form1()
    {
        InitializeComponent();
        synchronizationContext = SynchronizationContext.Current;
    }
    
    private async void button1_Click(object sender, EventArgs e)
    {
        button1.Enabled = false;
        var count = 0;
    
        await Task.Run(() =>
        {
            for (var i = 0; i <= 5000000; i++)
            {
                UpdateUI(i);
                count = i;
            }
        });
    
        label1.Text = @"Counter " + count;
        button1.Enabled = true;
    }
    
    public void UpdateUI(int value)
    {
        var timeNow = DateTime.Now;
    
        if ((DateTime.Now - previousTime).Milliseconds <= 50) return;
    
        synchronizationContext.Post(new SendOrPostCallback(o =>
        {
            label1.Text = @"Counter " + (int)o;
        }), value);             
    
        previousTime = timeNow;
    } 

    Please tell me BeginInvoke and SynchronizationContext both does the same thing. which one is more efficient. is there any specific scenario where BeginInvoke is good and in what kind of scenario SynchronizationContext would be good.

    Sunday, December 23, 2018 9:34 AM

Answers

  • Hi Studip_inn,

    Thank you for posting here.

    >>BeginInvoke and SynchronizationContext both does the same thing

    For your question, no. They are different. Some things they do the same but some of them are different.

    Please check the part of the link below, it gives a good explanation.

    This part "What is SynchronizationContext, and what does it mean to capture and use it?" shows the results. 

    https://blogs.msdn.microsoft.com/pfxteam/2012/06/15/executioncontext-vs-synchronizationcontext/

    Best Regards,

    Wendy


    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.

    Monday, December 24, 2018 6:37 AM
    Moderator

All replies

  • Sudip, you are guilty of what we call "premature optimization".  Those two operations do serve pretty much the same purpose.  BeginInvoke is the original mechanism, and SynchronizationContext takes advantage of newer aspects of the language, like lambdas.  "Efficiency" is not really a concern; you should use the one that fits better with your program flow.

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Monday, December 24, 2018 3:08 AM
  • Hi Studip_inn,

    Thank you for posting here.

    >>BeginInvoke and SynchronizationContext both does the same thing

    For your question, no. They are different. Some things they do the same but some of them are different.

    Please check the part of the link below, it gives a good explanation.

    This part "What is SynchronizationContext, and what does it mean to capture and use it?" shows the results. 

    https://blogs.msdn.microsoft.com/pfxteam/2012/06/15/executioncontext-vs-synchronizationcontext/

    Best Regards,

    Wendy


    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.

    Monday, December 24, 2018 6:37 AM
    Moderator