2

我已经搜索了几天,并尝试了我能找到的所有解决方案。我知道这是我做的不对,但是,我不确定正确的方法是什么。

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

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

这是我开始报告的代码...

字符串 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;
    }
}

这是回调代码的代码......

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 触发?有没有办法强制它在主线程上执行?

4

2 回答 2

1

它不会在 JS 中触发,因为您正在分离一个新线程。同时,请求早已返回给客户端并关闭了连接。当线程尝试将某些内容写入响应时,它已经完成了。

而不是这样做,只需在UpdatePanel. 然后,您无需启动新线程。

于 2013-03-06T16:43:33.123 回答
0
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.
于 2013-03-07T01:20:42.683 回答