首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

不支持一个 STA 线程上针对多个句柄的 WaitAll。解决方法

2012-03-30 
不支持一个 STA 线程上针对多个句柄的 WaitAll。MTAThreadAttribute() _Sub BindMultiData()Dim CusQuery

不支持一个 STA 线程上针对多个句柄的 WaitAll。
<MTAThreadAttribute()> _
  Sub BindMultiData()
  Dim CusQueryStr As String = "select * from customers where companyName ='Alfreds Futterkiste'"

  Dim SupQueryStr As String = "SELECT Customers.CompanyName, Customers.ContactName," & _
  "Orders.OrderID, Orders.OrderDate, " & _
  "Orders.RequiredDate, Orders.ShippedDate " & _
  "FROM Orders, Customers " & _
  "WHERE Orders.CustomerID = Customers.CustomerID " & _
  "AND Customers.CompanyName = 'Alfreds Futterkiste' " & _
  "ORDER BY Customers.CompanyName, Customers.ContactName"

  Using myCon As SqlConnection = New SqlConnection(ConStr)
  Dim CusCmd As SqlCommand = New SqlCommand(CusQueryStr, myCon)
  Dim SupCmd As SqlCommand = New SqlCommand(SupQueryStr, myCon)

  Dim CusDR As SqlDataReader
  Dim SupDr As SqlDataReader

  Dim CusisynResult As IAsyncResult
  Dim SupisynResult As IAsyncResult

  '创建句柄数组
  Dim WHandles() As System.Threading.WaitHandle = {New AutoResetEvent(False), New AutoResetEvent(False)}
  ' Private Shared waitHandles() As WaitHandle = {New AutoResetEvent(False), New AutoResetEvent(False)}


  Dim CusHandle As System.Threading.WaitHandle
  Dim SupHandle As System.Threading.WaitHandle

  myCon.Open()

  CusisynResult = CusCmd.BeginExecuteReader(CommandBehavior.CloseConnection)
  SupisynResult = SupCmd.BeginExecuteReader(CommandBehavior.CloseConnection)

  CusHandle = CusisynResult.AsyncWaitHandle
  SupHandle = SupisynResult.AsyncWaitHandle

  '将等待句柄赋值给句柄数组 
  WHandles(0) = CusHandle
  WHandles(1) = SupHandle

  '将数组传给waitall方法,等待所以的异步查询完成
  System.Threading.WaitHandle.WaitAll(WHandles) ‘//////////就是这儿提示错了


  CusDR = CusCmd.EndExecuteReader(CusisynResult)
  SupDr = SupCmd.EndExecuteReader(SupisynResult)


  Dim Cusbs As BindingSource = New BindingSource
  Cusbs.DataSource = CusDR
  DataGridView1.DataSource = Cusbs

  Dim SupBs As BindingSource = New BindingSource
  SupBs.DataSource = SupDr
  DataGridView1.DataSource = SupBs


  End Using

  End Sub

[解决办法]
ding 有没有高手?

热点排行