开发者

call Invoke method from a custom class

i was have a problem like this

My form doesn't properly display when it is launched开发者_StackOverflow社区 from another thread

now my question is how to call Invoke method from a custom class not from a Form

void call_thread()
    {

        Thread t = new Thread(new ThreadStart(this.ShowForm1));
        t.Start();

    }

 delegate void Func();
    private void ShowForm1()
    {            
        if (this.InvokeRequired) //error
        {
            Func f = new Func(ShowForm1);
            this.Invoke(f); //error
        }
        else
        {
            Form1 form1 = new Form1();
            form1.Show();
        }            
    }


You can't. Invoke is specific to Winforms Controls as it enters a message into the Windows Message pump to do whatever it is you need to do. Therefore, in your custom class, where there's obviously not Message Pump, this can't be done.


i get the Answer

in the thread i can call form1.ShowDialog(); it is not appear as a Dialog because it in another thread

the new code is

void call_thread()
    {

        Thread t = new Thread(new ThreadStart(this.ShowForm1));
        t.Start();

    }

    private void ShowForm1()
    {            
           Form1 form1 = new Form1();
            form1.ShowDialog();

    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜