我编写了代码,用于检查CS:GO是否正在运行,但它没有更新。

huangapple go评论76阅读模式
英文:

I wrote code that checks if csgo is a running process, but its not updating

问题

public void HookGame()
{
Process[] processList = Process.GetProcesses();

foreach (Process process in processList)
{
    label2.Dispose();
    label3.Show();

    if (process.ProcessName.Contains("csgo"))
    {
        timer2.Start();
        label4.Text = "Csgo is running";
        this.label4.Location = new System.Drawing.Point(label4.Left = (this.Width - label4.Width), 200);
        label4.Show();
    }
    else
    {
        label4.Text = "Csgo is not running";
        this.label4.Location = new System.Drawing.Point(label4.Left = (this.Width - label4.Width), 200);
        Refresh();
        label4.Show();
    }
}

}

I called the function on timer3 which is a 5000 interval timer.

private void timer3_Tick(object sender, EventArgs e)
{
HookGame();
}

Called the timer3 on the formload.

Ok the issue is when I open csgo, the timer2 starting which it should be, but the label text doesn't change, its weird tbh can someone help me about that?

I tried making label4 dispose and show again but didn't work

英文:
public void HookGame()
{
    Process[] processList = Process.GetProcesses();

    foreach (Process process in processList)
    {
        label2.Dispose();
        label3.Show();

        if (process.ProcessName.Contains("csgo"))
        {
            timer2.Start();
            label4.Text = "Csgo is running";
            this.label4.Location = new System.Drawing.Point(label4.Left = (this.Width - label4.Width), 200);
            label4.Show();
        }
        else
        {
            label4.Text = "Csgo is not running";
            this.label4.Location = new System.Drawing.Point(label4.Left = (this.Width - label4.Width), 200);
            Refresh();
            label4.Show();
        }
    }
}

I called the function on timer3 which is a 5000 interval timer.

private void timer3_Tick(object sender, EventArgs e)
{
    HookGame();
}    

Called the timer3 on the formload.

Ok the issue is when I open csgo, the timer2 starting which it should be, but the label text doesn't change, its weird tbh can someone help me about that?

I tried making label4 dispose and show again but didn't work

答案1

得分: 0

如果在Windows Forms应用程序中的定时器事件处理程序中引发了未处理的异常,它将停止引发异常的定时器,直到定时器重新启动,之后不会引发任何后续的定时器事件。

看起来问题可能是您多次处理label2控件,这可能会引发异常。

您可以尝试将控件隐藏而不是处理它,方法是将其Visible属性设置为false,或者只需使用Hide()方法。您还可以通过仅在找到进程时更新标签文本和位置,并通过名称查找进程而不是遍历所有进程来简化代码:

public void HookGame()
{
    Process[] processList = Process.GetProcessesByName("csgo");
    bool csgoFound = processList.Length > 0;

    label2.Hide();
    label3.Show();

    label4.Text = csgoFound ? "Csgo is running" : "Csgo is not running";
    label4.Location = new System.Drawing.Point(this.Width - label4.Width, 200);
    label4.Show();
}

请注意,如果定时器已启动,您不需要重新启动定时器,因为Timer实现了在用户定义的时间间隔内引发事件的定时器。

英文:

If an unhandled exception is thrown in a timer event handler in a Windows Forms app, it will stop the timer that raised the exception, and any subsequent timer events will not be raised until the timer is restarted.

It looks like the problem might be that you are disposing the label2 control many times which can cause an exception to be thrown.

Instead of disposing the control, you could try hiding it by setting its Visible property to false or just using Hide() method. You can also simplify the code by only updating the label text and location when the process is found and by finding process by name instead of iterating through all processes:

public void HookGame()
{
    Process[] processList = Process.GetProcessesByName("csgo");
    bool csgoFound = processList.Length > 0;

    label2.Hide();
    label3.Show();

    label4.Text = csgoFound ? "Csgo is running" : "Csgo is not running";
    label4.Location = new System.Drawing.Point(label4.Left = (this.Width - label4.Width), 200);
    label4.Show();
}

Note that you do not need to restart the timer if it has already been started because Timer implements a timer that raises an event at user-defined intervals.

huangapple
  • 本文由 发表于 2023年2月27日 04:19:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574783.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定