简体   繁体   English

在单独的线程上消耗wcf服务

[英]consume wcf service on separate thread

I have win form which represents simple wcf client app. 我有代表简单的wcf客户应用程序的获胜形式。 This client consumes wcf service over http. 该客户端通过http使用wcf服务。

Inside form there is loadingLabel.Text property where I want to display loading ... text. 表单内部有loadingLabel.Text属性,我想在其中显示loading ...文本。 When wcf service returns data other property labelAllBooksNr.Text should be populated. 当wcf服务返回数据时,应填充其他属性labelAllBooksNr.Text

Service will return integer in allBooksNumber property. 服务将在allBooksNumber属性中返回整数。

 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. 由于我没有使用线程的经验,因此我要求某人提供我应该遵循的最佳模式。

the best pattern you can use is the BackgroundWorker as Executes an operation on a separate thread and offers many methods 可以使用的最佳模式是BackgroundWorker,因为在单独的线程上执行操作并提供了许多方法

from MSDN 从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. 当您需要响应式UI且与此类操作相关的长时间延迟时,BackgroundWorker类提供了一种便捷的解决方案。

 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 希望这个帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM