Problem: How to handle events in Visual Basic .NET 4.0 using Handler ?
Solution: Just Copy The Whole Code Below in Console Application
VB Code:
C# Code:
Solution: Just Copy The Whole Code Below in Console Application
VB Code:
Module Module1
Sub Main()
Dim objWork As New Worker
AddHandler objWork.WorkDone, AddressOf work_WorkDone
objWork.DoWork()
End Sub
Private Sub work_WorkDone(ByVal completedWork As Integer)
Console.WriteLine(completedWork)
End Sub
End Module
Public Class Worker
Public Event WorkDone(ByVal completedWork As Integer)
Public Sub DoWork()
For completedWork As Integer = 1 To 100
System.Threading.Thread.Sleep(1000)
RaiseEvent WorkDone(completedWork)
Next
End Sub
End Class
Sub Main()
Dim objWork As New Worker
AddHandler objWork.WorkDone, AddressOf work_WorkDone
objWork.DoWork()
End Sub
Private Sub work_WorkDone(ByVal completedWork As Integer)
Console.WriteLine(completedWork)
End Sub
End Module
Public Class Worker
Public Event WorkDone(ByVal completedWork As Integer)
Public Sub DoWork()
For completedWork As Integer = 1 To 100
System.Threading.Thread.Sleep(1000)
RaiseEvent WorkDone(completedWork)
Next
End Sub
End Class
C# Code:
using Microsoft.VisualBasic; using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Diagnostics;
static class Module1 { public static void Main() { Worker objWork = new Worker(); objWork.WorkDone += work_WorkDone; objWork.DoWork();
} } private static void work_WorkDone(int completedWork) { Console.WriteLine(completedWork); } public class Worker { public event WorkDoneEventHandler WorkDone; public delegate void WorkDoneEventHandler(int completedWork); public void DoWork() { for (int completedWork = 1; completedWork <= 100; completedWork++)
{ System.Threading.Thread.Sleep(1000); if (WorkDone != null)
{ WorkDone(completedWork); } } } }
No comments:
Post a Comment