进程队列定时器

发布于 2024-09-15 12:55:19 字数 959 浏览 3 评论 0原文

我想使用计时器每 10 秒扫描一次队列。如果此队列中有超过 0 个项目,则将第一个项目 Deque,将其作为参数传递给 Process 并运行该进程。执行此进程时应禁用计时器。一旦进程退出,它应该重新启用计时器。

队列中的项目可以手动添加,也可以来自数据库。

第一个进程完成后,以下 C# 代码不起作用。由于某种原因,计时器不再启用。有人可以帮忙吗?

public MainForm()
{
    InitializeComponent();

    queue = new Queue<string>();
    process = new Process();
    process.Exited += new EventHandler(Process_Exited);
    process.EnableRaisingEvents = true;
}

void StartProcess(string args)
{
    ProcessStartInfo psi = new ProcessStartInfo();
    psi.FileName = @"C:\Program Files\My Software\RunProcess.exe";
    psi.Arguments = args;
    psi.WindowStyle = ProcessWindowStyle.Minimized;

    process.StartInfo = psi;
    process.Start();
}

void Process_Exited(object sender, EventArgs e)
{
    timer.Enabled = true;
}

void Timer_Tick(object sender, EventArgs e)
{
    if (queue.Count > 0)
    {
        timer.Enabled = false;
        StartProcess(queue.Dequeue());
    }
}

I want to scan a queue every 10 seconds using a Timer. If there are more then 0 items in this queue then Deque the first one, pass it as an argument to a Process and run that process. The timer should be disabled while this process executes. Once the process exits, it should re-enable the timer.

The items in the queue can be added manually or can come from a database.

The following C# code does not works after the first process is finished. For some reason the timer is not enabled again. Can somebody help?

public MainForm()
{
    InitializeComponent();

    queue = new Queue<string>();
    process = new Process();
    process.Exited += new EventHandler(Process_Exited);
    process.EnableRaisingEvents = true;
}

void StartProcess(string args)
{
    ProcessStartInfo psi = new ProcessStartInfo();
    psi.FileName = @"C:\Program Files\My Software\RunProcess.exe";
    psi.Arguments = args;
    psi.WindowStyle = ProcessWindowStyle.Minimized;

    process.StartInfo = psi;
    process.Start();
}

void Process_Exited(object sender, EventArgs e)
{
    timer.Enabled = true;
}

void Timer_Tick(object sender, EventArgs e)
{
    if (queue.Count > 0)
    {
        timer.Enabled = false;
        StartProcess(queue.Dequeue());
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

束缚m 2024-09-22 12:55:19

由于时间不够,我没有查找详细信息,但我发现将 timer.Enabled = true; 分派到 UI 线程即可解决问题。

编辑:刚刚检查了 MSDN:“这个 Windows 计时器是为单线程环境设计的,其中 UI 线程用于执行处理。它要求用户代码有一个可用的 UI 消息泵,并且始终从同一个线程进行操作,或将调用编组到另一个线程”

I didn't look up the details for lack of time but I found out that dispatching timer.Enabled = true; to the UI thread will do the trick.

EDIT: Just checked MSDN: "This Windows timer is designed for a single-threaded environment where UI threads are used to perform processing. It requires that the user code have a UI message pump available and always operate from the same thread, or marshal the call onto another thread. "

你列表最软的妹 2024-09-22 12:55:19

考虑使用 Process.WaitForExit() 而不是处理 Exited 事件。这样,您可以设置超时值(以防进程永远不会退出),并且可以从 Timer_Tick() 方法中启动/停止计时器。

Consider using Process.WaitForExit() instead of handling the Exited event. This way, you can set a timeout value (in case the process never exits) and you can start/stop the timer from within the Timer_Tick() method.

蓦然回首 2024-09-22 12:55:19

运气好的话:

timer.AutoReset = true;

Any luck with:

timer.AutoReset = true;

?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文