C# 如何启用从窗体到类计时器的变量

C# 如何启用从窗体到类计时器的变量,c#,C#,我这里有一个主要的表单类: public partial class Main : Form { public void button_Click(object sender, EventArgs e) { //here I want to call function runtime() from SchedulingTimer class // in

我这里有一个主要的表单类:

        public partial class Main : Form
        {
            public void button_Click(object sender, EventArgs e)
            {
               //here I want to call function runtime() from SchedulingTimer class
                 // in oder to run afunc() every second or any Interval
            }
           public void afunc()
                {
                  Message.Show(textbox1.Text);
                 }
       }
我有一个计时器类:

public class SchedulingTimer
    {

        public static  void runtime()
        {
            Timer myTimer = new Timer();
            myTimer.Elapsed += new ElapsedEventHandler(DisplayTimeEvent);
            myTimer.Interval =10000 ; // 1000 ms is one second
            myTimer.Start();

        }

        public static void DisplayTimeEvent(object source, ElapsedEventArgs e)
        {
                //call function from main which have agrument textbox.Text
                   afunc();//or any function which variable sended from Main form
        }
    }
但是当我在
displaytimevent
方法中调用
afunc
时,它有一些错误,原因是这是一个
静态方法,因此无法访问
textbox1.Text
。我想我的代码有点错误

更新:

  • 我设置了
    myTimer.Enable=true
    ,然后单击
    按钮
    但什么都没有 发生了。似乎
    afunc()
    不起作用
  • 在DisplayTimeEvent中创建Main方法的实例
    Main objMain=new Main()
    objMain.afunc()并且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!");
    
    我在afunc中调用Yahoo类,这真的很困惑。当我点击
    按钮时,它只显示:
    (“请选择搜索引擎!”)事件,尽管我已经签入了
    单选按钮


首先,您应该这样做:
\u timer.Enabled=true;//启用它

  Timer myTimer = new Timer();

            myTimer.Elapsed += new ElapsedEventHandler(DisplayTimeEvent);
            myTimer.Interval =10000 ; // 1000 ms is one second
            myTimer.Enabled = true

出现错误的原因是
运行时
方法是静态的,因此无法直接访问非静态方法/属性

您可以将
操作
传递到
运行时
方法中,这样它将触发您从主
表单

例如:

private void button1_Click(object sender, EventArgs e)
{
    SchedulingTimer.runtime(afunc);
}


public class SchedulingTimer
{
    public static void runtime(Action callback)
    {
        System.Timers.Timer myTimer = new System.Timers.Timer();

        // when timer fires call the action/method passed in
        myTimer.Elapsed += (s, e) => callback.Invoke();
        myTimer.Interval = 10000; // 1000 ms is one second
        myTimer.Start();
    }
}
但是,如果从计时器访问UI控件,则可能需要调用UI线程

private void button1_Click(object sender, EventArgs e)
{
    SchedulingTimer.runtime(() => base.Invoke((Action)delegate 
    {
       afunc();
    }));
}

您应该使用
System.Windows.Forms.Timer
而不是
System.Timers.Timer
。前者在尝试访问主窗体时可以避免交叉线程问题

我问你为什么需要
SchedulingTimer
类。我会把代码写在表格里

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

您可以单击并用收到的确切错误消息更新您的问题吗?您必须在DisplayTimeEvent中创建Main方法的实例。Main objMain=新的Main();objMain.afunc()@马鲁蒂,我已经更新了我的问题。请再看一次!谢谢,按照您的建议,我设置了Timer.Enabled=true。但之后,我点击按钮->无任何事情发生,afunc不运行。谢谢你!成功了。!!!但我如何设置它每秒钟调用一次函数,但只能在一分钟内调用。例如:在Min=30->run->Min=31中,它会停止