i want to creat form using thread but the form disappear
i write this
private void button1_Click(object sender, EventArgs e)
{
Thread T = new Thread(CreateForm);
T.Start();
}
void CreateForm()
{
Form2 f = new Form2();
f.Show();
}

Question About Threads
highpockets
Did any of my other suggestions help Making it a member may just solve the problem, but I doubt it, because the thread will still end.
ybuzby
First of all let me say that if you don't know what a member variable is, then you're not really at a point where you should be playing with threads. You should aim to crawl, then walk, then run.
A member variable is a variable defined within the class, but not within a method. This variable is then visible throughout the class, hence a member variable. But, like I said, I don't think that will help, unless you create it in the same thread, and use threading within the form class for any work that needs doing. In any case, it makes no sense to run UI on another thread. If it's just a window, the thread achieves nothing. If it does some serious work, that form will be unresponsive as it's on the same thread as the work is being done. Just the work needs to be done on a background thread, to keep the UI responsive.
NeilTrain
private Form2 _form; // Private member.
private void button1_Click(object sender, EventArgs e)
{
// Create thread and start it.
ThreadStart threadMethod = new ThreadStart( ShowForm2 );
Thread thread = new Thread(threadMethod);
thread.Start();
}
public void ShowForm2()
{
if( InvokeRequired )
{
// Invoke was required, create delagete and invoke method.
MethodInvoker method = new MethodInvoker( ShowForm2 );
Invoke( method );
return;
}
// If form isn't created, initialize it.
if( _form == null )
{
_form = new Form2();
}
// Show the form.
_form.Show();
}
ventura
thanks for replay but ShowDialog dont solve the problem
because that form will create another so win i close it all forms created
by it will close
Fam Won Sing
c_lover,
I am new to C#, so your question intrigued me.
Try adding a "sleep" statement to your createform method.
Form2 f = new Form2();
f.Show();
Thread.Sleep(2000);
This will allow you to visualize your new form. It disappears when the createform method ends. This is good if you are invoking this thread for a tedious background process that you wish to start,run,finish with little notice.Look at this help article on background processes.
ms-help://MS.VSExpressCC.v80/MS.NETFramework.v20.en/dv_mclictl/html/4691b796-9200-471a-89c3-ba4c7cc78c03.htm
sorry about the formatting the link, I don't have a handle on the forum posting!
your current need for a new thread process is unknown at this time. But maybe a new thread is not what you need.
Thanks for your post! Your post sent me in a new direction today and I learned alot.
dougal83
The Show method shows a modeless form, which means that the code that created it keeps on running. In this case, that means the thread ends. Your form should be a member variable, and you should probably just show it, and use threading within the form for any work that the form does.
Or you could call ShowDialog, which would show a modal form, I imagine that would work fine because the form would be modal on another thread.
hyungshinp
Orobias
VasilyU
Instead of creating a form in a separate thread, create the form in the UI thread, and execute the task that must be performed on another thread. So, you'll have to disconnect the UI from the logic (which is always a good thing).