Question

I've got a windows svc which is running asynchronously (I've edited the methods and their parameters to make them async), a little like: http://msdn.microsoft.com/en-us/library/ms731177.aspx

However, I call the task which I want to run asynchronously (the call to the service/server), and then update the UI (using ReportProgress() on the backgroundworker - all of this is happening in the dowork() method of a backgroundworker). However, I call the Endxxx method to get the results, but the problem is, shouldn't my code look like?

while (!asyncresult.IsCompleted) { // Do all UI updating etc here... }

// Call endXXX here.

However, this approach locks the UI. At the moment, my code is like so (and doesn't lock the 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 this correct? It seems wrong to me.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top