Solved

Background Worker and returning values

Posted on 2006-11-09
5
475 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

733 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