Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
In C#, how can I create a new form in thread ?
C#
//----- Begin Thread // Recieve Data From Socket
while (True)
{

Test (AccesMethodNewFom()); // if the ChatWindow is not open will ...
}

//-----End Thread

//-----AccesMethodNewFom() // Open the ChatWindow

Form F=new Form();
//.... blablabla ....
F.show()

Notice I 'd like to use "F.show()" not "Application.run(F)"
Any solution for my graduation project, plz, please?
Thank you very much...
Posted
Updated 1-Jun-10 1:47am
v3

I would recommend that you employ the services of one of the recently invented things called a search engine, with a search term something like c# create form in thread, or similar.
 
Share this answer
 
Comments
Member 15263051 18-Oct-21 6:06am    
It's crazy. I did that and it brought me here. It's almost like answering the question is a better use of time than telling them to google it.
While you are somewhat right in that using Form.Show on a separate thread doesn't generally work, it's not for the reasons you're thinking.

When you do a new thread such as in:

C#
Thread t = new Thread(OpenNewForm);
t.Start();


whenever that method completes (in this case OpenNewForm), the thread will "go away" killing anything within it. Because you're using .Show(), the method will complete.

If however, you used .ShowDialog(), the thread will not finish until that thread is closed.

And, even though I know this works, I just tested it like so:

C#
private void button1_Click(object sender, EventArgs e)
{
  Thread t = new Thread(OpenNewForm);
  t.Start();
}

private void OpenNewForm()
{
  Form1 newForm = new Form1();
  newForm.ShowDialog();
}


Doing this works...you can even close the main form and the application won't close until newForm is closed and the thread has been allowed to complete.
 
Share this answer
 
The way your code is currently stated, you're going to create a new form infinitely. This will result in many hundreds of forms instantly created before your system crashes, and we see you back here asking why your thread is creating so many forms.

Your thread method should instantiate the form, and unless you've got code that you're not showing us, all this is going to do is create a bunch of empty forms.

Finally, the way you're instantiating and showing the form should work fine. What problem are you having?
 
Share this answer
 
Comments
mido-kok 2-Jun-10 6:37am    
No at all ,isn't infinitely ,,,because you have not seen all the code ,please Just answer me about how can I Open new Form in thread , try it and tell me , you can't do "Form.show()" in thread , and if you do "Application.Run(Form)" the rest of code does not continue the execution I think you know that,Just I have tried a lot,but no solution,it's my graduation's project ,,,
No at all ,isn't infinitely ,,,because you have not seen all the code ,please Just answer me about how can I Open new Form in thread , try it and tell me , you can't do "Form.show()" in thread , and if you do "Application.Run(Form)" the rest of code does not continue the execution I think you know that,Just I have tried a lot,but no solution,it's my graduation's project ,,,
 
Share this answer
 
Comments
William Winner 2-Jun-10 12:31pm    
Don't post a new answer...do what you did...post a comment.

Secondly, it is terrible practice to write:

while(true)

That's what JSOP is saying. You may have a break or return in there, but that is TERRIBLE coding.
William Winner 2-Jun-10 12:53pm    
Reason for my vote of 1
posted a response to an answer as an answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900