tnblog
首页
视频
资源
登录

接口异步调用

5508人阅读 2021/4/25 11:50 总访问:1590424 评论:0 收藏:0 手机
分类: mvc

1.先创建一个WCf的应用程序

2.在窗体里调用的步骤

2.1添加接口
//异步调用

  1. private void button1_Click(object sender, EventArgs e)
  2. {//方法一:自己开辟线程

//1.1用Thread开辟线程是比较老的方法

  1. Thread thread = new Thread(() =>
  2. {
  3. Service2.Service2Client client = new Service2.Service2Client();
  4. int result = client.Sum(1, 3);
  5. MessageBox.Show(result + "");
  6. });
  7. thread.Start();
  8. }

//方法1.2

  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. //1.2使用task开辟
  4. Task.Run(() =>
  5. {
  6. Service2.Service2Client client = new Service2.Service2Client();
  7. int result = client.Sum(1, 3);
  8. MessageBox.Show(result + "");
  9. });
  10. }

//方法2

private void button1_Click(object sender, EventArgs e)
{

  1. CallSumAsyuc();

}

方法2”方法CallSumAsyuc()”的位置

  1. public async void CallSumAsyuc()
  2. {
  3. Service2.Service2Client client = new Service2.Service2Client();
  4. int result = await client.SumAsync(1, 3);
  5. MessageBox.Show(result + "");
  6. }

//方法3


private void button1_Click(object sender, EventArgs e)
{

  1. //首先在 Service2右击“配置服务应用”=>"点生成异步操作"
  2. Service2.Service2Client client = new Service2.Service2Client();
  3. client.SumAsync(6,6);
  4. //接口方法执行完毕后执行
  5. client.SumCompleted += client_SumCompleted;

}

方法client.SumCompleted += client_SumCompleted处

public void client_SumCompleted(object sender, Service2.SumCompletedEventArgs e)
{
MessageBox.Show(“接口返回值:”+e.Result);
}

评价
没有个性,不需要签名
排名
4
文章
473
粉丝
3
评论
2
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术