关于Timer 传递参数的问题

时间:2020-12-26 20:35:13

public void aaa()
{
foreach (TreeNode tn in treeView1.Nodes)
                    {
                        if (tn.Text == "aaa")
                        {
                            t = new System.Timers.Timer(1000);
                            t.Elapsed += new System.Timers.ElapsedEventHandler(picloop);
                            t.Enabled = true;

                        }
                    }
}

        public void picloop(object sender, System.Timers.ElapsedEventArgs e)
        {
            MessageBox.Show("aaa");
        }




我想在picloop()中传个参数进去,望高手指教

9 个解决方案

#1


用个外部变量。

public void aaa()
{
foreach (TreeNode tn in treeView1.Nodes)
                    {
                        if (tn.Text == "aaa")
                        {
                            t = new System.Timers.Timer(1000);
                            t.Elapsed += new System.Timers.ElapsedEventHandler(picloop);
                            strs="ppppp";
                            t.Enabled = true;

                        }
                    }
}


 string strs;
        public void picloop(object sender, System.Timers.ElapsedEventArgs e)
        {
            MessageBox.Show(strs);
        }

#2


全心全意为抢分服务。。 关于Timer 传递参数的问题

#3


1楼是个办法,当然我推荐你用匿名方法:

public void aaa()
{
foreach (TreeNode tn in treeView1.Nodes)
                    {
                        if (tn.Text == "aaa")
                        {
                            t = new System.Timers.Timer(1000);
                            t.Elapsed += new System.Timers.ElapsedEventHandler(delegate (object sender,System.Timers.ElapsedEventArgs e)
{
    MessageBox.Show(tn.Text);
});
                            t.Enabled = true;

                        }
                    }
}

#4


不好意思,忘记说明不能用全局变量 ,必须传递参数过去
现在穿参数已经搞定,换了种方式


foreach (TreeNode tn in treeView1.Nodes)
                    {
                        if (tn.Text == msg.From.User)
                        {
                            //t = new System.Timers.Timer(1000);
                            //t.Elapsed += new System.Timers.ElapsedEventHandler(picloop);
                            //t.Enabled = true;
                            MainForm te = new MainForm();
                            TimerCallback timerDelegate = new TimerCallback(te.picloop);
                            System.Threading.Timer t = new System.Threading.Timer(timerDelegate, tn, 0, 1000);
                        }
                    }

 public void picloop(object tn)
        {
            
            Console.Write("aaa");
        }


现在问题是如何把object tn 转换成treenode

#5


用匿名方法就不存在你说的这个问题
你非不用的话,那可以这样:

public void picloop(object tn)
        {
            if(tn is TreeNode)
            {
                Console.Write(((TreeNode)tn).Text);
            }
        }

#6


谢谢ls,已经搞定,结贴

#7


谢谢ls,已经搞定,结贴

#8


很有用啊,谢了

#9


很有用啊,谢了

#1


用个外部变量。

public void aaa()
{
foreach (TreeNode tn in treeView1.Nodes)
                    {
                        if (tn.Text == "aaa")
                        {
                            t = new System.Timers.Timer(1000);
                            t.Elapsed += new System.Timers.ElapsedEventHandler(picloop);
                            strs="ppppp";
                            t.Enabled = true;

                        }
                    }
}


 string strs;
        public void picloop(object sender, System.Timers.ElapsedEventArgs e)
        {
            MessageBox.Show(strs);
        }

#2


全心全意为抢分服务。。 关于Timer 传递参数的问题

#3


1楼是个办法,当然我推荐你用匿名方法:

public void aaa()
{
foreach (TreeNode tn in treeView1.Nodes)
                    {
                        if (tn.Text == "aaa")
                        {
                            t = new System.Timers.Timer(1000);
                            t.Elapsed += new System.Timers.ElapsedEventHandler(delegate (object sender,System.Timers.ElapsedEventArgs e)
{
    MessageBox.Show(tn.Text);
});
                            t.Enabled = true;

                        }
                    }
}

#4


不好意思,忘记说明不能用全局变量 ,必须传递参数过去
现在穿参数已经搞定,换了种方式


foreach (TreeNode tn in treeView1.Nodes)
                    {
                        if (tn.Text == msg.From.User)
                        {
                            //t = new System.Timers.Timer(1000);
                            //t.Elapsed += new System.Timers.ElapsedEventHandler(picloop);
                            //t.Enabled = true;
                            MainForm te = new MainForm();
                            TimerCallback timerDelegate = new TimerCallback(te.picloop);
                            System.Threading.Timer t = new System.Threading.Timer(timerDelegate, tn, 0, 1000);
                        }
                    }

 public void picloop(object tn)
        {
            
            Console.Write("aaa");
        }


现在问题是如何把object tn 转换成treenode

#5


用匿名方法就不存在你说的这个问题
你非不用的话,那可以这样:

public void picloop(object tn)
        {
            if(tn is TreeNode)
            {
                Console.Write(((TreeNode)tn).Text);
            }
        }

#6


谢谢ls,已经搞定,结贴

#7


谢谢ls,已经搞定,结贴

#8


很有用啊,谢了

#9


很有用啊,谢了