|
-
April 13th, 2009, 11:57 AM
#1
Creating and Using Windows.Forms inside Threads
Hi,
I am just wondering something related to safety:
Is it safe to create Windows forms from separated Threads (System.Threading) ?
The Thread is created from a UserControl (from a click button for example).
The point is that when the thread is started I need to show some "custom forms" (Windows.Forms) that are created inside the thread in order to promp to the user the -OK and -CANCEL options in order to take a desition from there.
The thread is a single thread apartment (STA)
Example:
Code:
namespace ThreadTestandForms
{
public partial class FormGUI : UserControl
{
private void button2_Click(object sender, EventArgs e)
{
//WANT TO KEEP GUI RESPONSIVE.
//WANT TO KEEP GUI RESPONSIVE.
Thread ThreadSendParts;
ThreadSendParts = new Thread(new ThreadStart(RunsOnThread));
ThreadSendParts.SetApartmentState(ApartmentState.STA);
//I want to use a good priority.
ThreadSendParts.Priority = ThreadPriority.Highest;
ThreadSendParts.Start();
FrmSentPrtPgres.ShowDialog(); //I need to show a modal dialog after running the thread.
}
private void RunsOnThread()
{
for (int i = 0; i <= 100; i += 10) //EMULATE VERY HEAVY PROCESS
{
Thread.Sleep(200); //EMULATE VERY HEAVY PROCESS
//This method uses BeginInvoke and InvokeRequired
ShowProgress("My Progress Status", i);
//Example of doing some custom form with some custom conditions.
FrmMyMessage MyForm = new FrmMyMessage("Do you want do this or that?");
//show message stop exectuting until user takes a decision.
DialogResult DLRes = MyForm.ShowDialog();
if (DLRes == DialogResult.OK)
{
//do something this
}
else
{
//do something that
}
}
//Execute some extra things when finishing the thread.
//This method uses BeginInvoke and InvokeRequired
ThreadCompleted();
}
}
}
Regards,
Last edited by ennrike; April 13th, 2009 at 05:30 PM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|