c#重新运行线程

本文关键字:线程 重新运行 | 更新日期: 2023-09-27 18:07:58

我想重新运行线程时,它完成工作。我有两个程序。一个在Windows窗体,第二个在cmd。Windows窗体程序运行程序在cmd。

我尝试使用while(true)和if with: process。hashexited, . waitforexit, . join在thread, . isbusy和RunWorkerCompleted上的rerun方法。但这行不通。

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

c#重新运行线程

这个类可以帮助你达到这个目的:

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

EDIT:

如果你想使用你的类作为一个计时器,并且每隔一段时间完成任务,下面的类可能真的很方便。

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

EDIT 2:用法:

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