c# - How to enable variable from form into class Timer -


i have main form class here:

        public partial class main : form         {             public void button_click(object sender, eventargs e)             {                //here want call function runtime() schedulingtimer class                  // in oder run afunc() every second or interval             }            public void afunc()                 {                   message.show(textbox1.text);                  }        } 

and have timer class:

public class schedulingtimer     {          public static  void runtime()         {             timer mytimer = new timer();             mytimer.elapsed += new elapsedeventhandler(displaytimeevent);             mytimer.interval =10000 ; // 1000 ms 1 second             mytimer.start();          }          public static void displaytimeevent(object source, elapsedeventargs e)         {                 //call function main have agrument textbox.text                    afunc();//or function variable sended main form         }     } 

but when call afunc in displaytimeevent method has error cause static method cannot access textbox1.text. think code has mistake.

update:

  • i set mytimer.enable= true ,then, click on button nothing happend. seems afunc() doesn't work.
  • create instance of main method in displaytimeevent. main objmain=new main(); objmain.afunc(); , there detail in afunc:

        string keyw = cbkeyw.text.tostring();     string link = cblink.text.tostring();       if (radiobutton.checked)     {         yahoo yahoo = new yahoo();         yahoo.runproxyyahoo(proxylist, keyw, link, numpage, countid);     }     else         messagebox.show("please choose search engine!"); 

    i call yahoo class in afunc, confused. when click button, show:("please choose search engine!"); event though 've checked in radiobutton

you should use system.windows.forms.timer not system.timers.timer. former avoid cross threading issues when trying access main form.

i question why need schedulingtimer class. have code in form.

using system.windows.forms;  public partial class main : form {   timer mytimer = new timer { interval = 10000 };    public void button_click(object sender, eventargs e)   {      mytimer.tick += new eventhandler(ontick);      mytimer.start();   }   public void ontick(object sender, eventargs ea)   {      mytimer.stop();      message.show(textbox1.text);   }  } 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -