C# compile error: “Invoke or BeginInvoke cannot be called on a control until the window handle has been created.”

前端 未结 9 901
时光取名叫无心
时光取名叫无心 2020-12-16 23:16

I just posted a question about how to get a delegate to update a textbox on another form. Just when I thought I had the answer using Invoke...this happens. Here is my code:<

9条回答
  •  一生所求
    2020-12-17 00:13

    I have solved this in the past using the following method:

    private void invokeOnFormThread(MethodInvoker method)
    {
        if (IsHandleCreated)
             Invoke(new EventHandler(delegate { method(); }));
        else
            method();
    }
    

    Call invokeOnFormThread instead of Invoke. It will only use the form's thread if a handle has already been created, otherwise it will use the caller's thread.

提交回复
热议问题