Visual Basic .NET Language
बहु सूत्रण
खोज…
थ्रेड क्लास का उपयोग करके मल्टीथ्रेडिंग
यह उदाहरण Thread
क्लास का उपयोग करता है, लेकिन BackgroundWorker
का उपयोग करके मल्टीथ्रेडेड एप्लिकेशन भी बनाया जा सकता है। AddNumber
, SubstractNumber
और DivideNumber
फ़ंक्शन को अलग-अलग थ्रेड द्वारा निष्पादित किया जाएगा:
संपादित करें: अब UI थ्रेड समाप्त होने के लिए बच्चे के थ्रेड्स का इंतजार करता है और परिणाम दिखाता है।
Module Module1
'Declare the Thread and assign a sub to that
Dim AddThread As New Threading.Thread(AddressOf AddNumber)
Dim SubstractThread As New Threading.Thread(AddressOf SubstractNumber)
Dim DivideThread As New Threading.Thread(AddressOf DivideNumber)
'Declare the variable for holding the result
Dim addResult As Integer
Dim SubStractResult As Integer
Dim DivisionResult As Double
Dim bFinishAddition As Boolean = False
Dim bFinishSubstration As Boolean = False
Dim bFinishDivision As Boolean = False
Dim bShownAdditionResult As Boolean = False
Dim bShownDivisionResult As Boolean = False
Dim bShownSubstractionResult As Boolean = False
Sub Main()
'Now start the trheads
AddThread.Start()
SubstractThread.Start()
DivideThread.Start()
'Wait and display the results in console
Console.WriteLine("Waiting for threads to finish...")
Console.WriteLine("")
While bFinishAddition = False Or bFinishDivision = False Or bFinishSubstration = False
Threading.Thread.Sleep(50) 'UI thread is sleeping
If bFinishAddition And Not bShownAdditionResult Then
Console.WriteLine("Addition Result : " & addResult)
bShownAdditionResult = True
End If
If bFinishSubstration And Not bShownSubstractionResult Then
Console.WriteLine("Substraction Result : " & SubStractResult)
bShownSubstractionResult = True
End If
If bFinishDivision And Not bShownDivisionResult Then
Console.WriteLine("Division Result : " & DivisionResult)
bShownDivisionResult = True
End If
End While
Console.WriteLine("")
Console.WriteLine("Finished all threads.")
Console.ReadKey()
End Sub
Private Sub AddNumber()
Dim n1 As Integer = 22
Dim n2 As Integer = 11
For i As Integer = 0 To 100
addResult = addResult + (n1 + n2)
Threading.Thread.Sleep(50) 'sleeping Add thread
Next
bFinishAddition = True
End Sub
Private Sub SubstractNumber()
Dim n1 As Integer = 22
Dim n2 As Integer = 11
For i As Integer = 0 To 80
SubStractResult = SubStractResult - (n1 - n2)
Threading.Thread.Sleep(50)
Next
bFinishSubstration = True
End Sub
Private Sub DivideNumber()
Dim n1 As Integer = 22
Dim n2 As Integer = 11
For i As Integer = 0 To 60
DivisionResult = DivisionResult + (n1 / n2)
Threading.Thread.Sleep(50)
Next
bFinishDivision = True
End Sub
End Module
Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow