![](/img/trans.png)
[英]Creating a new, temporary Form on a separate thread from the main form using C# and WinForms
[英]C# New Form Creating In A Thread
我有一個TCP服務器應用程序,並且有一個用於與TCP客戶端通信的線程。 當我從客戶端收到數據時,我想使用此數據創建新表單,但無法在線程中創建表單。 我可以使用按鈕單擊事件輕松地創建表單。
我哪里錯了?
謝謝。
執行此工作的示例代碼:
private void Button1_Click(object sender, EventArgs e)
{
Thread t1 = new Thread(StartMe);
t1.Name = "Custom Thread";
t1.IsBackground = true;
t1.Start();
}
private void StartMe()
{
//We are switching to main UI thread.
TextBox1.Invoke(new InvokeDelegate(InvokeMethod), null);
}
public void InvokeMethod()
{
//This function will be on main thread if called by Control.Invoke/Control.BeginInvoke
MyForm frm = new MyForm();
frm.Show();
}
您必須在某處將上下文更改為GUI線程以創建新表單-在某處,您將需要BeginInvoke()。
這是哪種服務器-是“經典”同步服務器,其中每個客戶端連接都有一個偵聽線程和一個server <> client線程?
您不想在客戶端連接時創建表單,僅在連接的客戶端明確詢問時才創建此表單,是嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.