Find Code:
All Words
Any of the Words
Exact Phrase
Home
:
Code
:
Forums
:
Submit
:
Mailing List
:
About
:
Contact
Code
All
VB.NET
ASP.NET
C#
VB Classic
ASP Classic
Snippets
Popular
Resources
Submit Code
Forums
Articles
Tips
Links
Books
Contest
Link to us
Multithreading in Visual Basic.NET
Author:
Pramod Kumar Singh
Submitted:
2/17/2021
Version:
VB 2015
Compatibility:
VB 2005, VB 2008, VB 2010, VB 2012, VB 2015
Category:
Miscellaneous
Views:
4094
The sample code demonstrates multithreading in VB.NET. The application creates two threads that are accesing the same account amount.This reqires synchronization of threads, which is accomplished in VB.NET through use of the Monitor object of the the Thread class. To run this code, create a new Console application in Visual Basic.NET, paste the code into the code window, right click the project name and select project properties, and change the startup object to Thread Sample.
Declarations:
'none
Code:
Imports System Imports System.Threading Public Module ThreadSample Public Balance As Integer = 1000 Sub Main() Dim account As Account = New Account() Dim depositeBalance1 As DepositeBalance = New _ DepositeBalance(account, 1000, "Customer 1") Dim depositeBalance2 As DepositeBalance = New _ DepositeBalance(account, 1000, "Customer 2") Dim t1 As Thread = New _ Thread(AddressOf depositeBalance1.DepositeAmount) Dim t2 As Thread = New _ Thread(AddressOf depositeBalance2.DepositeAmount) t1.Start() t2.Start() Try t1.Join() t2.Join() Catch e As Exception Console.Write(e.ToString()) Finally 'Do Nothing End Try End Sub Public Class Account Private balanceAmount As Integer Public Sub Deposite(ByVal amount As Integer, _ ByVal message As String) Console.Write(message & _ " Depositing Amount " & amount) Console.Write(message & " Checking Previous Balance") Monitor.Enter(Me) balanceAmount = getBalance() Console.Write(message & _ " Previous Balance in Account " & balanceAmount) balanceAmount += amount Console.Write(message & _ " Updating Balance in Account ") setBalance(balanceAmount) Monitor.Exit(Me) Console.Write(message & " Update Balance " & Balance) End Sub Private Function getBalance() As Integer Try Thread.sleep(1000) Catch e As Exception Console.Write(e.ToString()) Finally 'Do Nothing End Try Return Balance End Function Private Sub setBalance(ByVal amount As Integer) Try Thread.sleep(1000) Catch e As Exception Console.Write(e.ToString()) Finally 'Do Nothing End Try Balance = amount End Sub End Class Public Class DepositeBalance Private account As Account Private amount As Integer Private message As String Public Sub new(ByRef account As Account, _ ByVal amount As Integer, ByVal message As String) MyBase.New() Me.account = account Me.amount = amount Me.message = message End Sub Public Sub DepositeAmount() Account.Deposite(amount, message) End Sub End Class End Module
Home
|
Forums
|
Submit
|
Books
|
Mailing List
|
Advertising
|
About
|
Contact
© 2023 A1VBCode. All rights reserved.
Legal disclaimer & terms of use
Privacy statement