C#: How to force “calling” a method from the main thread by signaling in some way from another thread

前端 未结 3 596
情深已故
情深已故 2020-12-21 16:26

Sorry for long title, I don\'t know even the way on how to express the question

I\'m using a library which run a callback from a different context from the main thre

3条回答
  •  眼角桃花
    2020-12-21 16:46

    If you're using WinForms and you want to execute something on the UI thread, you need to call either Invoke or BeginInvoke on some control (be it a Button or a Form or whatever) that was created on that thread. You'll need a reference to it in order to do this.

    For example, with your code and assuming that you have a reference to a form called form:

    private uint lgLcdOnConfigureCB(int connection, System.IntPtr pContext)
    {
        form.Invoke(new MethodInvoker(() => OnConfigure(EventArgs.Empty)));
        return 0U;
    }
    

提交回复
热议问题