简体   繁体   English

ASP.net C#从另一个线程返回JS警报

[英]ASP.net C# Return JS Alert from another thread

I have the following case: 我有以下情况:

in Default.aspx.cs 在Default.aspx.cs中

void Button1_Click(Object Sender, EventArgs e)
{
  new Thread(() =>LongProcessFunction()).Start(); 
  // I am running this function in a new thread so the end user does not wait till the end of the process
}

public void LongProcessFunction()
{
  //some code with long process
  // When done alert the user no matter what he's doing on the website.
  System.Web.UI.ScriptManager.RegisterStartupScript(this, GetType(), "Alert","alert('Function is Complete')" , true);    
}

The problem is that I need this alert script to show for end user. 问题是我需要此警报脚本才能显示给最终用户。 I am assuming that it's running on the other thread and not showing. 我假设它正在其他线程上运行并且没有显示。

You should be able to set a session or application variable at the end of your long running function, which indicates that the process has finished. 您应该能够在长时间运行的函数的末尾设置会话或应用程序变量,这表明该过程已完成。 You could then check if this variable has been set on each page load (ideally in a base class which inherits from Page) and, if it has been set, unset it and call your RegisterStartupScript code. 然后,您可以检查是否在每次页面加载时都设置了此变量(最好是在继承自Page的基类中),如果已设置,请取消设置它并调用RegisterStartupScript代码。

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

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