嗨,我有一个与旧线程类似的问题,我仍然无法解决(通过 EventHandler 传回返回值)。我最终试图实现一个计时器,它会以均匀的间隔计算速度,所以我需要 Elapsed Time Event 返回某种值。我试过使用全局变量,但事件似乎并没有改变变量。有什么建议吗?提前致谢!
namespace Timer_Label
{
public static class GlobalVariables
{
public static int _stringHolder;
public static int StringHolder
{
get { return _stringHolder; }
set { _stringHolder = value; }
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
System.Timers.Timer myTimer = new System.Timers.Timer();
myTimer.Elapsed += new ElapsedEventHandler(DisplayTimeEvent);
myTimer.Interval = 500;
myTimer.Start();
MessageBox.Show(Convert.ToString(GlobalVariables.StringHolder));
}
public static void DisplayTimeEvent(object sender, ElapsedEventArgs e)
{
GlobalVariables.StringHolder = "1";
}