Thread.Sleep()之后,Javascript无法正常工作 [英] Javascript is not working after Thread.Sleep()

查看:104
本文介绍了Thread.Sleep()之后,Javascript无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

海,

我正在使用以下代码

Hai,

I am using the following code

delegate d=new delegate(DeleMethod);
delegate.BeginInvoke(new AsyncCallback(WorkCompleted), null););

protected void DeleMethod()
        {
            ClientScript.RegisterStartupScript(this.GetType(), "Sample", "javascript:msg('before executing delegate method');", true);
            Thread.Sleep(7000);
            ClientScript.RegisterStartupScript(this.GetType(), "Sample", "javascript:msg('after executing delegate method');", true);
           }

  protected void WorkCompleted(IAsyncResult result)
        {
            DelGetBack.EndInvoke(result);
            result.AsyncWaitHandle.Close();
            ClientScript.RegisterStartupScript(this.GetType(), "Sample", "javascript:msg('waittttt');", true);
            DelGetBack = null;
            //DelGetBack.EndInvoke(result);
        }



Thread.Sleep javascript警报消息之前,我正在吸气. Thread.Sleep之后,我没有收到JavaScript警报消息.为什么?我应该改变什么?请帮我.即使在EndInvoke方法中也不起作用.

谢谢



Before Thread.Sleep javascript alert message i am gettting. After Thread.Sleep i am not getting the javascript alert message. Why? What i should to change? please help me. Even in EndInvoke method also not working.

Thanks

推荐答案

我认为您误解了服务器/客户端的工作方式.
如果您注册一个脚本,然后等待,然后再注册另一个脚本,则客户端只会在最后收到整个页面,并立即执行这两个脚本.
您正在做的是:
响应开始时,您将运行一个异步委托.
那个异步委托正在另一个线程发送响应之前注册一个脚本(但这可能不会发生……您可能会在其中引起很多线程问题)……然后,等待7秒时,响应被发送并关闭...并且您的注册是无用的(而且有错误),因为响应已由另一个线程发送.
I think you are misunderstanding how server/client works.
If you register a script, then wait, then register another script, the client will only receive the entire page at the end, executing both scripts immediatelly.
What you are doing is:
When the response begins, you run an async delegate.
That async delegate is registering a script before the other thread sends the response (but that may not happen... you can cause a lot of threading problems there)... then, when waiting for 7 seconds, the response is sent and closed... and your registration is useless (and buggy) as the response was already sent by another thread.


您应该更改什么?首先,您真的需要了解这里幕后发生的事情.在调用ClientScript.RegisterStartupScript的第一行之后的DeleMethod中,您正在调用Thread.Sleep,这将暂停在异步BeginInvoke调用中运行的代码.由于我看不到任何将异步线程与主线程连接的同步措施,因此该主线程早已完成运行,并且浏览器已接收到由aspx页面呈现的HTML.
在所述渲染期间,输出JavaScript代码,该JavaScript代码先前是通过调用ClientScript.RegisterStartupScript注册的.调用此函数无非是确保将指定的javascript代码以某种方式插入呈现的HTML中,以便一旦将页面加载到浏览器中便会执行该代码.
呈现页面并将输出返回到浏览器后,对ClientScript.RegisterStartupScript的附加调用就毫无意义了.实际上,如果这在幕后造成了某种破坏,我不会感到惊讶,因为结果已经返回到浏览器,因此您不会有任何破坏的想法.

问候,

—MRB
What should you change? First of all you really need to understand what is going on behind the scenes here. In your DeleMethod right after the first line with the call to ClientScript.RegisterStartupScript you are calling Thread.Sleep which will pause the code running in the asynchrounous BeginInvoke call. Since I don''t see any synchronization measures that will join the main thread with asynchronous threads, the main thread will long have finished running and the HTML rendered by your aspx page is already received by the browser.
During said rendering javascript code is output which was previously registered with a call to ClientScript.RegisterStartupScript. Calling this function does nothing more than make sure that the specified javascript code is inserted into the rendered HTML in such a fashion that it will be executed once the page has loaded in the browser.
Once the page is rendered and the output been returned to the browser there is no sense in makin additional calls to ClientScript.RegisterStartupScript. In fact I wouldn''t be surprised if this wreaked some sort of havoc behind the scenes, none of which you will have any notion of as the result was already returned to the browser.

Regards,

—MRB


这篇关于Thread.Sleep()之后,Javascript无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆