跨线程创建窗体的两种方法

1、利用新STA线程进行创建

//线程
private System.Threading.Thread th=null;

//创建线程
try
{
    th = new System.Threading.Thread((System.Threading.ThreadStart)delegate
    {
        Application.Run(new fmFreeChatClient(url));
    });

    th.SetApartmentState(ApartmentState.STA);
    th.Start();
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

2、用delegate

//在form中声明
#region 新建Form并显示
delegate void showForm(string url);
public void showMyForm(string url)
{
//新建窗体并显示
}
#endregion

//在线程中调用
Object[] objs = new Object[1];
objs[0]="hello";
this.Invoke(new showForm(showMyForm),objs);

Leave a Reply

Your email address will not be published. Required fields are marked *

*