如何等待等待功能完成

时间:2013-10-31 15:07:30

标签: function async-await

当我调用连接到使用SerialPort与通风系统通信的af服务器的异步功能时,我遇到了问题。问题是如果我慢慢调用函数它的工作非常好。通过Metro界面中的按钮。但是如果我想要在彼此之后发送10个命令。例如。 10x Await Await ReadData("COMMAND")失败并显示错误由于线程退出或应用程序请求,I / O操作已中止(800703E3) 因此,我认为它不是在新功能尝试之前完成功能我是否正确?

我致电:DataReceived = Await ReadData(SendCommand)

然后调用函数

Public Shared Async Function SendReceive(SendCommand As String) As Task(Of String)
  'Some code (try/catch) and so on.
  Await _Socket.ConnectAsync(srvHostname, srvPort)

  'Some more code (write to server that talks to ventilation system)
  _writer.WriteString(SendCommand)
  Await _writer.StoreAsync()

  ' More code (read answer from the server from  the ventilation system)
  Dim strReceivedBytes As String = Await _reader.LoadAsync(512)
  Return _Reader.ReadString(strReceivedBytes)
End Function

1 个答案:

答案 0 :(得分:0)

不知何故,问题只是在功能结束前多次点击时才会出现问题。所以解决方案是禁用按钮直到功能存在。

相关问题