Why is InvokeRequired preferred over WindowsFormsSynchronizationContext?

前端 未结 2 756
旧时难觅i
旧时难觅i 2021-01-12 09:18

Anytime the beginner asks something like: How to update the GUI from another thread in C#?, the answer is pretty straight:

if (foo.InvokeRequired)
{
    foo.         


        
2条回答
  •  佛祖请我去吃肉
    2021-01-12 10:07

    Who said InvokeRequired / Control.BeginInvoke is preferred? If you ask me, in most cases it's an anti pattern for the exact reasons you mentioned. The question you linked to has many answers, and some actually do suggest using the synchronization context (including mine).

    While it's true that any given control could be disposed by the time you're trying to access it from the posted delegate, that's easily solved using Control.IsDisposed (as your delegate is executing on the UI thread so nothing can dispose controls while it's running):

    public partial class MyForm : Form
    {
        private readonly SynchronizationContext _context;
        public MyForm()
        {
            _context = SynchronizationContext.Current
            //...
        }
    
        private MethodOnOtherThread()
        {
             //...
             _context.Post(status => 
              {
                 // I think it's enough to check the form's IsDisposed
                 // But if you want to be extra paranoid you can test someLabel.IsDisposed
                 if (!IsDisposed) {someLabel.Text = newText;}
              },null);
        }
    }
    

提交回复
热议问题