簡體   English   中英

在新線程中啟動表單並在它們之間引發事件

[英]Start form in new thread and Raise events between them

我嘗試在新線程中啟動表單(請參見下面的代碼架構)。 但是,窗體顯示后將關閉。

Thread te;    
dia di = new dia();
private static Thread te;
public static event AddingNewEventHandler tempchange;
public delegate void AddingNewEventHandler(int sender, EventArgs e);
static void Main(string[] args)
{    
   di.Coneig += new Config.AddingNewEventHandler(config);
   te = new Thread(new ThreadStart(di.Show));     
   te.Start();    
   while(true)
   {
     //async code to Form
   }
}

public static void config(int[] sender, EventArgs e)
{
    //Edit some values in the main(Class variables)
}
Thread te;    
dia di = new dia();

static void Main(string[] args)
{    
   te = new Thread(new ThreadStart(di.Show));     
   te.Start();    
   Console.ReadKey();
}

編輯:

我檢查過這個作品。

    static void Main(string[] args)
    {
       Form di = new Form();


        Thread te = new Thread(() => 
        {
            Application.EnableVisualStyles();
            Application.Run(di);
        });
        te.Start();
    }

嘗試這個:

te = new Thread(new ThreadStart(()=>di.ShowDialog()));

我不確定它的用途,但應該可以。

線程將保持活動狀態,直到您關閉表單。

您可以使用

te.Join();

作為Main結束之前的最后一行。 這應該使它工作。

關於它應該如何的更長的解釋

當您啟動Windows窗體應用程序時,您將啟動GUI /事件/主線程。 可視化一個無限的while循環,就像下面在winforms庫中的某個地方

Queue<EventArgs> EventQueue = new Queue<EventArgs>();
while (!StopRequested) {
    if (EventQueue.Any())
        ProcessEvent(EventQueue.Dequeue());
    Thread.CurrentThread.Sleep(100);
}

在該線程中創建所有控件,並在該線程中觸發所有事件。 現在,由於此代碼不存在於您的代碼中,因此獲得GUI / Event相關服務的唯一方法是將其發布到此EventQueue

現在有了這種背景,讓我們來處理您的情況。

引發和處理事件

引發和處理事件對一個表單/線程的工作方式完全相同,就像對多個表單/線程的工作方式一樣。 記住,引發事件只是將事件發布到該EventQueue ,而事件處理程序的調用是在ProcessEvents內部進行的

形成生命周期

在典型的WinForms應用程序中,您將使用下面的一行來告訴系統創建EventQueue並將其生命周期與表單的生命周期相關聯。

Application.Run(new Form());

您可以使用

new Form().Show()

開始將事件泵入EventQueue ,從而顯示表單,但是請記住,一旦您的應用程序的主線程到達Main的末尾, EventLoop就會突然終止。

因此,關閉此表單將導致您的應用程序停止,反之亦然。

你的情況

我強烈建議您從主線程啟動Form,然后在新線程上簡單地完成其他工作。 我看到您將sender用作intint[] ,這是一個問題。 如果需要達到相同的目標,下面是我的寫法。 只要有可能,我都會盡量使其與您的樣本相似

class Program {
    static Form1 frm;

    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        frm = new Form1();
        frm.Test += TestEventHandler;

        new Thread(new ThreadStart(DoAsyncProcessing)).Start();

        Application.Run(frm);
    }

    static void DoAsyncProcessing() {
        // async code goes here
        Thread.Sleep(5000);

        // raise the event
        frm.RaiseTestEvent("Test Event Data");
    }

    static void TestEventHandler(object sender, TestEventArgs e) {
        System.Diagnostics.Debug.WriteLine("Received test event with data: " 
            + e.EventData);
    }
}

public partial class Form1 : Form {
    public event EventHandler<TestEventArgs> Test;

    public Form1() {
        InitializeComponent();
    }

    public void RaiseTestEvent(string eventData) {
        var arg = new TestEventArgs() { EventData = eventData };
        OnTest(arg);
    }

    protected virtual void OnTest(TestEventArgs e) {
        EventHandler<TestEventArgs> handler = Test;
        if (handler != null)
            handler(this, e);
    }
}

public class TestEventArgs : EventArgs {
    public object EventData { get; set; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM