C# .NET - how to run two different form using two different thread

Asked By aman on 06-Jul-11 03:22 AM
hi all,

i m trying create an application with two forms and my requirement is that when error occurs in one formsthen the second form should hault, it should work properly ...... for same i have use the below code.

private void button1_Click(object sender, EventArgs e)

{




Thread t1 = new Thread(Method1);

t1.Start();


Thread t2 = new Thread(Method2);

t2.Start();


}

public void Method1()

{

FormforRandoms o = new FormforRandoms();//this is the form

o.ShowDialog();

}

public void Method2()

{

FormDividing o1 = new FormDividing();//this is the form

o1.ShowDialog();

}


please help me out

Thanks,
regards,
Aman Khan

Reena Jain replied to aman on 06-Jul-11 03:58 AM
Hi,

Try this

Modify the default Main() in the default Program class as follows:

static void Main()
  {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
 
    System.Threading.Thread thread =
    new System.Threading.Thread(delegate()
    {
      Form form = new Form1();
      form.ShowDialog();
    });
    thread.SetApartmentState(System.Threading.ApartmentState.STA);
    thread.Start();
 
    Application.Run(new Form1());
 
    thread.Join();
  }

You'll get two copies of Form1 at the same time and they will be completely independent and you can close them in either order without having the rug pulled out from the other.

Hope this will help you
Ravi S replied to aman on 06-Jul-11 04:30 AM
HI

It's not advisable to set up windows in a single process to operate on multiple threads. This is because there are a lot of windows messages that are processed by the main message handler (which is 1 single thread) that needs to coordinate behaviour across the forms.

 

However, what you *can* do is use the ThreadPool or BackgroundWorker classes to do the *work* in each form. So basically, each form runs from the main thread (to avoid message pumping issues), but the "processing" code they perform utilize the ThreadPool or BackgroundWorker so the work can be done asynchronously.

Jitendra Faye replied to aman on 06-Jul-11 05:09 AM
Try this -

public static void ThreadProc1()

{

    Application.Run(new Form1());

}

 

public static void ThreadProc2()

{

    Application.Run(new Form2());

}


private void button1_Click(object sender, EventArgs e)

{

    System.Threading.Thread t1 = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadProc1));

    t1.Start();

  System.Threading.Thread t2 = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadProc2));

    t2.Start();

 

}

Try and let me know.

aman replied to Jitendra Faye on 06-Jul-11 06:04 AM
hi there and thanks for ur reply.......

mine code is working fine but my requirement is that, whenever their is error in one form it should not effect the other form......like suppose error occurs on the form2 then form3 should not get closed it should continue to be running...


Thanks and Regards,
Aman Khan      
Jitendra Faye replied to aman on 06-Jul-11 09:00 AM
I got this solution in this link--

http://stackoverflow.com/questions/1560741/can-i-avoid-exceptions-in-c-continuing-code-execution

You could create a SkipOnError method like this:

private SkipOnError(Action action)
{
    try 
    {
        action();
    }
    catch
    {
    }
}

Then you could call it like so:

try
{ 
    SkipOnError(() => /*line1*/);
    line2;
    line3;
} catch {}

Edit: This should make it easier to skip a given exception:

private SkipOnError(Action action, Type exceptionToSkip)
{
    try 
    {
        action();
    }
    catch (Exception e)
    {
        if (e.GetType() != exceptionToSkip) throw;            
    }
}

Hope this will help you.