110,536
社区成员
发帖
与我相关
我的任务
分享
timer过于频繁,主线程又会占着,你就改成Thread线程去处理 放到Load事件启动 Thread _th=new Thread(new ThreadStart(Helper)); _th.Start(); void Helper() { while(!isBreak) { Phone ph = new Phone(); AddPhone(ph); if (first == 0) { GetPhone_RunningEvent(null); first++; } Thread.Sleep(200);//每200MS一次 } } public void AddPhone(Phone ph) { try { StringBuilder str = new StringBuilder(); int count = LClient.LClientRcv(str); if (!string.IsNullOrWhiteSpace(str.ToString())) { ph = PhoneCon(str.ToString()); if (ph.Number != null) { waitPhone.Add(ph); //新窗口,直接弹出不用委托 // this.BeginInvoke(new BeginInvokeCreateForm(GetPhone_RunningEvent)); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
如果没有应用到其他主线程东西,你就在线程直接new Form().Show()也是可以的
你的程序每两百毫秒会弹出一个窗体,也就是说不停的弹出,你说会卡死吗、?搞不懂你为什么这样做
文字表达不好的,直接帖出代码,这是最好解决问题的办法
private void timer1_Tick(object sender, EventArgs e)
{
Phone ph = new Phone();
AddPhone(ph);
if (first == 0)
{
GetPhone_RunningEvent(null);
first++;
}
}
public void AddPhone(Phone ph)
{
try
{
StringBuilder str = new StringBuilder();
int count = LClient.LClientRcv(str);
if (!string.IsNullOrWhiteSpace(str.ToString()))
{
ph = PhoneCon(str.ToString());
if (ph.Number != null)
{
waitPhone.Add(ph);
this.BeginInvoke(new BeginInvokeCreateForm(GetPhone_RunningEvent));
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void GetPhone_RunningEvent(object sender)
{
if (Jump.Ph == null)
{
if (waitPhone.Count > 0)
{
if (string.IsNullOrEmpty(waitPhone[0].Error))
{
Jump.Ph = waitPhone[0]; //打开新窗体 该窗体关闭时触发GetPhone_RunningEvent事件
Jump.OpenJp().Show();
waitPhone.RemoveAt(0);
}
else
{
waitPhone.RemoveAt(0);
GetPhone_RunningEvent(sender);
}
}
}
RunningEvent -= GetPhone_RunningEvent;
}
private void ShowMsg(string msg) { if (this.InvokeRequired) { Action<string, bool> at = ShowMsg; this.Invoke(at, msg); } else { //显示窗口代码 new FormMsg(msg).Show(); } }
private void ShowMsg(string msg)
{
if (this.InvokeRequired)
{
Action<string> at = ShowMsg;
this.Invoke(at, msg);
}
else
{
//显示窗口代码
new FormMsg(msg).Show();
}
}
private void ShowMsg(string msg)
{
if (this.InvokeRequired)
{
Action<string, bool> at = ShowMsg;
this.Invoke(at, msg);
}
else
{
//显示窗口代码
new FormMsg(msg).Show();
}
}