简体   繁体   English

使用引发事件的线程中的消息更新屏幕(ASP.NET)

[英]Updating screen with messages from a thread that raises events (ASP.NET)

I have a very long running process that I start in a thread. 我有一个很长的运行过程,我从一个线程开始。 This raises events with messages to say what it is doing. 这会引发带有消息的事件,说明消息正在做什么。

How can I update a label on screen with these messages? 如何使用这些消息更新屏幕上的标签? (The label is in an update panel if that makes it easier). (如果这样更容易,标签将显示在更新面板中)。

If this is not possible, is there a better way you can suggest? 如果这不可能,那么有没有更好的建议呢?

Once the page request has completed the server has no explicit knowledge of, or connection to, the requesting browser so you are unable to easily push a response to the user. 一旦完成页面请求,服务器就不会对请求的浏览器有任何明显的了解或连接,因此您将无法轻松地向用户推送响应。 The easiest way would be to persist the messages in some way using an identifier and then expose the messages via a service or CallBack function, you could use a Ajax timer to make calls to the service and update the UI accordingly. 最简单的方法是使用标识符以某种方式保留消息,然后通过服务或CallBack函数公开消息,您可以使用Ajax计时器来调用服务并相应地更新UI。

Yes, you can. 是的你可以。 Create a method that will have a parameter (message string to be shown). 创建一个具有参数的方法(要显示的消息字符串)。 Like: 喜欢:

private void SetLabelText(string message)
{
            if (label.InvokeRequired)
            {
                Invoke(new MethodInvoker(() => SetLabelText(message)
                           ));
            }
            else
            {
                label.Text = message;
            }
}

Use this method in your event handler. 在事件处理程序中使用此方法。

使用背景工,更新UI 链接文本非常有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM