⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multithreadbox.vb

📁 Programming the .NET Compact Framework with vb 源代码
💻 VB
字号:
' -----------------------------------------------------------------------------
' Code from _Programming the .NET Compact Framework with VB_
' and _Programming the .NET Compact Framework with C#_
' (c) Copyright 2002-2004 Paul Yao and David Durant. 
' All rights reserved.
' -----------------------------------------------------------------------------

Imports System
Imports System.Drawing
Imports System.Collections
Imports System.Windows.Forms
Imports System.Data
Imports System.Threading
Imports System.ComponentModel

Imports MultiThreaded

' Note:
' This control imposes a restriction on the service
'    theads that provide it information, be
'    they threads of code that resides in this 
'    namespace, or threads whose thread proc was
'    defined in some component outside this namespace.
' This restriction is "real world" and should be
'    imposed by all forms/controls that receive data
'    threads other than their main thread.  It is: 
'    The service thread must use Invoke to call the
'    control's method.  
' This ensures that calling of the control's method 
'    runs on the correct thread and is properly 
'    synchronized.  This should be a requirement 
'    in all applications.  Do not be deceived by 
'    the fact that, in this simple app running
'    in a simple execution environment, the call
'       this.formCaller.Invoke(this.deleCallback);
'    could be replaced by 
'       this.deleCallback(this.formCaller,EventArgs.Empty);
'    The latter offers no benefit, and will not run
'    properly under all conditions.  Always use Invoke.

Namespace YaoDurant.Gui

   Public Class MultiThreadBox
      Inherits TextBox

      Private qPassData As Queue
      Private deleReceiveData As EventHandler


      Public Sub New()
         ' Create a System.Collections.Queue to be used by
         '    the service threads to pass data bask to the
         '    this control.
         qPassData = New Queue

         ' The delegate to the form's method that will be
         '    invoked by the service threads.
         deleReceiveData = _
            New EventHandler(AddressOf Me.ReceiveData)
      End Sub


      Public Sub RequestData()
         ' Create a GetDataWrapper object.  The object will
         '    Create and start a thread.  The thread will get
         '    a dataset, place it into qPassData, and then
         '    invoke ReceiveData via a delegate.
         Dim refGetData As _
            New MultiThreaded.GetDataWrapper(Me, _
                                             deleReceiveData, _
                                             qPassData)
      End Sub


      Private Sub ReceiveData(ByVal s As Object, _
                              ByVal e As System.EventArgs)
         ' Retrieve the dataset that was placed into qPassData
         '    by the service thread and display it in controls.
         ' In our app there will only one object in the queue
         '    when this routine is Invoked because we only 
         '    have one service thread running.  Because of
         '    this, the code below could be simplier, 
         '    replacing the while loop with a call to 
         '    Dequeue.  But we wanted to illustrate the 
         '    code needed for the more "real world" 
         '    situation, in which multiple threads might have 
         '    placed multiple objects in the queue.
         Dim dsetPerson As DataSet
         Dim queueEnumerator As IEnumerator = _
                                    qPassData.GetEnumerator()

         ' Enumerate through all the 
         '    DataSets that are in the queue.
         While queueEnumerator.MoveNext()
            dsetPerson = CType(queueEnumerator.Current, DataSet)
            ' Move the DataSet contents into the Text property.
            Me.Text = _
               dsetPerson.Tables(0). _
                  Rows(0)("FirstName").ToString() + _
                  " " + _
               dsetPerson.Tables(0). _
                  Rows(0)("LastName").ToString()
         End While
      End Sub
   End Class

End Namespace

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -