C#重新运行线程
我想在线程完成工作后重新运行它。我有两个节目。一个在Windows窗体中,另一个在cmd中。Windows窗体程序在cmd中运行程序 我尝试使用while(true)和if with:process.HasExited、.WaitForExit、.Join on thred、.IsBusy并在RunWorkerCompleted上重新运行方法。但它不起作用 BgWorker代码(单击按钮时的操作): 我想重新运行线程的函数C#重新运行线程,c#,multithreading,backgroundworker,C#,Multithreading,Backgroundworker,我想在线程完成工作后重新运行它。我有两个节目。一个在Windows窗体中,另一个在cmd中。Windows窗体程序在cmd中运行程序 我尝试使用while(true)和if with:process.HasExited、.WaitForExit、.Join on thred、.IsBusy并在RunWorkerCompleted上重新运行方法。但它不起作用 BgWorker代码(单击按钮时的操作): 我想重新运行线程的函数 private void uruchomWatek(object sen
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();
}
本课程可帮助您实现以下目的:
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:
用法:
首先看起来不错,但我仍然不知道如何使用它。我不想用定时器,因为shell程序检查一些东西,我想在它完成后重新运行它。好的,我的朋友,所以你应该使用第一个类。为了便于使用,我将编辑我的答案以向您解释。我按照您所写的做了;-)我不知道如何使用它来重新运行我的shell程序,只要想再次运行它就调用Run方法。好的,它可以工作。我添加Process.Start(prs.WaitForExit();:-)Thakns,我把线拔掉。当(true){if(process.HasExited){start}
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();
}
}
sendThread = new BackgroundThread();
sendThread.DoWork += sendThread_DoWork;
sendThread.Run();
void sendThread_DoWork(object sender, DoWorkEventArgs e)
{
...
}