使用计时器在线程内启动应用程序
我有一个线程,在这个线程中我调用一个需要很长时间才能处理的方法。
该方法有一个 foreach
,每次迭代使用一个计数器 (counter++)
我想这样做:
- 在
foreach
中,如果 counter > > 20:- 检查是否有 name =“SendMailService.exe”的进程正在执行。
- 如果不执行,请使用
process.start(sendmailservice.exe);
启动 - 如果执行,请等待 3 分钟,然后再次检查。
- 在foreach代码之后
- 检查是否有名称=“SendMailService.exe”的进程正在执行。
- 如果进程正在执行,请等待该进程完成。
有人能理解我吗?任何帮助将不胜感激。
我的代码是:
private void bEnviarAleatorioSeleccionados_Click(object sender, EventArgs e)
{
CrossThreadCalls();
this.bEnviarAleatorioSeleccionados.Enabled = false;
// Threads werden erzeugt
Thread hiloMain = new Thread(
new ThreadStart(EnviarAleatorioSeleccionadosYEnviarCorreo));
hiloMain.Start(); // Threads werden gestartet
}
private void EnviarAleatorioSeleccionadosYEnviarCorreo()
{
string rutaFicheroMp3 = GetRutaFicheroMp3DeLoquendo(textoLoquendo);
int contador = 0;
foreach (ListViewItem item in lstRecientes.Items)
{
if (item.Checked)
{
contador++;
EnviarCorreoParaElementoListView(item, rutaFicheroMp3, item.Text);
}
// DO THE CHECK HERE, EACH 3 MINUTES (TIMER) !!!! HOW ????????
if (contador > 20)
{
var listaP = Process.GetProcesses().FirstOrDefault(
p => p.ProcessName == "ServicioEnvioCorreo.exe");
if (listaP == null)
{
// Iniciar Envio de Correo
EnviarCorreosPorProceso();
}
}
}
EliminarFicheroLoquendo(rutaFicheroMp3);
this.bEnviarAleatorioSeleccionados.Enabled = true;
}
I have a thread, within this thread I call a method which takes a long time to process.
The method has a foreach
using a counter for each iteration (counter++)
I want to do this:
- Within
foreach
, if counter > 20:- Check if any proccess with name = "SendMailService.exe" is executing.
- If NOT executing, launch using
process.start(sendmailservice.exe);
- If executing wait 3 minutes and then check again.
- After the foreach code
- Check if any proccess with name = "SendMailService.exe" is executing.
- If process is executing, wait until the process finished.
Anybody can understand me? any help will be appreciated.
My code is:
private void bEnviarAleatorioSeleccionados_Click(object sender, EventArgs e)
{
CrossThreadCalls();
this.bEnviarAleatorioSeleccionados.Enabled = false;
// Threads werden erzeugt
Thread hiloMain = new Thread(
new ThreadStart(EnviarAleatorioSeleccionadosYEnviarCorreo));
hiloMain.Start(); // Threads werden gestartet
}
private void EnviarAleatorioSeleccionadosYEnviarCorreo()
{
string rutaFicheroMp3 = GetRutaFicheroMp3DeLoquendo(textoLoquendo);
int contador = 0;
foreach (ListViewItem item in lstRecientes.Items)
{
if (item.Checked)
{
contador++;
EnviarCorreoParaElementoListView(item, rutaFicheroMp3, item.Text);
}
// DO THE CHECK HERE, EACH 3 MINUTES (TIMER) !!!! HOW ????????
if (contador > 20)
{
var listaP = Process.GetProcesses().FirstOrDefault(
p => p.ProcessName == "ServicioEnvioCorreo.exe");
if (listaP == null)
{
// Iniciar Envio de Correo
EnviarCorreosPorProceso();
}
}
}
EliminarFicheroLoquendo(rutaFicheroMp3);
this.bEnviarAleatorioSeleccionados.Enabled = true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,有两件事:
既然你无论如何都是在一个单独的线程上执行此操作:
你可以轻松地阻塞线程进行 3 分钟检查,而不是使用计时器。一个简单的
Thread.Sleep(180000);
会给循环带来 3 分钟的延迟。话虽这么说,我怀疑这是否真的是解决这个问题的最佳方法。由于您自己启动该过程,因此可以将处理程序附加到
Process.Exited
事件,并在进程退出后“重新启动”进程。这将消除对循环、检查、甚至可能整个单独线程的全部需要。只需启动该进程,当它存在时,重新启动它(20x)。Well, two things:
Since you're doing this on a separate thread anyways:
You could easily just block the thread for your 3 minute check, instead of using a timer. A simple
Thread.Sleep(180000);
will put a 3 minute delay into your loop.That being said, I'd question whether this is really the best approach to this problem. Since you're starting the process yourself, you could attach a handler to the
Process.Exited
event, and "restart" the process after it exits. This would eliminate the entire need for the loops, checks, and probably even the entire separate thread. Just start the process, and when it exists, restart it (20x).