views:

153

answers:

4

I'm just trying to run a new thread each time a button click even occurs which should create a new form. I tried this in the button click event in the MainForm:

private void button1_Click(object sender, EventArgs e)
{
    worker1 = new Thread(new ThreadStart(thread1));
    worker2 = new Thread(new ThreadStart(thread2));

    worker1.Start();
    worker2.Start();
}

private void thread1()
{
    SubForm s = new SubForm();
    s.Show();
}

private void thread2()
{
    SubForm s = new SubForm();
    s.Show();
}

The code in the Subform button click event goes like this:

private void button1_Click(object sender, EventArgs e)
{
    int max;
    try
    {
        max = Convert.ToInt32(textBox1.Text);
    }
    catch
    {
        MessageBox.Show("Enter numbers", "ERROR");
        return;
    }

    progressBar1.Maximum = max;

    for ( long i = 0; i < max; i++)
    {
        progressBar1.Value = Convert.ToInt32(i);
    }          
}

Is this the right way? Because I'm trying to open two independent forms, operations in one thread should not affect the other thread.

Or is BackGroundworker the solution to implement this? If yes, can anyone please help me with that?

+4  A: 

The direct answer is

private void thread1()
{
    SubForm s = new SubForm();
    //s.Show();
    Application.Run(s);
}

But I'm not saying it's a good idea.

Henk Holterman
I actually used this once in a full-screen kiosk application where I wanted a custom fatal error screen that would fill the screen and allow the machine to be rebooted even if the GUI thread was locked up. The Windows explorer.exe process was not even running, so there would be no other way to terminate the program short of a power cut if it ever locked up.
Dan Bryant
Just remember to set the `ApartmentState` to STA, or else you'll crash when you try to use the clipboard or show a file dialog.
romkyns
ya..... But it automatically gets added above the Main() itself right??
SLp
@romkyns, right. @user40, yes but that attribute is for the Main thread. You do need to configure additional MsgLoop threads.
Henk Holterman
+9  A: 

You do not need to run forms in separate threads. You can just call s.Show() on multiple forms normally. They will not block each other.

Of course, if you’re doing something else, like some sort of calculation or other task that takes a long while, then you should run that in a separate thread, but not the form.

Here is a bit of code that will let you create a progress bar that shows progress for a long process. Notice that every time to access the form from inside the thread, you have to use .Invoke(), which actually schedules that invocation to run on the GUI thread when it’s ready.

public void StartLongProcess()
{
    // Create and show the form with the progress bar
    var progressForm = new Subform();
    progressForm.Show();
    bool interrupt = false;

    // Run the calculation in a separate thread
    var thread = new Thread(() =>
    {
        // Do some calculation, presumably in some sort of loop...
        while ( ... )
        {
            // Every time you want to update the progress bar:
            progressForm.Invoke(new Action(
                () => { progressForm.ProgressBar.Value = ...; }));

            // If you’re ready to cancel the calculation:
            if (interrupt)
                break;
        }

        // The calculation is finished — close the progress form
        progressForm.Invoke(new Action(() => { progressForm.Close(); }));
    });
    thread.Start();

    // Allow the user to cancel the calculation with a Cancel button
    progressForm.CancelButton.Click += (s, e) => { interrupt = true; };
}
Timwi
@ Timwi: Thanks for your answer. But I tried it already. My requirement is different actually. Say I open two subforms and a long process is running in the SubForm1, can I access the SubForm2 now ??
SLp
@ Timwi: Sorry I didnt see your update the first time. So this is what im trying to do exactly 'run that in a separate thread, but not the form' as u said, but im wondering how? sorry, im new to these and i have just started learning.
SLp
@user403489, the advisable thing to do is not to run the long process in SubForm1, but rather run that long process in a BackgroundWorker (or Thread, but BackgroundWorker makes GUI thread interaction easier to manage.) Otherwise you're making SubForm1 non-responsive, which leads to ugly drawing artifacts (no repainting) among other things.
Dan Bryant
@user403489, I’ve posted some example code in the answer.
Timwi
@ Timwi: Thank you so much. I'l try this out.
SLp
+3  A: 

Although I'm not 100% aware of anything that says running completely seperate forms doing completely isolated operations in their own threads is dangerous in any way, running all UI operations on a single thread is generally regarded as good practice.

You can support this simply by having your Subform class use BackgroundWorker. When the form is shown, kick off the BackgroundWorker so that it processes whatever you need it to.

Then you can simply create new instances of your Subform on your GUI thread and show them. The form will show and start its operation on another thread.

This way the UI will be running on the GUI thread, but the operations the forms are running will be running on ThreadPool threads.

Update

Here's an example of what your background worker handlers might look like - note that (as usual) this is just off the top of my head, but I think you can get your head around the basic principles.

Add a BackgroundWorker to your form named worker. Hook it up to the following event handlers:

void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    // Executed on GUI thread.
    if (e.Error != null)
    {
        // Background thread errored - report it in a messagebox.
        MessageBox.Show(e.Error.ToString());
        return;
    }

    // Worker succeeded.
}

void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    // Executed on GUI thread.
    progressBar1.Value = e.ProgressPercentage;
}

void worker_DoWork(object sender, DoWorkEventArgs e)
{
    // Executed on ThreadPool thread.
    int max = (int)e.Argument;

    for (long i = 0; i < max; i++)
    {
        worker.ReportProgress(Convert.ToInt32(i));
    }
}

Your click handler would look something like:

void button1_Click(object sender, EventArgs e)
{
    int max;

    try
    {
        // This is what you have in your click handler,
        // Int32.TryParse is a much better alternative.
        max = Convert.ToInt32(textBox1.Text);
    }
    catch
    {
        MessageBox.Show("Enter numbers", "ERROR");
        return;
    }

    progressBar1.Maximum = max;

    worker.RunWorkerAsync(max);
}

I hope that helps.

Alex Humphrey
Thanks Mr Alex Humphrey. I do understand by ur answer that a BackgroundWorker is the best practise, but can u please help me with that in this example. Cos, i know what it does, but all the examples I have seen are too long to follow. So just to understand the simple stuffs, I took a small example. I hope this is useful for others too. Thanks.
SLp
@user403489: I've updated with some sample code - I hope it gives you an idea of how to use BackgroundWorker.
Alex Humphrey
Thanks Mr Alex. Your help is of great use for people like me.
SLp
@ Alex: I tried this and it works. Thank you so much. I used ' Application.Run(s); ' instead of s.show that i used originally and 'Int32.TryParse' doesnt seem to work, 'No overload for method 'TryParse' takes '1' arguments ' is the error i get. I searched for the solution. I declared 'max' as 'int' and i used 'Int32.TryParse' only. But i still get that error. neway, my Main doubt regarding the GUI form is solved. Thank you all.
SLp
@SLp: The reason I said use Int32.TryParse is so you can avoid swallowing exceptions. Use something like int max = 0; if(Int32.TryParse(textBox1.Text, out max)){ // Text is an integer. } else { // Text is not a valid integer. } - and could you click the tick mark next to your chosen answer so people with the same problem reading your question can identify the best approach? Cheers.
Alex Humphrey
A: 
supercat