开发者

Form.ShowDialog in another thread

In main thread I created a worker thread that creates a new form and calls form.showDialog. After calling form.Close the application dies.

Why is this happening?

//called from someone worker thread. not from main thread
    void s_notificationAgent_AgentError(object sender, EventArgs e)
    {
        try
        {
            ConnectingForm form = new ConnectionForm();
            form.ShowDialog();//if i close form clicking on button. app dies(

        }
        catch(Exception ex)
        {

        }                       
    }

UPD: because I do not have the right to respond to this question i answered question in 'edit':) after googling i found that..

the cause is that you're accessing the Application object on a thread other than the thread it was created on. The solution is to invoke a method onto the thread that created the Application object (or the owner).

it helped me:)

    //called from someone worker thread. not from main thread
    void s_notificationAgent_AgentError(object sender, EventArgs e)
    {          
            //through the synchContext calling methods from GUI thread
         winContext.Send(_=> {form = new Form1();},null);   
         winContext.Send(_=> form.Sho开发者_如何转开发wDialog(),null);                                              
    }


All UI code runs on a single thread. Once that thread has ended, the application has ended, the message pump is torn down, etc.

In your worker thread, you need to create a new application and run your from on it, just like your program does when it starts up (usually this code is generated for you). Look in your program.cs to see this generated code, and copy and paste it to your worker thread.

Something like this: System.Windows.Forms.Application.Run(new MyDialogInTheWorkerThread());

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜