2

I have two forms. form1 calls starts a background running thread during its loading. once it stars running. form 2 will popup having two buttons (start&stop). when i press stop button the thread should pause and when i press start, the pause thread should start it execution from where it stopped.

I tried to use this code.

   myResetEvent.WaitOne();//  to pause  the thread

   myResetEvent.Set();   // to resume the thread.

as these events are defined in the form1but I'm wanting it to work from form2.

swiftBoy
  • 35,607
  • 26
  • 136
  • 135
Deadlock
  • 330
  • 1
  • 3
  • 21
  • Welcome to [so], you would need to pass the Background Worker to the other form and attempt to cancel eg: `abortableBackgroundWorker1.CancelAsync();` – Jeremy Thompson Jun 14 '13 at 05:36
  • @JeremyThompson thanks... can you please help me in solving the above mentioned problem.. – Deadlock Jun 14 '13 at 05:39
  • Put this through a code converter to get it to be C#: http://stackoverflow.com/questions/13206926/vb-net-progressbar-backgroundworker/13486676#13486676 then change code to not Hide the form in the `lblClose_LinkClicked` method. Then simply rename the lblClose.Text = "Restart" and enable the Timer again. HTH – Jeremy Thompson Jun 14 '13 at 05:44
  • You have this mixed up. The WaitOne(0) call needs to be in the worker code. The form class should only ever call Set() or Reset(). – Hans Passant Jun 14 '13 at 11:09

2 Answers2

0

Finally i got the answer, it worked for my case,posting it, might it will help others..

Form 1 code..

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace thread_example
{
public partial class Form1 : Form
{
    private int i = 0;
    public Thread Run_thread = null, run1 = null; // thread definition
    public static AutoResetEvent myResetEvent = new AutoResetEvent(false);//intially set to false..
    public Form1()
    {
        InitializeComponent();
        run1 = new Thread(new ThreadStart(run_tab));
        run1.IsBackground = true;
        run1.Start();
    }

    private void run_tab()
    {
        //do something.        

    }

    private void button1_Click(object sender, EventArgs e)
    {
        form2 f2 = new form2();
        f2.Show();
    }
}
}




// Form 2 code...

  namespace thread_example
  {
    public partial class form2 : Form
  {
    public form2()
    {
        InitializeComponent();
    }

    private void btn_stop_Click(object sender, EventArgs e)
    {
        Form1.myResetEvent.WaitOne();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Form1.myResetEvent.Set();
    }
 }
}
Deadlock
  • 330
  • 1
  • 3
  • 21
-1

You can pass the Event instance to the second form as form parameters.

class Form1
{
  ManualEvent myResetEvent;

  void ShowForm2()
  {
    var form2 = new Form2(myResetEvent);
    form.ShowDialog();
  }
}

class Form2
{
  ManualEvent myResetEvent;

  Form2(ManualEvent event)
  {
    myResetEvent = event;
  }

  void StopButtonClick()
  {
    myResetEvent.Reset();
  }

  void ContinueButtonClick()
  {
    myResetEvent.Set();
  }
}
Viacheslav Smityukh
  • 5,652
  • 4
  • 24
  • 42