C#重新运行线程

时间:2015-08-15 07:33:03

标签: c# multithreading backgroundworker

我想在完成工作后重新运行线程。我有两个程序。一个在Windows窗体中,另一个在cmd中。 Windows窗体程序在cmd中运行程序。

我尝试使用while(true)和if:process.HasExited,.WaitForExit,.Join on thred,.IsBusy和RunWorkerCompleted上的重新运行方法。但它不起作用。

BgWorker代码(点击按钮时的操作):

        backgroundWorker1.RunWorkerAsync();
        backgroundWorker1.DoWork += new DoWorkEventHandler(uruchomWatek);

我想重新运行线程的函数whitch

private void uruchomWatek(object sender, DoWorkEventArgs e)
{
    String polaczenieZDB = config.Default.adresDb + ";" + config.Default.nazwaDb + ";" + config.Default.login + ";" + config.Default.haslo;

    //przygotowuję proces 
    Process pr = new Process();
    ProcessStartInfo prs = new ProcessStartInfo();
    //uruchamiam cmd
    prs.FileName = "cmd";
    // /c START uruchamia program w cmd, przekazuję tutaj prametry
    prs.Arguments = " /c START " + " " + @sciezkaDoSlaveTextBox.Text + " " + ipAdresTextBox.Text + " "
                    + numerPortuTextBox.Text + " " + polaczenieZDB + " " + pobierzZadaniaDoSpr();
    pr.StartInfo = prs;

    //uruchamiam proces w nowym wątku
    ThreadStart ths = new ThreadStart(() => pr.Start());
    Thread th = new Thread(ths);
    th.IsBackground = true;
    th.Start();
}

1 个答案:

答案 0 :(得分:1)

本课程可以帮助您完成此目的:

 public class BackgroundThread : BackgroundWorker
    {
        public BackgroundThread()
        {
            this.WorkerSupportsCancellation = true;
        }
        protected override void OnDoWork(DoWorkEventArgs e)
        {
            try
            {
                base.OnDoWork(e);
            }
            catch (Exception exception)
            {
                //Log Exception
            }
        }
        public void Run()
        {
            if (this.IsBusy)
                return;
            this.RunWorkerAsync();
        }
        public void Stop()
        {
            this.CancelAsync();
            this.Dispose(true);
        }
    }

编辑:

如果您想将您的班级用作计时器并按时间间隔完成任务,那么以下课程可能非常方便。

  public class BackgroundTimer : BackgroundWorker
    {
        private ManualResetEvent intervalManualReset;
        private enum ProcessStatus { Created, Running, JobCompleted, ExceptionOccured };
        private ProcessStatus processStatus = new ProcessStatus();
        public int Interval { get; set; }

        public BackgroundTimer()
        {
            this.processStatus = ProcessStatus.Created;
            this.WorkerSupportsCancellation = true;
            this.Interval = 1000;
        }

        protected override void OnRunWorkerCompleted(RunWorkerCompletedEventArgs e)
        {
            base.OnRunWorkerCompleted(e);
            if (processStatus == ProcessStatus.ExceptionOccured)
                // Log ...  
            processStatus = ProcessStatus.JobCompleted;
        }
        protected override void OnDoWork(DoWorkEventArgs e)
        {
            while (!this.CancellationPending)
            {
                try
                {
                    base.OnDoWork(e);
                    this.Sleep();
                }
                catch (Exception exception)
                {
                    // Log ...
                    this.processStatus = ProcessStatus.ExceptionOccured;
                    this.Stop();
                }
            }
            if (e != null)
                e.Cancel = true;
        }

        public void Start()
        {
            this.processStatus = ProcessStatus.Running;
            if (this.IsBusy)
                return;

            this.intervalManualReset = new ManualResetEvent(false);
            this.RunWorkerAsync();
        }
        public void Stop()
        {
            this.CancelAsync();
            this.WakeUp();
            this.Dispose(true);
        }
        public void WakeUp()
        {
            if (this.intervalManualReset != null)
                this.intervalManualReset.Set();
        }
        private void Sleep()
        {
            if (this.intervalManualReset != null)
            {
                this.intervalManualReset.Reset();
                this.intervalManualReset.WaitOne(this.Interval);
            }
        }
        public void Activate()
        {
            if (!this.IsBusy)
                // Log ...
            this.Start();
        }
    }

编辑2: 用法:

    sendThread = new BackgroundThread();
    sendThread.DoWork += sendThread_DoWork;
    sendThread.Run();
    void sendThread_DoWork(object sender, DoWorkEventArgs e)
    {
       ...
    }