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

ASP.net C# Return JS Alert from another thread

本文关键字:返回 JS 警报 线程 另一个 net ASP      更新时间:2023-09-26

我有以下情况:

默认.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);    
}

问题是我需要为最终用户显示此警报脚本。我假设它在另一个线程上运行并且没有显示。

您应该能够在长时间运行的函数结束时设置会话或应用程序变量,这表示该过程已完成。然后,您可以检查是否已在每次页面加载时设置此变量(理想情况下是在从Page继承的基类中),如果已设置,则取消设置并调用RegisterStartupScript代码。