How to create a form in your stream and save it throughout the life of the application

I am creating a small testing component and run into a problem

Basically, the component is a class decorator that controls all access to the database, it creates a form with two buttons on it: "Simulate a lost connection" and "Reconnect". Click the button, and instead of passing function calls through the wrapper, you start throwing NoConnectionException () with ease and simplicity, and are also useful for testing.

The problem is that this particular application, when it detects a lost connection, calls the modal dialog box "connection lost!" which is there until the connection is restored. Since this is modal, I cannot press my button to simulate a restored connection.

What I need to do is run my small test form in another thread. I'm not quite sure how to do this. I tried

new Thread( new ThreadStart( (Action)delegate {_form.Start();} ) ).Start(); 

But the thread closes as soon as the method returns, so the form never appears, except instantly.

Any idea how I go to achieve what I want?

+3
source share
4 answers

You will need to start the message loop of the newly created thread. You can do this by calling Application.Run (form).

+3
source

Try

 new Thread( new ThreadStart((Action)delegate { _form.Start(); System.Windows.Forms.Application.Run(); } ) ).Start(); 
+1
source

It looks like you are not holding on to your thread. This is an object, like everything else, so if it is bound to your method, it will go out of scope after the exit of your method. Try making it an instance variable.

0
source
 showdialog : System.Threading.Thread t = new System.Threading.Thread(new form1().ShowDialog()); t.Start(); 
0
source

All Articles