Solved

Background Worker and returning values

Posted on 2006-11-09
5
472 Views
Last Modified: 2012-05-05
Hello all. Morning question...
I have the main thread running my main form. I use a backgroundworker when user click button to use a webservice that controls european VAT. my code is:

//...
MBForm.Showdialog() // form whre is shown the progress of the webservice running on the Background Thread
Backgroundworker.RunWorkerASynch();

//... BackGroundWorker_Dowork()
Webservice.run()

All that I need is: How can I have retuning values when the background worker has finished the work? I mean the Webservice is controlling the european VAT returning bool valid and string name and string address. How can I have this three returning values for using it on the main Thread? Do I have to assingn these values on a global variable of is there a better way? Thx in advance
0
Comment
Question by:puckkk
  • 3
  • 2
5 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 299 total points
ID: 17905249
You can pass result of background operation using DoWorkEventArgs.Result property.

Backgroundworker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Backgroundworker_RunWorkerCompleted);

void BackGroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
    // your existing code

    // Make result
    OperationResult r = new Result();   // OperationResult  is your class which contains results of background operation
    r.Name = ...;
    r.Address = ...;     // fill result properties
    e.Result = r;
}

void Backgroundworker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
     if ( e.Error != null )
     {
         // report error
     }
     else if ( e.Cancelled )
     {
          // report cancellation
     }
     else
     {
         OperationResult r = (OperationResult)e.Result;
         // get result here
     }
}
 
0
 

Author Comment

by:puckkk
ID: 17905283
Nice. Thx a lot agian man.
0
 

Author Comment

by:puckkk
ID: 17912268
Sorry Alex, just one more thing...
   OperationResult r = new Result();   // OperationResult  is your class which contains results of background operation
 r.Name = ...;
    r.Address = ...;     // fill result properties

What is the best way to create OperationResult class?

public class Operation Result
{
        string name;
        string address
        OperationResult(string _name, string address)
        {
             name=_name;
             address = _address;

        }
}

Or is there a better way to create an object assigning to properties to it? Thx again
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17912525
Any way that is convenient for you: class with constructor and read-only properties, class with read/write properties. Anything that allows to keep information and read it. As example of such clas, which is designed only for keeping parameters, see ProcessStartInfo class.
0
 

Author Comment

by:puckkk
ID: 17912532
Thx a lot.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now