views:

217

answers:

2

hi. i am facing problem from many days. I have a thread listening for notificaitons from other system. whenever i receive a notification over socket i want to display it in Alert window using Infragistics's UltraDesktopAlert control. there is no winform in this class library.

please tell me how to display it using a thread. a sample is here

  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);      
        }

but this code is not displaying alert window.

i checked the following link also http://stackoverflow.com/questions/661561/how-to-update-gui-from-another-thread-in-c

but problem is that i have no Winform in my class library. what i wants to display alert window whenever i receive notification on socket.

+2  A: 

You need to do safe cross threading since the ultraDesktopAlert1 Infragistics object was created by the main thread but being accessed on a different thread.

Check out this MSDN Documentation on how to do it.

Jojo Sardez
A: 

The easiest way to do this is save the UI thread's synchronization context and Send a lambda over to the UI thread...

On the UI Thread, when/before initiating your Async operation...

SynchronizationContext syncContext = SynchronizationContext.Current;

Save this into the class that is doing the aSync work, then you want to Send your code to run on the saved synchronization context.

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

Note that if you need to retrieve the Current SynchronizationContext from the UI thread, you can't get the UI threads synchronization context from a non UI thread.

Mark Allanson