Solved

Background Worker and returning values

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

22 Experts available now in Live!

Get 1:1 Help Now