【问题标题】:C# rerun threadC# 重新运行线程
【发布时间】:2015-08-15 07:33:02
【问题描述】:

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

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

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

【问题讨论】:

  • Thakns,我删除了线程。我试过类似的东西 while(true) { if(process.HasExited) { start } }

标签: c# multithreading backgroundworker


【解决方案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)
    {
       ...
    }

【讨论】:

  • 第一次看起来不错,但是还是不知道怎么用。我不想使用计时器,因为 shell 程序会检查一些东西,我想在它完成后重新运行它。
  • 好吧,我的朋友,所以你应该使用头等舱。对于用法,我将编辑我的答案为您解释。
  • 我做了你写的 ;-) 我不知道如何使用它来重新运行我的 shell 程序
  • 只要你想再次运行它就调用 Run 方法。
  • 好的,它工作。我添加 Process.Start(prs).WaitForExit(); :-)
猜你喜欢
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多