如何从线程显示 Infragistics 的 UltraDesktopAlert

发布于 2024-08-22 15:25:32 字数 1158 浏览 8 评论 0原文

我已经面临很多天的问题了。我有一个线程正在监听来自其他系统的通知。每当我通过套接字收到通知时,我想使用 Infragistics 的 UltraDesktopAlert 控件将其显示在警报窗口中。这个类库中没有winform。

请告诉我如何使用线程显示它。示例在这里

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

,但此代码不显示警报窗口。

我还检查了以下链接 如何从 C# 中的另一个线程更新 GUI?

但问题是我的类库中没有 Winform。每当我在套接字上收到通知时,我想显示警报窗口。

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
How to update the 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清眉祭 2024-08-29 15:25:32

最简单的方法是保存 UI 线程的同步上下文并将 lambda 发送到 UI 线程...

在 UI 线程上,启动异步操作时/之前...

SynchronizationContext syncContext = SynchronizationContext.Current;

将其保存到正在执行 aSync 的类中工作,然后您想要发送代码以在保存的同步上下文上运行。

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

请注意,如果您需要从 UI 线程检索 Current SynchronizationContext,则无法从非 UI 线程获取 UI 线程同步上下文。

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.

聆听风音 2024-08-29 15:25:32

您需要执行安全的跨线程,因为 ultraDesktopAlert1 Infragistics 对象是由主线程创建的,但在不同的线程上访问。

请查看此 MSDN 文档,了解如何执行此操作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文