Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Windows.Forms.Timer工作不正常_C#_Timer_Messagebox - Fatal编程技术网

C# Windows.Forms.Timer工作不正常

C# Windows.Forms.Timer工作不正常,c#,timer,messagebox,C#,Timer,Messagebox,我发布了一个示例代码,用一个更大的代码来解码一个问题 我有一个2秒计时器-System.Windows.Forms.timer 每隔2秒,我将一个全局int递增1,并使用MessageBox显示其值 如果整数达到4,我关闭主标志 这是密码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System

我发布了一个示例代码,用一个更大的代码来解码一个问题

我有一个2秒计时器-System.Windows.Forms.timer

每隔2秒,我将一个全局int递增1,并使用MessageBox显示其值 如果整数达到4,我关闭主标志

这是密码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        bool Play_On = false;
        int i = 0;
        public Form1()
        {
            InitializeComponent();
        }
        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "Play")
            {
                button1.Text = "Puase";
                Play_On = true;
            }
            else
            {
                button1.Text = "Play";
                Play_On = false;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Timer MinResTimer = new Timer();

            {
                MinResTimer.Tick += new EventHandler(MinResTimer_Elapsed);
                MinResTimer.Interval = 2000;
                MinResTimer.Enabled = true;
                MinResTimer.Start();
            }
        }
        public void MinResTimer_Elapsed(object sender, EventArgs e)
        {

            if (Play_On == true)
            {
                MessageBox.Show("timed"+ i.ToString());
                i++;
            }
            if (i == 4)
            {
                Play_On = false;
                button1.Text = "Play";

            }
        }
    }
}
问题是该标志根本没有关闭。 我得到的输出是Timed0-在一个消息框中,很多次。 我想我对messagebox有些问题-不确定


有人能帮我找出这是怎么回事吗?

在调用MessageBox之前,请将i++的增量移动到

然后,您将看到,您确实希望在显示消息框时保护事件不被仍然触发

因此,新代码将把I++移动到MessageBox之前,然后在调用MessageBox之前和之后禁用并启用计时器

    void MinResTimer_Elapsed(object sender, EventArgs e)
    {
        if (Play_On == true)
        {
            i++;
            MessageBox.Show("timed" + i.ToString());
        }
        if (i == 4)
        {
            Play_On = false;
            MinResTimer.Enabled = false;
            button1.Text = "Pause";
        }
    }  

    private void button1_Click(object sender, EventArgs e)
    {
        if (button1.Text == "Play")
        {
            button1.Text = "Puase";
            MinResTimer.Enabled = true;
            i = 0;
            Play_On = true;
        }
        else
        {
            button1.Text = "Play";
            MinResTimer.Enabled = false;
            Play_On = false;
        }
    }  
编辑#1:

    bool Play_On = false;
    int i = 0;
    Timer MinResTimer;

    private void Form1_Load(object sender, EventArgs e)
    {
        MinResTimer = new Timer();

        {
            MinResTimer.Tick += new EventHandler(MinResTimer_Elapsed);
            MinResTimer.Interval = 2000;
            MinResTimer.Enabled = true;
            MinResTimer.Start();
        }
    }
    void MinResTimer_Elapsed(object sender, EventArgs e)
    {
        if (Play_On == true && i <= 4)
        {
            i++;
            MessageBox.Show("timed" + i.ToString());
        }
        if (i == 4)
        {
            Play_On = false;
            button1.Text = "Pause";
        }
    }  

    private void button1_Click(object sender, EventArgs e)
    {
        if (button1.Text == "Play")
        {
            button1.Text = "Puase";
            Play_On = true;
        }
        else
        {
            button1.Text = "Play";
            Play_On = false;
        }
    }
bool Play\u On=false;
int i=0;
定时器最小再定时器;
私有void Form1\u加载(对象发送方、事件参数e)
{
MinResTimer=新计时器();
{
MinResTimer.Tick+=新事件处理程序(MinResTimer\u已用);
最小时间间隔=2000;
MinResTimer.Enabled=true;
MinResTimer.Start();
}
}
void MinResTimer_已过(对象发送方,事件参数e)
{

如果(Play_On==true&&i当messagebox显示时,您没有单击“确定”按钮来关闭messagebox,i++无法完成。因此,在您单击messagebox上的“确定”按钮之前,我总是等于0。

我不明白您所说的。messagebox为什么会有这么大的问题?如果我将i++的增量放在messagebox之前,则标志仍然存在未关闭。如果禁用并启用计时器,可能会失去连续性,这是我不想在我的大屏幕中看到的app@noob我认为您希望在
MessageBox.Show
之后立即执行代码,并且在关闭消息框后同时执行代码…只有一种情况会发生-如果消息框代码在调用后将执行一次对话框关闭后…您可能希望创建自定义非模态对话框以获取以前的行为。因此,当messagebox显示时,它之后的代码将不会运行?不,我希望计时器正在运行,只是如果单击按钮1,则不执行任何操作。此外,您的代码会出错,MinResTimer在当前上下文中不存在。如果g要使用上面的代码,您需要首先声明
MinResTimer
变量。然后在
Form1\u Load
事件中,您需要键入此
MinResTimer=new Timer();
而不是'Timer MinResTimer=new Timer();确切地说,这是为什么?