多線程中用委托,不會沖突。
成都創(chuàng)新互聯(lián)-成都網站建設公司,專注成都網站建設、成都做網站、網站營銷推廣,域名注冊,虛擬主機,網站運營有關企業(yè)網站制作方案、改版、費用等問題,請聯(lián)系成都創(chuàng)新互聯(lián)。
在類中用委托,也可以方便模塊思路。
一委托:此示例演示如何將方法與委托關聯(lián)然后通過委托調用該方法。
創(chuàng)建委托和匹配過程
創(chuàng)建一個名為 MySubDelegate 的委托。
Delegate Sub MySubDelegate(ByVal x As Integer)
聲明一個類,該類包含與該委托具有相同簽名的方法。
Class class1
Sub Sub1(ByVal x As Integer)
MsgBox("The value of x is: " CStr(x))
End Sub
End Class
定義一個方法,該方法創(chuàng)建該委托的實例并通過調用內置的 Invoke 方法調用與該委托關聯(lián)的方法。
Protected Sub DelegateTest()
Dim c1 As New class1
' Create an instance of the delegate.
Dim msd As MySubDelegate = AddressOf c1.Sub1
' Call the method.
msd.Invoke(10)
End Sub
二、事件
下面的示例程序闡釋如何在一個類中引發(fā)一個事件,然后在另一個類中處理該事件。AlarmClock 類定義公共事件 Alarm,并提供引發(fā)該事件的方法。AlarmEventArgs 類派生自 EventArgs,并定義 Alarm 事件特定的數據。WakeMeUp 類定義處理 Alarm 事件的 AlarmRang 方法。AlarmDriver 類一起使用類,將使用 WakeMeUp 的 AlarmRang 方法設置為處理 AlarmClock 的 Alarm 事件。
該示例程序使用事件和委托和引發(fā)事件中詳細說明的概念。
示例
' EventSample.vb.
'
Option Explicit
Option Strict
Imports System
Imports System.ComponentModel
Imports Microsoft.VisualBasic
Namespace EventSample
' Class that contains the data for
' the alarm event. Derives from System.EventArgs.
'
Public Class AlarmEventArgs
Inherits EventArgs
Private _snoozePressed As Boolean
Private nrings As Integer
'Constructor.
'
Public Sub New(snoozePressed As Boolean, nrings As Integer)
Me._snoozePressed = snoozePressed
Me.nrings = nrings
End Sub
' The NumRings property returns the number of rings
' that the alarm clock has sounded when the alarm event
' is generated.
'
Public ReadOnly Property NumRings() As Integer
Get
Return nrings
End Get
End Property
' The SnoozePressed property indicates whether the snooze
' button is pressed on the alarm when the alarm event is generated.
'
Public ReadOnly Property SnoozePressed() As Boolean
Get
Return _snoozePressed
End Get
End Property
' The AlarmText property that contains the wake-up message.
'
Public ReadOnly Property AlarmText() As String
Get
If _snoozePressed Then
Return "Wake Up!!! Snooze time is over."
Else
Return "Wake Up!"
End If
End Get
End Property
End Class
' Delegate declaration.
'
Public Delegate Sub AlarmEventHandler(sender As Object, _
e As AlarmEventArgs)
' The Alarm class that raises the alarm event.
'
Public Class AlarmClock
Private _snoozePressed As Boolean = False
Private nrings As Integer = 0
Private stopFlag As Boolean = False
' The Stop property indicates whether the
' alarm should be turned off.
'
Public Property [Stop]() As Boolean
Get
Return stopFlag
End Get
Set
stopFlag = value
End Set
End Property
' The SnoozePressed property indicates whether the snooze
' button is pressed on the alarm when the alarm event is generated.
'
Public Property SnoozePressed() As Boolean
Get
Return _snoozePressed
End Get
Set
_snoozePressed = value
End Set
End Property
' The event member that is of type AlarmEventHandler.
'
Public Event Alarm As AlarmEventHandler
' The protected OnAlarm method raises the event by invoking
' the delegates. The sender is always this, the current instance
' of the class.
'
Protected Overridable Sub OnAlarm(e As AlarmEventArgs)
RaiseEvent Alarm(Me, e)
End Sub
' This alarm clock does not have
' a user interface.
' To simulate the alarm mechanism it has a loop
' that raises the alarm event at every iteration
' with a time delay of 300 milliseconds,
' if snooze is not pressed. If snooze is pressed,
' the time delay is 1000 milliseconds.
'
Public Sub Start()
Do
nrings += 1
If stopFlag Then
Exit Do
Else
If _snoozePressed Then
System.Threading.Thread.Sleep(1000)
If (True) Then
Dim e As New AlarmEventArgs(_snoozePressed, nrings)
OnAlarm(e)
End If
Else
System.Threading.Thread.Sleep(300)
Dim e As New AlarmEventArgs(_snoozePressed, nrings)
OnAlarm(e)
End If
End If
Loop
End Sub
End Class
' The WakeMeUp class has a method AlarmRang that handles the
' alarm event.
'
Public Class WakeMeUp
Public Sub AlarmRang(sender As Object, e As AlarmEventArgs)
Console.WriteLine((e.AlarmText + ControlChars.Cr))
If Not e.SnoozePressed Then
If e.NumRings Mod 10 = 0 Then
Console.WriteLine(" Let alarm ring? Enter Y")
Console.WriteLine(" Press Snooze? Enter N")
Console.WriteLine(" Stop Alarm? Enter Q")
Dim input As String = Console.ReadLine()
If input.Equals("Y") Or input.Equals("y") Then
Return
Else
If input.Equals("N") Or input.Equals("n") Then
CType(sender, AlarmClock).SnoozePressed = True
Return
Else
CType(sender, AlarmClock).Stop = True
Return
End If
End If
End If
Else
Console.WriteLine(" Let alarm ring? Enter Y")
Console.WriteLine(" Stop Alarm? Enter Q")
Dim input As String = Console.ReadLine()
If input.Equals("Y") Or input.Equals("y") Then
Return
Else
CType(sender, AlarmClock).Stop = True
Return
End If
End If
End Sub
End Class
' The driver class that hooks up the event handling method of
' WakeMeUp to the alarm event of an Alarm object using a delegate.
' In a forms-based application, the driver class is the
' form.
'
Public Class AlarmDriver
Public Shared Sub Main()
' Instantiates the event receiver.
Dim w As New WakeMeUp()
' Instantiates the event source.
Dim clock As New AlarmClock()
' Wires the AlarmRang method to the Alarm event.
AddHandler clock.Alarm, AddressOf w.AlarmRang
clock.Start()
End Sub
End Class
End Namespace
把UI對象當做參數傳入 比如 Sub a(Form As Form1)
之后再用Form.Invoke(你的委托,參數) 就可以即時修改Form中的內容了
委托主要用于.NETFramework中的事件處理程序和回調函數,它是事件的基礎。委托的作用類似于c++中函數指針的作用。不同的是,委托實例獨立于它所封裝的方法的類,并且方法類型與委托的類型是兼容的。函數指針只能引用靜態(tài)函數,而委托可以應用靜態(tài)和實例方法。所有委托都是繼承自System.Delegate類,并且有一個調用列表。調用委托時所執(zhí)行的方法都被存放在這樣的一個連接列表中。使用delegate關鍵字可以聲明一個委托。通過將委托與命名方法或匿名方法關聯(lián),可以對委托進行實例化。為了與命名方法一起使用,委托必須用具有可接受簽名的方法進行實例化。usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceConsoleApplication1{//聲明一個委托delegateintMydelegate();classProgram{staticvoidMain(string[]args){testp=newtest();//將委托指向非靜態(tài)方法Mydelegatem=newMydelegate(p.InstanceMethod);//調用非靜態(tài)方法m();//將委托指向靜態(tài)方法m=newMydelegate(test.StaticMethod);//調用靜態(tài)方法m();Console.Read();}}publicclasstest{publicintInstanceMethod(){Console.WriteLine("正在調用非靜態(tài)方法InstanceMethod().");return0;}staticpublicintStaticMethod(){Console.WriteLine("正在調用靜態(tài)方法StaticMethod()。。。。");return0;}}}
address of 顧名思義,就是地址指向,每個函數都有一個地址,address of后面加函數名稱。
handels 事件,你看看按鈕的單擊事件,窗體的啟動事件,每個后面都有這個。
delegate 就是聲明一個委托了。
我也不好詳細說,其實你上Baidu搜這幾個關鍵字加上點注解,比如“delegate的用法”,N多!
文章題目:關于vb.net委托的使用的信息
本文地址:http://www.chinadenli.net/article16/dodcpgg.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、商城網站、做網站、關鍵詞優(yōu)化、網站改版、外貿網站建設
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)