ASP.Net ThreadPool委托回调——JavaScript不会在回调线程上触发

ASP.Net ThreadPool Delegate Callback -- JavaScript Not Firing On Callback Thread

本文关键字:回调 线程 JavaScript ThreadPool Net ASP      更新时间:2023-09-26

我已经找了好几天了,我已经尝试了我能找到的每一个解决方案。我知道这是我做得不对的事情,但是,我不确定正确的方法是什么。

我有ASP。.Net c#网站,运行在。Net Framework 4.5上。我在表单上有一个链接按钮,当点击时,使用ThreadPool触发一个长时间运行的进程。我有一个委托回调设置,当进程被取消或完成时,代码会触发。(我正在使用取消令牌来取消进程,如果重要的话,该进程是活动报告。)

就像我说的,一切都很好,除了当回调代码触发它不执行javascript。(仅供参考——这不是javascript回调,只是试图在进程结束时触发一些javascript代码。)

这是我开始报告的代码…

string sThreadID = Thread.CurrentThread.ManagedThreadId.ToString();ThreadPool。QueueUserWorkItem(new WaitCallback(StartReport), cts.Token);

下面是StartReport....的代码
public static void StartReport(object obj) {
    try {
        OnTaskCompleteDelegate callback = new OnTaskCompleteDelegate(OnTaskComplete);
        BoyceReporting.CallReport(boyce.BoyceThreadingEnvironment.OBRO, "THREADING");
        if (boyce.BoyceThreadingEnvironment.CTS.Token.IsCancellationRequested) {
            boyce.BoyceThreadingEnvironment.SESSION.sScriptToExecute = "alert('Report Canceled By User');";
            callback("CANCELED");
        } else {
            callback("FINISHED");
        }
    } catch {
        throw;
    }
}

下面是CallBack代码.....

public static void OnTaskComplete(string ReportResult) {
    try {
        sReportResult = ReportResult;
        if (ReportResult == "CANCELED") {
            //  In case we need to do additional things if the report is canceled
        }
        string sThreadID = Thread.CurrentThread.ManagedThreadId.ToString();
        boyce.BoyceThreadingEnvironment.THISPAGE.ClientScript.RegisterStartupScript(boyce.BoyceThreadingEnvironment.THISPAGE.GetType(), "FireTheScript" + DateTime.Now.ToString(), boyce.BoyceThreadingEnvironment.SESSION.sScriptToExecute, true);
        ScriptManager.RegisterStartupScript(boyce.BoyceThreadingEnvironment.THISPAGE, boyce.BoyceThreadingEnvironment.THISPAGE.GetType(), "DisplayReport" + DateTime.Now.ToString(), boyce.BoyceThreadingEnvironment.SESSION.sScriptToExecute, true);
    } catch {
        throw;
    }
}

这里是问题,我有.....

一切都很好,除了我不能得到最后一行代码来触发脚本。ScriptManager。RegisterStartupScript

这就是我认为发生的事情.....

从查看线程ID,我确信代码没有触发的原因是因为我试图在回调事件中触发的ScriptManager代码在不同的线程上,而不是主线程。

这是我的问题.....

(1)我是正确的,为什么这是不触发JavaScript(2)我怎么能(从内部回调)得到这个JavaScript火灾?是否有一种方法来强制这在主线程上执行?

它不会在JS中触发,因为你正在旋转一个新线程。与此同时,请求早已返回到客户端并关闭了连接。当线程尝试向响应写入一些内容时,它已经完成了。

而不是这样做,只是有你的按钮点击(或任何它是启动报告),在UpdatePanel内。这样,就不需要启动新线程了。

Here is the cod I used in the C# Code Behind to call the web service to start monitoring this process.
----------------------------------------------------------------------------------------------
                CurrentSession.bIsReportRunning = true;
                ScriptManager.RegisterStartupScript(this, this.GetType(), "WaitForReport" + DateTime.Now.ToString(), "jsWaitOnCallReport();", true);
                MultiThreadReport.RunTheReport(HttpContext.Current, CurrentSession, this, oBRO);
Here is the code that calls the method, using the threadpool, and the method called..
----------------------------------------------------------------------------------------------
            ThreadPool.QueueUserWorkItem(new WaitCallback(StartReport), cts.Token);
    public static void StartReport(object obj) {
        try {
            OnTaskCompleteDelegate callback = new OnTaskCompleteDelegate(OnTaskComplete);
            BoyceReporting.CallReport(boyce.BoyceThreadingEnvironment.OBRO, "THREADING");
            HttpContext.Current = boyce.BoyceThreadingEnvironment.CONTEXT;
            if (boyce.BoyceThreadingEnvironment.CTS.Token.IsCancellationRequested) {
                boyce.BoyceThreadingEnvironment.SESSION.sScriptToExecute = "alert('Report Canceled By User');";
                boyce.BoyceThreadingEnvironment.SESSION.bIsReportRunning = false;
                callback("CANCELED");
            } else {
                boyce.BoyceThreadingEnvironment.SESSION.bIsReportRunning = false;
                callback("FINISHED");
            }
        } catch {
            throw;
        }
    }
Here is the web service method I created to monitor the process, with a built in safety net
--------------------------------------------------------------------------------------------
    [WebMethod(EnableSession = true)]
    public string WaitOnReport() {
        try {
            HttpContext.Current = boyce.BoyceThreadingEnvironment.CONTEXT;
            SessionManager CurrentSession;
            CurrentSession = (SessionManager)boyce.BoyceThreadingEnvironment.SESSION;
            DateTime dtStartTime = DateTime.Now;
            DateTime dtCurrentTime = DateTime.Now;
            if (CurrentSession != null) {
                do {
                    //  Build a safety limit into this loop to avoid an infinate loope
                    //  If this runs longer than 20 minutes, then force an error due to timeout
                    //  This timeout should be lowered when they find out what the issue is with 
                    //  the "long running reports".  For now, I set it to 20 minutes but shoud be MUCH lower.
                    dtCurrentTime = DateTime.Now;
                    TimeSpan span = dtCurrentTime-dtStartTime;
                    double totalMinutes = span.TotalMinutes;
                    if (totalMinutes>=20) {
                        return "alert('Error In Creating Report (Time-Out)');";
                    }
                } while (CurrentSession.bIsReportRunning == true);
                //  If all goes well, return the script to either OPEN the report or display CANCEL message
                return CurrentSession.sScriptToExecute;
            } else {
                return "alert('Error In Creating Report (Session)');";
            }
        } catch {
            throw;
        }
    }

And here is the JavaScript code I used to initiate the Web Service Call and Also The Postback
--------------------------------------------------------------------------------------------
function jsWaitOnCallReport() {
    try {
        var oWebService = BoyceWebService.WaitOnReport(jsWaitOnCallReport_CallBack);
    } catch (e) {
        alert('Error In Calling Report Screen -- ' + e);
    }
}
function jsWaitOnCallReport_CallBack(result) {
    try {
        eval(result); 
        var myExtender = $find('ModalPopupExtenderPROGRESS');
        if (myExtender != null) {
            try {
                myExtender.hide();
            } catch (e) {
                //  Ignore Any Error That May Be Thrown Here
            }
        }
        $find('PROGRESS').hide();
    } catch (e) {
        alert('Error In Opening Report Screen -- ' + e);
    }
}
Hope this helps someone else out..  Like I said, I am not sure this is the best solution, but it works..   I would be interested in other solutions for this issue to try...  Thanks.