getnit 0 Light Poster

I am trying to establish a pause and play communication between threads.I am spawning two threads for two forms at the startup, one is the mainthread which is for mainwindow and second thread of form2,

var thread = new Thread(ThreadStart);
thread.TrySetApartmentState(ApartmentState.STA);
thread.Start();

private static void ThreadStart()
    {
        Application.Run(new SecondForm()); // other form started on its own UI thread
    }

Mainwindow has a button on clicking which multiple threads would be spawn for different operations - it executes without pause until the intended jobs are complete.

In the second form I have two buttons stop, and start clicking on which I should be able to stop and start the mainwindow(All the child threads).

For this I am declaring public static ManualResetEvent mre = new ManualResetEvent(false);

in program.cs where two UI threads are started and trying to invoke it from the second form on start and stop

private void btnStop_Click(object sender, EventArgs e)
{
    Invoke((MethodInvoker) delegate
    {
        Program.mre.WaitOne(); 
    });
}

private void btnStart_Click(object sender, EventArgs e)
{
    Invoke((MethodInvoker) delegate
    {
        Program.mre.Set();
    });
}

Once I click the button on mainwindow which continues with series of operations I try to click on STOP button in secondform, this disables the second form, and the operation from the mainwindow continues, please let me know where I am going wrong

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.