Run function every second Visual C#

后端 未结 4 845
长发绾君心
长发绾君心 2021-01-14 12:08

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

void func(){

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

 draw(A,B, Pen);         


        
相关标签:
4条回答
  • 2021-01-14 12:30

    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);
    }
    
    0 讨论(0)
  • 2021-01-14 12:39

    SOLVED for now with

    System.Threading.Thread.Sleep(700);
    
    0 讨论(0)
  • 2021-01-14 12:40

    I'm not entirely clear 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 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
    }
    
    0 讨论(0)
  • 2021-01-14 12:41

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

    0 讨论(0)
提交回复
热议问题