c# - IAsyncresult - polling without freezing the UI? -


i've got windows svc running asynchronously (i've edited methods , parameters make them async), little like: http://msdn.microsoft.com/en-us/library/ms731177.aspx

however, call task want run asynchronously (the call service/server), , update ui (using reportprogress() on backgroundworker - of happening in dowork() method of backgroundworker). however, call endxxx method results, problem is, shouldn't code like?

while (!asyncresult.iscompleted) { // ui updating etc here... }  // call endxxx here. 

however, approach locks ui. @ moment, code (and doesn't lock ui):

 iasyncresult res = null;                  try                 {                      res = servicex.beginxxx(resultcallback, "");                 }                 catch (faultexception<managementexception> managementex)                 {                     logger.error(managementex.detail.tostring());                     messagebox.show("could not add printer. see log.");                 }                        installbackgoundworker.reportprogress(90);                     installbackgoundworker.reportprogress(91);                     installbackgoundworker.reportprogress(93);                      installbackgoundworker.reportprogress(94);                     installbackgoundworker.reportprogress(95);                     installbackgoundworker.reportprogress(96);                     installbackgoundworker.reportprogress(97);                        if (res.iscompleted)                     {                         resultcallback(res);                     }                     installbackgoundworker.reportprogress(100); 

is correct? seems wrong me.

i'm not you're using async pattern correctly. should this:

void start() {     system.io.stream s = ...;     byte[] buf = ...;      // start io.      s.beginread(buf, 0, buf.length, res =>         {             // gets called when it's finished,             // you're in different thread.              int len = s.endread(res);              // must call dispatcher.invoke             // ui thread.              dispatcher.invoke((action)delegate                 {                     // perform ui updates here.                 });         }, null);      // io started (and maybe finished) here,     // don't need put here. } 

written stream because don't know signature of object, idea! need process completion of operation in callback give it, not directly after call begin method. shouldn't need poll iscompleted property.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -