每秒运行一次函数 Visual C#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10954859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:55:21  来源:igfitidea点击:

Run function every second Visual C#

c#timerdraw

提问by ?еца Алекса

I have problems with timer. I have function in function (draw in func)

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

void func(){

 /*...do something ... */
for(){
   for() {
  /*loop*/

 draw(A,B, Pen);

 }

/*... do something ...*/
  }
}

This is draw 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();
      }

I call function 'func' on button click

我在按钮单击时调用函数“func”

private void button4_Click(object sender, EventArgs e){

    func();

}

I want to call draw function evry second (draw the line every second). Between drawings, function needs to continue working and calculate = loop, and draw next line for some time(interval). I tried with

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

timer1.Tick += new EventHandler(timer1_Tick);

etc..

等等..

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

etc..

等等..

but all that stops my function, and draw one random line. I just want the time(interval) between two drawings in function 'func'. Without timer works fine, but draw all lines immediately, I need slow drawing. Cheers.

但所有这一切都会停止我的功能,并随机绘制一条线。我只想要函数“func”中两个绘图之间的时间(间隔)。没有计时器工作正常,但立即绘制所有线条,我需要缓慢绘制。干杯。

采纳答案by ?еца Алекса

SOLVED for now with

现在解决了

System.Threading.Thread.Sleep(700);

回答by JeffFerguson

I'm not entirelyclear on what you're trying to do, but, in general, you can use an object of the Timer class to specify code to be executed on a specified interval. The code would look something like this:

我不完全清楚您要做什么,但一般来说,您可以使用 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
}

回答by Peter Ritchie

You don't drawin a WinForms app, you respond to update or paint messages. Do what you want to do in the form's Paintevent (or override the OnPaintmethod). When you want the form to be re-drawn, use Form.Invalidate. e.g. call Form.Invalidatein the timer tick...

您不是在 WinForms 应用程序中绘图,而是响应更新或绘制消息。在表单的Paint事件中做你想做的事情(或覆盖OnPaint方法)。当您希望重新绘制表单时,请使用Form.Invalidate. 例如调用Form.Invalidate定时器滴答...

回答by Behnam Esmaili

try this

尝试这个

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);
}