简体   繁体   中英

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.

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 链接文本非常有用

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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