Click here to Skip to main content
15,878,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai,

I am using the following code

C#
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);
        }


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
Posted
Updated 1-Dec-11 2:20am
v4
Comments
[no name] 1-Dec-11 8:21am    
EDIT: added "pre" and "code" tags
Sergey Alexandrovich Kryukov 1-Dec-11 10:45am    
First of all, why calling Sleep? Makes no sense.
--SA

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.
 
Share this answer
 
Comments
Member 7684075 1-Dec-11 8:27am    
Tnks Paulo. So how to modify the code?

Assume i am not put any thread.slepp(). But i am putting breakpoint. When compiler is coming I am not pressed F10 for next step. I am press F10 after sometime. Such that situation also alert message is not happening. Suppose if i remove the break point and run it, it will run perfectly. So i think here time is important? and i really confused.
Paulo Zemek 1-Dec-11 8:30am    
You can't use parallel code to do that... well... at least not parallel code that can end after the actual ASP.Net method.
So, if you are in Page_Load, you can generate any async event, as long as the Page_Load waits for the end of such parallel delegate. (and if both affect the response, then you will need locks).
But considering that web-servers usually already use all their cpus when there are enough clients, I think you should not use BeginInvoke. Simple invoke the delegate normally.
Member 7684075 1-Dec-11 8:36am    
Ok. Suppose i am using loop condition instead of thread.sleep(7000). If the loop condition taking a time to finish that situation also i am not get alert message. So what i should do exactly? Please give me some idea.
Paulo Zemek 1-Dec-11 8:41am    
Why do you want to use BeginInvoke?
Why not simple call the delegate directly?
Your problem is:
Thread A:
Page_Load (for example) starts a second thread with your delegate.
Page_Load continues while the other thread continues.
Page_Load finishes, the response is sent... and the other thread continues.
Doing anything on the other thread now is useless.

So... do it synchronously, call the delegate normally, not as BeginInvoke.
Member 7684075 1-Dec-11 8:48am    
Actually my problem is After successfully login i should show alert message after 25mins. At the same time i should not use thread. Thats why i am using thread.begininvoke(). Because here the page is working fine even if i click another button also. This is working parellay. I hope this thread.sleep will get resume after 25 minutes then i can show alert message. That is the reason i am trying like this.
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
 
Share this answer
 
Comments
Member 7684075 1-Dec-11 8:41am    
Thanks MRB. So how to join with main thread after pausing the asynchrnous threads. I dont have idea to joining the subthread to main thread. Give me idea please.
Manfred Rudolf Bihy 1-Dec-11 9:29am    
I saw from the thread above with Paulo Zemek what you are trying to achieve. For the reasons I already gave in my solution using Threading in the way you intended will not work. You should use Ajax instead polling the server from javascript code. When the server signals a certain condition in its response to your Ajax call you can display an alert dialog or whatever you see fits your purpose.

Cheers!
Member 7684075 5-Dec-11 2:16am    
I dont want use javascript also Manfred. how to do? I hope we can acheive this one by using delegate and event. So have u any idea?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900