winform程序 我用一个timer控件 每200MS执行一次 用来获取数据 当获取到数据的时候 会弹出新窗体 但是弹出的新窗体会卡死
在不停用该timer控件的情况下 怎么优化其性能 (试过用线程 但好像也有这样的问题 不知道是不是我线程没写好) c#winform线程优化
在不停用该timer控件的情况下 怎么优化其性能 (试过用线程 但好像也有这样的问题 不知道是不是我线程没写好) c#winform线程优化
private void ShowMsg(string msg)
{
if (this.InvokeRequired)
{
Action<string, bool> at = ShowMsg;
this.Invoke(at, msg);
}
else
{
//显示窗口代码
new FormMsg(msg).Show();
}
}
{
if (this.InvokeRequired)
{
Action<string> at = ShowMsg;
this.Invoke(at, msg);
}
else
{
//显示窗口代码
new FormMsg(msg).Show();
}
}
{ 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;
}
放到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);
}
}
只是想不通你为什么要这么设计呢?