Solved

Background Worker and returning values

Posted on 2006-11-09
5
474 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

840 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