2012-04-12 48 views
0

我有以下情况:ASP.net C#返回JS警报从另一个线程

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

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

+0

长时间运行多久?如果时间不长,您可以发出ajax请求。 – 2012-04-12 14:26:13

+0

这是一个非常长的数据导出功能,需要38秒才能达到40,而用户并不需要等待它,他将数据导出到另一个数据库! – user1329310 2012-04-12 14:28:13

回答

0

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

+0

你好Lan, 其实这就是我迄今为止所做的。我已经分配了一个静态变量,并且在我测试的masterPage_load上,如果静态变量是“”或者有消息,然后再次将静态变量重置为“”。这只有在回发发生时才有效。在我的情况下,用户可能不会做任何可能导致回发的事情,并且我不能强制回发,因为我可能会中断用户操作。我想在主页上实现一个计时器,并在Tick上做静态消息的测试,但由于某种原因没有工作!任何想法 ? – user1329310 2012-04-13 05:14:10