C# 重新运行线程

C# rerun thread

我想在它完成工作后重新运行线程。我有两个程序。一个在 Windows 表单中,第二个在 cmd 中。 Windows 表单程序 运行 cmd 中的程序。

我尝试在 RunWorkerCompleted 上使用 while(true) 和 if:process.HasExited、.WaitForExit、.Join on thred、.IsBusy 和 re运行 方法。但这是行不通的。

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

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

我想重新运行线程

的函数
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();
}

此 class 可能会帮助您完成此目的:

 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);
        }
    }

编辑:

如果您想将 class 用作计时器并按时间间隔执行任务,则以下 class 可能会非常方便。

  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)
    {
       ...
    }