| 网站首页 | VB.Net实例分析 | .Net技术文摘 | 下载中心 | VB.Net书籍笔记 | .Net源码 | VBS脚本与组件调用 | Windows2008技术文摘 | 给我们留言 | 
最新公告:

  没有公告

您现在的位置: 乐博网 >> VB.Net实例分析 >> 实例学习笔记 >> 实例分析正文
最新推荐 更多内容
ThreadUIUpdate(VB2010实例)
ThreadUIUpdate(VB2010实例)
作者:Akyao    来源:乐博网收集     更新时间:2011-2-26

本文演示当数据来自不同的线程时如何更新用户界面,来自乐博网。

如果你想下载本文的源代码RAR压缩集合包  请访问
VB2010源代码集合包(芋头糕)    http://www.lob.cn/code/utility/2795.shtml
特别感谢网友 芋头糕 将此资源提供乐博网分享

ThreadUIUpdate实例代码如下:

' Copyright (c) Microsoft Corporation. All rights reserved.
Imports System.Threading

Public Class MainForm

    ' Used to track which thread called the UI Update function.
    Private callingThread As Integer  

    ' Initialize the form. 更多.net源代码和教程  来自乐博网 lob.cn
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Update the UI to show the current thread that the UI is running on.
        callingThread = System.Threading.Thread.CurrentThread.ManagedThreadId()
        Label2.Text = "The UI Thread Number is: " + callingThread.ToString()
        Label1.Text = ""
    End Sub

    ' Functions to update the UI from a new thread.
#Region "New Thread Functions"

    Private Sub ThreadButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ThreadButton.Click
        ' Starts a new thread to call UpdateUI from.
        Dim newThread As New Thread(AddressOf UpdateUI)
        newThread.Start()
    End Sub

#End Region

    ' Functions to update the UI using the UI thread.
#Region "UI Update Functions"

    Private Sub UIButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UIButton.Click
        ' Update the UI from the UI thread.
        callingThread = System.Threading.Thread.CurrentThread.ManagedThreadId()
        UpdateUI()
    End Sub

    ' The Delegate that is invoked by the control on the form that needs to be updated.
    Delegate Sub UIDelegate()

    Private Sub UpdateUI()
        ' If InvokeRequired is true then the call is being made on a thread other
        ' than the UI thread.
        If Label1.InvokeRequired Then
            ' Call UpdateUI by invoking a delegate with the UI control
            callingThread = System.Threading.Thread.CurrentThread.ManagedThreadId()
            Dim newDelegate As New UIDelegate(AddressOf UpdateUI)
            Label1.Invoke(newDelegate)
        Else
            Label1.Text = "The update function used thread " + callingThread.ToString()
        End If
    End Sub

#End Region

    ' Functions to update the UI using a threading timer.
#Region "Timer Functions"
    Private Sub StartTimer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartTimer.Click

        Dim timerThread As New Thread(AddressOf RunTimer)
        timerThread.Start()

    End Sub

    Class TimerObjClass
        Public Count As Integer
        Public TimerReference As System.Threading.Timer
        Public TimerCanceled As Boolean
    End Class

    Sub RunTimer()
        Dim StateObj As New TimerObjClass()
        StateObj.TimerCanceled = False
        StateObj.Count = 1
        Dim TimerDelegate As New Threading.TimerCallback(AddressOf TimerTask)

        Dim TimerItem As New System.Threading.Timer(TimerDelegate, StateObj, _
                                                    2000, 2000)
        StateObj.TimerReference = TimerItem
        While StateObj.Count < 5
            System.Threading.Thread.Sleep(2000)
        End While

        StateObj.TimerCanceled = True
    End Sub

    Sub TimerTask(ByVal StateObj As Object)
        Dim State As TimerObjClass = CType(StateObj, TimerObjClass)
        State.Count += 1
        callingThread = System.Threading.Thread.CurrentThread.ManagedThreadId()

        UpdateUI()

        If State.TimerCanceled Then
            State.TimerReference.Dispose()
        End If
    End Sub
#End Region

    Private Sub exitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitToolStripMenuItem.Click
        Me.Close()
    End Sub
End Class

  • 上一篇:

  • 下一篇:
  • 【字体: 】【打印此文】【关闭窗口
      相关文章:(只显示最新16条)
    Tablet PC 2005 的区分上下文功能(VB2010实例)
    响应数字化仪触笔背面的橡皮擦(VB2010实例)
    WMI编程实例(VB2010实例)
    系统服务管理实例(VB2010实例)
    进程管理(VB2010实例)
    显示进程组成模块(VB2010实例)
    任务管理器编程(VB2010实例)
    性能计数器编程(VB2010实例)
    消息队列MSMQListener(VB2010实例)
    消息队列MSMQClient(VB2010实例)
    写入事件日志(VB2010实例)
    读取事件日志(VB2010实例)
    创建和删除事件日志(VB2010实例)
    COMPort编程(VB2010实例)
    读取大文件(VB2010实例)
    读取文件(VB2010实例)

    | 设为首页 | 加入收藏 | 联系站长 | | 友情链接 | 版权申明 |
    乐博网欢迎各种媒体转载我们的原创作品[转载请注明出处];我们鼓励更多VB.Net开发者一起加入研究与探讨;如发现文章访问错误、内容错误或版权疑问、内容有违相关法律(如涉及政治、色情、反动或散布虚假有害信息)等情况,请及时向我们举报,我们将及时纠正!
    联系邮箱:Shiny#vip.qq.com (#替换为@) QQ交流群: 40797788 [闽ICP备05014267号]