我的计时器赢了';t勾选c#
我的计时器不会滴答作响,我试着用打印来检查计时器是否启动了,它启动了,但从不滴答作响。 代码如下:我的计时器赢了';t勾选c#,c#,timer,C#,Timer,我的计时器不会滴答作响,我试着用打印来检查计时器是否启动了,它启动了,但从不滴答作响。 代码如下: public Form1() { InitializeComponent(); this.DoubleBuffered = true; timer = new Timer(); timer.Enabled = true; timer.Interval = 50; timer.Tick +=
public Form1()
{
InitializeComponent();
this.DoubleBuffered = true;
timer = new Timer();
timer.Enabled = true;
timer.Interval = 50;
timer.Tick += new EventHandler(timer_Tick);
//Console.WriteLine("STARTING TIMER");
timer.Start();
NewFile();
}
private void timer_Tick(object sender, EventArgs e)
{
MessageBox.Show("TIMER TICKS");
doc.MoveBalls(leftX, topY, width, height);
doc.CheckCollision();
Invalidate(true);
Console.WriteLine("Moving2");
}
在附加
OnTick
事件之前已启用计时器。将Enabled
设置为true基本上与timer.Start()
相同。删除已启用的分配,或用它替换Start()调用。在附加OnTick
事件之前已启用计时器。将Enabled
设置为true基本上与timer.Start()
相同。删除已启用的分配,或用它替换Start()调用。使用System.Windows.Forms.Timer
使用winform
public Form1()
{
InitializeComponent();
this.DoubleBuffered = true;
System.Windows.Forms.Timer timer = new Timer();
timer.Enabled = true;
timer.Interval = 50;
timer.Tick += new EventHandler(timer_Tick);
//Console.WriteLine("STARTING TIMER");
timer.Start();
}
使用
System.Windows.Forms.Timer
处理winform
public Form1()
{
InitializeComponent();
this.DoubleBuffered = true;
System.Windows.Forms.Timer timer = new Timer();
timer.Enabled = true;
timer.Interval = 50;
timer.Tick += new EventHandler(timer_Tick);
//Console.WriteLine("STARTING TIMER");
timer.Start();
}
除了上面给出的答案外,您还可以使用一个接受回调的计时器线程和一个状态对象来保证工作线程的安全,如下所示 在名称空间之前包含库之后
using System.Threading;
private readonly TimeSpan _updateInterval = TimeSpan.FromSeconds(10);
private Timer _timer = new Timer(CallBakFunction, null, _updateInterval, _updateInterval);
private void CallBakFunction(object state)
{
}
//您的命名空间
using System.Threading;
private readonly TimeSpan _updateInterval = TimeSpan.FromSeconds(10);
private Timer _timer = new Timer(CallBakFunction, null, _updateInterval, _updateInterval);
private void CallBakFunction(object state)
{
}
在您的情况下,可以按如下方式进行:
private readonly TimeSpan _updateInterval = TimeSpan.FromSeconds(10);
private Timer _timer ;
public Form1()
{
InitializeComponent();
this.DoubleBuffered = true;
_timer = new Timer(timer_Tick, null, _updateInterval, _updateInterval);
NewFile();
}
private void timer_Tick(object state)
{
MessageBox.Show("TIMER TICKS");
doc.MoveBalls(leftX, topY, width, height);
doc.CheckCollision();
Invalidate(true);
Console.WriteLine("Moving2");
}
我已更新了一个,请查看以下内容:
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
{
private readonly TimeSpan _updateInterval = TimeSpan.FromSeconds(1);
private System.Threading.Timer _timer;
public Form1()
{
InitializeComponent(); this.DoubleBuffered = true;
_timer = new System.Threading.Timer(timer_Tick, null, _updateInterval, _updateInterval);
NewFile();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void NewFile() { }
private void timer_Tick(object state)
{
MessageBox.Show("TIMER TICKS");
//doc.MoveBalls(leftX, topY, width, height);
//doc.CheckCollision();
//Invalidate(true);
Console.WriteLine("Moving2");
}
}
}
除了上面给出的答案外,您还可以使用一个接受回调的计时器线程和一个状态对象来保证工作线程的安全,如下所示 在名称空间之前包含库之后
using System.Threading;
private readonly TimeSpan _updateInterval = TimeSpan.FromSeconds(10);
private Timer _timer = new Timer(CallBakFunction, null, _updateInterval, _updateInterval);
private void CallBakFunction(object state)
{
}
//您的命名空间
using System.Threading;
private readonly TimeSpan _updateInterval = TimeSpan.FromSeconds(10);
private Timer _timer = new Timer(CallBakFunction, null, _updateInterval, _updateInterval);
private void CallBakFunction(object state)
{
}
在您的情况下,可以按如下方式进行:
private readonly TimeSpan _updateInterval = TimeSpan.FromSeconds(10);
private Timer _timer ;
public Form1()
{
InitializeComponent();
this.DoubleBuffered = true;
_timer = new Timer(timer_Tick, null, _updateInterval, _updateInterval);
NewFile();
}
private void timer_Tick(object state)
{
MessageBox.Show("TIMER TICKS");
doc.MoveBalls(leftX, topY, width, height);
doc.CheckCollision();
Invalidate(true);
Console.WriteLine("Moving2");
}
我已更新了一个,请查看以下内容:
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
{
private readonly TimeSpan _updateInterval = TimeSpan.FromSeconds(1);
private System.Threading.Timer _timer;
public Form1()
{
InitializeComponent(); this.DoubleBuffered = true;
_timer = new System.Threading.Timer(timer_Tick, null, _updateInterval, _updateInterval);
NewFile();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void NewFile() { }
private void timer_Tick(object state)
{
MessageBox.Show("TIMER TICKS");
//doc.MoveBalls(leftX, topY, width, height);
//doc.CheckCollision();
//Invalidate(true);
Console.WriteLine("Moving2");
}
}
}
我改变了,又一次它没有滴答声是winform应用程序吗?也许你的问题不在于计时器,而是其他地方的问题即使有其他地方的问题,我正在打印timer_tick中的某些内容,但它没有在输出中显示任何内容。是的,我理解,但您在这里发布的代码与我在winform应用程序中使用的代码相同,并且在这里工作正常,因此此代码没有问题。我已更改,并且再次说明它没有滴答滴答滴答滴答滴答滴答滴答滴答滴答滴答滴答滴答滴答滴答滴答滴答滴答滴答滴答滴答滴答滴答滴答滴答滴答滴答滴答滴答滴答,问题在其他地方即使有其他地方的问题,我正在打印timer_tick中的某些内容,但它没有在输出中显示任何内容。是的,我理解,但您在这里发布的代码与我在winform应用程序中使用的代码相同,在这里工作正常,因此此代码没有问题。您使用的是哪个.NET framework?您使用的计时器(名称空间)?@JuanIgnacioCornet System.Windows.Forms;您正在使用哪个.NET框架?您使用的计时器(名称空间)?@JuanIgnacioCornet System.Windows.Forms;有一个错误计时器构造函数可以有一个或零个参数。我应该在哪里初始化system.windows.forms.Timer?有一个错误计时器构造函数可以有一个或零个参数。我应该在哪里初始化system.windows.forms.Timer??