2010-02-26 55 views
2

我面臨着多日的問題。我有一個線程正在偵聽來自其他系統的通知。每當我收到套接字通知時,我都想使用Infragistics的UltraDesktopAlert控件將其顯示在警報窗口中。這個類庫中沒有winform。如何從線程顯示Infragistics的UltraDesktopAlert

請告訴我如何使用線程顯示它。示例在這裏

void tcpServerListenter_OnCommReceive(object sender, CommEventArgs e) 
     { 
      string xmlString = Encoding.UTF8.GetString((byte[])e.data); 
      try 
      { 
       XDocument xmlDoc = XDocument.Parse(xmlString); 
       var res = (from msg in xmlDoc.Descendants("consoleNotification") 
          select msg.Element("value").Value).FirstOrDefault(); 

       this.notificationMsg = res; 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
       return; 
      } 

// here Alert window is not displaying. 
ultraDesktopAlert1.Show("Notification", this.notificationMsg);  
     } 

但此代碼不顯示警報窗口。

我檢查下面的鏈接也 How to update the GUI from another thread in C#?

,但問題是,我在我的類庫沒有Winform的。當我在套接字上收到通知時,我想要顯示警報窗口。

回答

0

因爲ultraDesktopAlert1 Infragistics對象是由主線程創建的,但是在不同的線程上訪問,所以您需要執行安全的跨線程。

看看這個MSDN Documentation如何做到這一點。

1

做到這一點,最簡單的方法是保存UI線程的同步上下文和發送拉姆達到UI線程...

在UI線程,當/啓動您的異步操作之前...

SynchronizationContext syncContext = SynchronizationContext.Current; 

將此保存到正在執行aSync工作的類中,然後您想要發送代碼以在保存的同步上下文中運行。

syncContext.Send((stateMsg) => ultraDesktopAlert1.Show("Notification", stateMsg), this.notificationMessage); 

請注意,如果你需要從UI線程檢索當前的SynchronizationContext,你不能從一個非UI線程的UI線程同步上下文。

相關問題