Posted by: Sourav | October 1, 2016

Retrieve data from thread using delegate,VB Teacher Sourav,Kolkata 09748184075


Imports System.Threading
Public Delegate Sub sumofnumbersdelegate(ByVal sumofnumbers As Integer)
Module Module1
Sub sumofnumbers(ByVal result As Integer)
Console.WriteLine(“The result is {0}”, result)

End Sub

Sub Main()
Console.WriteLine(“Please enter the target number”)
Dim target As Integer = Convert.ToInt32(Console.ReadLine)
Dim sumofnumbersdelegate1 As sumofnumbersdelegate = New sumofnumbersdelegate(AddressOf sumofnumbers)
Dim number As numbers = New numbers(target, sumofnumbersdelegate1)

Dim t1 As Thread = New Thread(AddressOf number.sumofnumbers)
t1.Start()

Console.ReadLine()

End Sub

End Module

Public Class numbers

Private target As Integer
Private sumofnumbersdelegate As sumofnumbersdelegate
Public Sub New(ByVal target As Integer, ByVal sumofnumbersdelegate As sumofnumbersdelegate)

Me.target = target
Me.sumofnumbersdelegate = sumofnumbersdelegate

End Sub

Public Sub sumofnumbers()
Dim result As Integer = 0

Dim i As Integer
For i = 1 To target
‘Console.WriteLine(i)
result = result + i
Next
If Not IsNothing(sumofnumbersdelegate) Then
sumofnumbersdelegate(result)
End If
End Sub

End Class

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: