问题:ontime()线程间操作无效从不是创建控件""的线程访问它代码为:
public Form1()
{
InitializeComponent();
InitializeTimer();
// InitializeNotifyMenu();
} public void InitializeTimer()
{
this.timerClock.Elapsed += new ElapsedEventHandler(OnTimer);
this.timerClock.Interval = 200;
this.timerClock.Enabled = true; }
public void OnTimer(Object source, ElapsedEventArgs e)
{
this.clockTime++; int countDown = this.alarmTime - this.clockTime;
timeInput.Text = secondsToTime(countDown);
}
public string secondsToTime(int seconds)
{
int minutes = 0;
int hours = 0; while (seconds >= 60)
{
minutes += 1;
seconds -= 60;
}
while (minutes >= 60)
{
hours += 1;
minutes -= 60;
} string strHours = hours.ToString();
string strMinutes = minutes.ToString();
string strSeconds = seconds.ToString(); if (strHours.Length < 1) strHours = "0" + strHours;
if (strMinutes.Length < 1) strMinutes = "0" + strMinutes;
if (strSeconds.Length < 1) strSeconds = "0" + strSeconds; return strHours + ":" + strMinutes + ":" + strSeconds;
}
代码运行时发生了ontime()线程间操作无效从不是创建控件""的线程访问它。我的解决方法为:
public void OnTimer(Object source, ElapsedEventArgs e)
{
this.clockTime++; int countDown = this.alarmTime - this.clockTime;
timeInput.Text = secondsToTime(countDown);
if (timeInput.InvokeRequired == true)
{ timeInput.Invoke(source, new object[] { Text });这儿就老不对。
}
else
{
timeInput.Text = "";
}
public Form1()
{
InitializeComponent();
InitializeTimer();
// InitializeNotifyMenu();
} public void InitializeTimer()
{
this.timerClock.Elapsed += new ElapsedEventHandler(OnTimer);
this.timerClock.Interval = 200;
this.timerClock.Enabled = true; }
public void OnTimer(Object source, ElapsedEventArgs e)
{
this.clockTime++; int countDown = this.alarmTime - this.clockTime;
timeInput.Text = secondsToTime(countDown);
}
public string secondsToTime(int seconds)
{
int minutes = 0;
int hours = 0; while (seconds >= 60)
{
minutes += 1;
seconds -= 60;
}
while (minutes >= 60)
{
hours += 1;
minutes -= 60;
} string strHours = hours.ToString();
string strMinutes = minutes.ToString();
string strSeconds = seconds.ToString(); if (strHours.Length < 1) strHours = "0" + strHours;
if (strMinutes.Length < 1) strMinutes = "0" + strMinutes;
if (strSeconds.Length < 1) strSeconds = "0" + strSeconds; return strHours + ":" + strMinutes + ":" + strSeconds;
}
代码运行时发生了ontime()线程间操作无效从不是创建控件""的线程访问它。我的解决方法为:
public void OnTimer(Object source, ElapsedEventArgs e)
{
this.clockTime++; int countDown = this.alarmTime - this.clockTime;
timeInput.Text = secondsToTime(countDown);
if (timeInput.InvokeRequired == true)
{ timeInput.Invoke(source, new object[] { Text });这儿就老不对。
}
else
{
timeInput.Text = "";
}
解决方案 »
- c# 查找Ctrl+F11快捷键被占用
- 【求助】怎么样将xml转化成的html直接显示在webbrower上而不用存成文件
- sql 如何替换某个字段里的空格
- VS项目右键的“清理”按钮是做什么用的?
- 如何实现远程连接服务器
- 回答上一个问题送100分,顶者每人10分,另开一贴结分
- 如何根据Name的值取道Hashtable 的内容??
- 巨难问题: C#中怎样动态改变方法的指针?
- 小弟初涉C#,请指点MessageBox迷津
- 正则问题 sourceString = rep.Replace(sourceString," $1=$2" + replaceURL + "$3$4"); 中的," $1=$2" + "$3$4怎么理解啊
- 有关正则表达式的问题
- c#绘制三维Mesh图,应该用什么控件
http://www.cnblogs.com/mokey/articles/2095457.html