每秒运行一次Visual C#

我有计时器的问题。 我在函数中有函数(在函数中绘制)

void func(){ /*...do something ... */ for(){ for() { /*loop*/ draw(A,B, Pen); } /*... do something ...*/ } } 

这是绘图function

  public void draw1(Point Poc, Point Kra, Pen o) { Graphics g = this.CreateGraphics(); g.DrawLine(o,Poc.X+4, Poc.Y+4,Kra.X+4, Kra.Y+4); g.Dispose(); } 

我在按钮点击时调用函数’func’

 private void button4_Click(object sender, EventArgs e){ func(); } 

我想调用绘制函数evry second(每秒绘制一行)。 在绘图之间,函数需要继续工作并计算=循环,并绘制下一行一段时间(间隔)。 我试过了

 timer1.Tick += new EventHandler(timer1_Tick); 

等等..

 private void timer1_Tick(object sender, EventArgs e) { ... draw(A, B, Pen) } 

等等..

但所有这些都会阻止我的function,并绘制一条随机线。 我只想要函数’func’中两张图之间的时间(间隔)。 没有计时器工作正常,但立即绘制所有行,我需要慢速绘图。 干杯。

我并不完全清楚你要做什么,但是,一般来说,你可以使用Timer类的一个对象来指定在指定的时间间隔内执行的代码。 代码看起来像这样:

 Timer myTimer = new Timer(); myTimer.Elapsed += new ElapsedEventHandler(DisplayTimeEvent); myTimer.Interval = 1000; // 1000 ms is one second myTimer.Start(); public static void DisplayTimeEvent(object source, ElapsedEventArgs e) { // code here will run every second } 

试试这个

 var aTimer = new System.Timers.Timer(1000); aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent); aTimer.Interval = 1000; aTimer.Enabled = true; //if your code is not registers timer globally then uncomment following code //GC.KeepAlive(aTimer); private void OnTimedEvent(object source, ElapsedEventArgs e) { draw(A, B, Pen); } 

您没有在WinForms应用程序中绘制 ,您响应更新或绘制消息。 在表单的Paint事件中执行您想要执行的操作(或覆盖OnPaint方法)。 如果要重新绘制表单,请使用Form.Invalidate 。 例如,在计时器勾号中调用Form.Invalidate

现在解决了

 System.Threading.Thread.Sleep(700);