user2783193
user2783193

Reputation: 1012

consume wcf service on separate thread

I have win form which represents simple wcf client app. This client consumes wcf service over http.

Inside form there is loadingLabel.Text property where I want to display loading ... text. When wcf service returns data other property labelAllBooksNr.Text should be populated.

Service will return integer in allBooksNumber property.

 private void Form1_Load(object sender, EventArgs e)
 {
    int allBooksNumber = BookAgent.CountAllBooks();        
 }

Since I do not have any experience in using threads I'm asking to someone provide the best pattern I should follow.

Upvotes: 0

Views: 193

Answers (1)

BRAHIM Kamel
BRAHIM Kamel

Reputation: 13755

the best pattern you can use is the BackgroundWorker as Executes an operation on a separate thread and offers many methods

from MSDN

When you want a responsive UI and you are faced with long delays associated with such operations, the BackgroundWorker class provides a convenient solution.

 namespace WindowsFormsApplication1

  {
   public partial class Form1 : Form
   {
      public Form1()
    {
        InitializeComponent();           
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        int allBooksNumber = BookAgent.CountAllBooks();
        e.Result = allBooksNumber;  
    }



    private void button1_Click(object sender, EventArgs e)
    {
         backgroundWorker1.RunWorkerAsync();
         label1.Text = "Loading....";
    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        label1.Text = e.Result.ToString;  
    }
}

}'

Hope this help

Upvotes: 1

Related Questions