110,536
社区成员
发帖
与我相关
我的任务
分享
void OpenPort()
{
try
{
mdm.Open();
mdm.Ring += new EventHandler<AEventArgs>(mdm_Ring);
}
catch(Exception ee)
{
MessageBox.Show(ee.Message);
}
}
void mdm_Ring(object sender, AEventArgs e)
{
string phone = e.phoneNumber;
if (IsExitWindow()) //是否存在这个窗体
{
/*Thread th = new Thread(delegate()
{
Application.Run(new Form3());
});
th.Start();
代码1 用上这段代码的话数据传入Form3窗体不会卡,一个程序太多Application.Run(new Form3())的话
显得不好管理.
*/
new Form3().Show(); //代码2:用上这句,数据同样可以传入Form3但是窗体会卡住.
//有没有办法用代码2,又不让Form3卡啊.
//MessageBox.Show("电话号码为:"+phone);
Thread.Sleep(500);
if (ShowTel != null)
{
ShowTel(phone);
}
}
else
{
Thread.Sleep(500);
if (ShowTel != null)
{
ShowTel(phone);
}
}
}
public Form3()
{
InitializeComponent();
//Application.DoEvents();
MainFrm.ShowTel += new MainFrm.MyEventHandler(MainFrm_ShowTel);
}
void MainFrm_ShowTel(string Num)
{
SetListBox1(Num);
}
private delegate void SetListBoxCallback1(string str);
public void SetListBox1(string str)
{
//比较调用SetListBox方法的线程和创建listBox1的线程是否同一个线程
//如果不是,则listBox1的InvokeRequired为true
if (listBox1.InvokeRequired)
{
SetListBoxCallback1 d = new SetListBoxCallback1(SetListBox1);
listBox1.Invoke(d, str);
}
else
{
//结果为false,直接执行
listBox1.Items.Add(str);
listBox1.SelectedIndex = listBox1.Items.Count - 1;
listBox1.ClearSelected();
}
}
private void ThreadFunc()
{
MethodInvoker mi = new MethodInvoker(this.ShowForm);
this.BeginInvoke(mi);
}
private void ShowForm()
{
Form2 temp = new Form2();
temp.Show();
}
private void button2_Click(object sender, EventArgs e)
{
Thread th = new Thread(new ThreadStart(ThreadFunc));
// th.ApartmentState = ApartmentState.STA;
th.Start();
}