Tuesday, April 27, 2010

Multi Threading in Visual Basic.Net




'Sample Code on how to use multithreading in Visual Basic .Net

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

14 comments:

Unknown said...

Your information related to dot net is really useful for fresher. Thanks for sharing this informative blog..Keep posting..

Dot Net Training



WarpByteChaser9000 said...

elazığ
gümüşhane
kilis
siirt
sakarya
R7İ76

NebulaGoddess said...

ankara parça eşya taşıma
takipçi satın al
antalya rent a car
antalya rent a car
ankara parça eşya taşıma
8CPL

Erdinç said...

Ağrı Lojistik
Çorlu Lojistik
Kars Lojistik
Antalya Lojistik
Rize Lojistik
TFB

30732Nikolas5EB74 said...

66309
Bitlis Parça Eşya Taşıma
Muğla Lojistik
Antalya Evden Eve Nakliyat
Tokat Parça Eşya Taşıma
Mersin Evden Eve Nakliyat

39AEBHeather03072 said...

AFE47
Kocaeli Parça Eşya Taşıma
Karaman Parça Eşya Taşıma
Zonguldak Evden Eve Nakliyat
Amasya Evden Eve Nakliyat
Aydın Parça Eşya Taşıma

12070Briley19E57 said...

ED439
Kırklareli Şehir İçi Nakliyat
Karabük Şehirler Arası Nakliyat
Balıkesir Parça Eşya Taşıma
Ordu Evden Eve Nakliyat
Van Parça Eşya Taşıma
İstanbul Lojistik
Kırıkkale Şehirler Arası Nakliyat
Bayburt Parça Eşya Taşıma
Iğdır Şehirler Arası Nakliyat

395C8Alayna098C6 said...

6C324
Tekirdağ Evden Eve Nakliyat
Adıyaman Evden Eve Nakliyat
Balıkesir Evden Eve Nakliyat
Kilis Evden Eve Nakliyat
oxandrolone anavar
Zonguldak Evden Eve Nakliyat
order dianabol methandienone
masteron for sale
Kırklareli Evden Eve Nakliyat

Seekware Global Northampton said...

Empower your business with bespoke software solutions tailored to your needs. Seekware Global offers expert custom software development services in Northampton, delivering innovative, scalable, and efficient solutions that drive growth and streamline operations. Partner with us for cutting-edge technology solutions crafted to elevate your business to new heights.

Trendy Mixup said...

Grand Theft Auto 6 is more than just a video game; it’s a cultural phenomenon that has captivated gamers for generations. With its immersive open world, engaging storylines, and memorable characters, Grand Theft Auto 6 promises to redefine the gaming experience once again. As we eagerly await its release, the anticipation and excitement continue to build, making Grand Theft Auto 6 one of the most highly anticipated video games of all time.

56061Danny7BADF said...

80FC9
mersin canli goruntulu sohbet siteleri
Balıkesir Chat Sohbet
yabancı görüntülü sohbet uygulamaları
bingöl telefonda kızlarla sohbet
canlı ücretsiz sohbet
düzce sohbet sitesi
diyarbakır seslı sohbet sıtelerı
kocaeli canlı sohbet bedava
tunceli görüntülü sohbet

IGL Coatings India said...

Experience automotive excellence with IGL Coatings India, the foremost ceramic coating manufacturers, wholesalers, and distributors in the India.

StarLink Global said...

Starlink Global excels as a provider of Web Development, Application Development, and SEO Services and Digital Marketing services. Our dedicated team creates innovative websites and applications while driving digital marketing strategies that propel businesses to success. Experience the power of our comprehensive digital solutions.

Top 10 Safety Barrier Manufacturers in the USA said...

Safety barriers are installed in areas where hazards may occur, keeping the environment safe where vehicles, goods, and people move. It is an important protective measure for road safety and factory safety. So how do you choose a reliable safety barrier product? A lot depends on a reliable safety barrier manufacturer. In today’s guide, we’ll introduce you to 10 reliable Security barrier manufacturers in the United States. Read on to learn more.

Post a Comment

 
Powered by Blogger