Re: No return from async web service call

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



{\rtf1\ansi\ansicpg936\deff0\deflang1033\deflangfe2052{\fonttbl{\f0\fnil\fprq2\fcharset0 MS Sans Serif;}}
\viewkind4\uc1\pard\lang2052\f0\fs20 Hi Paul,
\par
\par Have you tried only using the CallBack handler, generally this is the most recommend means in desktop applicaiton since it won't block your main UI thread. Also, have you tried testing through a simple webmethod which will take long time to run? Based on my local test, I can correctly get both
\par
\par while(!ar.IsCompleted)
\par \{
\par \tab pbTime.Increment(5);
\par \tab System.Threading.Thread.Sleep(1000);
\par \tab\tab\tab\tab
\par \}
\par
\par or using Callback handler means to work correctly. The only problem is that when using while loop to poll the Complete status, the main UI thread will be blocked from accepting other UI message gracefully.
\par Also, I've noticed that you're wanting to update the processbar constatly during the long run webmethod processing, if so, I'm afraid CallBack hander won't quite meet your requirement. Thus, spawn a new work thread to do the work will be the best choice (if you won't spawn lots of such thread since spawning a new thread is much more expensive than utilize threadpool thread...). The code will be something like:
\par
\par ==================
\par private void btnCall_Click(object sender, System.EventArgs e)
\par \{\tab
\par \tab System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(CallWS_Proc));
\par \tab thread.Start();
\par \}
\par
\par
\par private void CallWS_Proc()
\par \{
\par \tab DocService.DocService ds = new WSClient.DocService.DocService();
\par \tab ds.Credentials = System.Net.CredentialCache.DefaultCredentials;
\par
\par \tab IAsyncResult ar = ds.BeginDummyUpload(null,null, ds);
\par
\par \tab while(!ar.IsCompleted)
\par \tab\{
\par //call function to update the UI
\par
\par \tab\tab pbTime.Increment(5);
\par \tab\tab System.Threading.Thread.Sleep(1000);
\par \tab\tab\tab\tab
\par \tab\}
\par
\par \tab string ret = ds.EndDummyUpload(ar);
\par \}
\par
\par
\par If there're anything else we can help, please feel free to post here. Thanks,
\par
\par Steven Cheng
\par Microsoft Online Support
\par
\par Get Secure! www.microsoft.com/security
\par (This posting is provided "AS IS", with no warranties, and confers no rights.)
\par \pard\li720 --------------------
\par Thread-Topic: No return from async web service call
\par thread-index: AcW89ZW8tjUj7CXhR5y2bwnyuA6mlQ==
\par X-WBNR-Posting-Host: 62.6.144.66
\par From: "=?Utf-8?B?UGF1bCBIYXNlbGw=?=" <paul_hasell@xxxxxxxxxxxxxxxx>
\par References: <8BD9AD06-E6D9-4D1D-8565-1BF1CB0527F1@xxxxxxxxxxxxx> <1126903555.326591.302910@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>
\par Subject: Re: No return from async web service call
\par Date: Mon, 19 Sep 2005 01:39:01 -0700
\par Lines: 80
\par Message-ID: <D72AF3D8-F2EB-4072-8145-AC9871709AE0@xxxxxxxxxxxxx>
\par MIME-Version: 1.0
\par Content-Type: text/plain;
\par \tab charset="Utf-8"
\par Content-Transfer-Encoding: 7bit
\par X-Newsreader: Microsoft CDO for Windows 2000
\par Content-Class: urn:content-classes:message
\par Importance: normal
\par Priority: normal
\par X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
\par Newsgroups: microsoft.public.dotnet.framework.webservices
\par NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
\par Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA03.phx.gbl
\par Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.framework.webservices:7932
\par X-Tomcat-NG: microsoft.public.dotnet.framework.webservices
\par
\par Peter,
\par
\par Thanks, that would explain a lot. The idea of the infinite loop is to try
\par and fake an upload progress bar so the user doesn't think it's frozen if the
\par upload takes a while (our users can get very twitchy). I had tried using just
\par the IsCompleted without a callback but that didn't seem to be getting set
\par either!? I may have to to cheat and use a synchronous call on the main thread
\par and spin the progress bar onto another thread instead, not elegant but at
\par least functional.
\par
\par "Peter K" wrote:
\par
\par > Hi Paul,
\par >
\par > I have to admit that my thread knowledge has gotten pretty rusty, but
\par > I think that even though your webservice will run on new thread, the
\par > thread your callback will run on is the same thread that your
\par > btnUpload_click method will be running on. Therefore UploadHandler
\par > won't be able to run until btnUpload_click method has completed. Since
\par > you have infinite loop running in there, UploadHandler will just keep
\par > waiting and won't fire.
\par >
\par > I managed to replicate the same behaviour you are experiencing using a
\par > continual while loop. When I removed the while loop, your code worked
\par > fine.
\par >
\par > Hope that helps
\par >
\par > Peter Kelcey
\par >
\par > Paul Hasell wrote:
\par > > Hi,
\par > >
\par > > I'm trying to invoke a web method asynchronously but just can't seem to get
\par > > it to tell me when it has finished! Below is the code I am (currently) using:
\par > >
\par > > private void btnUpload_Click(object sender, System.EventArgs e)
\par > > \{
\par > > try
\par > > \{
\par > > SOPWebService.Client uploader = new
\par > > GLTUpload.SOPWebService.Client();
\par > > uploader.Credentials =
\par > > System.Net.CredentialCache.DefaultCredentials;
\par > >
\par > > IAsyncResult async_upload =
\par > > uploader.BeginUploadDirect(txtKeyAccountTeam.SelectedValue.ToString(),
\par > > _xml_content, new AsyncCallback(UploadHandler), uploader);
\par > >
\par > > while(!async_upload.IsCompleted)
\par > > \{
\par > > Thread.Sleep(1000);
\par > > Progress.Increment(1);
\par > > \}
\par > > \}
\par > > catch(Exception ex)
\par > > \{
\par > > MessageBox.Show(ex.ToString(), Application.ProductName,
\par > > MessageBoxButtons.OK, MessageBoxIcon.Error);
\par > > \}
\par > > finally
\par > > \{
\par > > Close();
\par > > \}
\par > > \}
\par > >
\par > > private void UploadHandler(IAsyncResult async_result)
\par > > \{
\par > > SOPWebService.Client sop_web_client = (SOPWebService.Client)
\par > > async_result.AsyncState;
\par > > sop_web_client.EndUploadDirect(async_result);
\par > > \}
\par > >
\par > > The problem is that although the web method is invoked, executes and ends
\par > > the callback never gets fired and the IAsyncResult never has it's IsCompleted
\par > > set to true.
\par > >
\par > > Anyone know what I'm missing?
\par >
\par >
\par \pard
\par
\par }


















Relevant Pages

  • Re: design Q : using timer/threads
    ... Get rid of all loop variables. ... inside the callback itself. ... contains the data to be written, or posting a message to an I/O completion port to let a ... Then, using your buffer management structure, do ...
    (microsoft.public.vc.mfc)
  • Ctypes and C Infinite Callback Loops
    ... ctypes to hook into some legacy code. ... calls a specific callback function. ... this infinite loop, and continue on my merry way. ... "AssertionError: cannot join current thread". ...
    (comp.lang.python)
  • Re: Sleeping...
    ... use SetTimer to a window; do not use a callback function. ... I built a DLL which have a for loop. ... The loop needs to "sleep" X ...
    (microsoft.public.vc.mfc)
  • Re: Compleated Begginers Guide. Now What?
    ... my droog - you tried to rabbit ... > Ball Blaster i wanted to permenetly run the ball with a loop well my ... > main loop ran the bat. ... All TK-lewdies here will creech 'callback' at you sth. ...
    (comp.lang.python)
  • Re: VFS: file-max limit 50044 reached
    ... Thanks Paul for reminding us that call_rcushould not ever call the callback ... send the line "unsubscribe linux-kernel" in ...
    (Linux-Kernel)