Sample Multithreaded Mailing Application
This Windows application uses multithreading capabilities of VB.NET to realize a mailing application project. You can use to learn the multithreading working style. Application seems like this (Bu Windows uygulaması, bir mailing projesini gerçekleştirmek için VB.NET'in çokkanalılık özelliklerini kullanmaktadır. Bu uygulamanın kodlarını inceleyerek, çokkanallı uygulama geliştirmeyi öğrenebilirsiniz. Uygulama şuna benziyor:):
Main code (Form1.vb):
Public Class Form1
Public Stopped As Boolean = False
Public MaxThreadCount As Integer = 10
Public PostCount As Integer = 20
Public PostSent As Integer = 0
Public AssignedJob As Integer = 0
Public Status As String = "Ready"
Public Threads As IList(Of System.Threading.Thread) = New List(Of System.Threading.Thread)
Public MessageSenders As IList(Of MessageSender) = New List(Of MessageSender)
Sub Logging(ByVal text As String)
lstLog.Items.Add(Now.ToLongTimeString + ": " + text)
End Sub
Sub SenderThreadOnFinish(ByVal JobInfo As JobInfo)
PostSent = PostSent + 1
End Sub
Sub AssignJob(ByVal ThreadIndex As Integer)
MessageSenders.Item(ThreadIndex).JobInfo.PostIndex = AssignedJob
If Threads.Count - 1 < ThreadIndex Then
Threads.Add(New System.Threading.Thread(AddressOf MessageSenders.Item(ThreadIndex).Send))
Else
Threads.Item(ThreadIndex) = New System.Threading.Thread(AddressOf MessageSenders.Item(ThreadIndex).Send)
End If
Threads.Item(ThreadIndex).Start()
AssignedJob = AssignedJob + 1
Logging("Thread" + ThreadIndex.ToString + " sending post#" + AssignedJob.ToString)
End Sub
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
Dim ThreadIndex As Integer
Status = "Running"
MaxThreadCount = nmrMaxThreadCount.Value
PostCount = nmrPostCount.Value
PostSent = 0
AssignedJob = 0
lstStatus.Items.Clear()
lstLog.Items.Clear()
'Initialize the status listbox
Logging("Threads are being created...")
For ThreadIndex = 0 To MaxThreadCount - 1
MessageSenders.Add(New MessageSender)
MessageSenders.Item(ThreadIndex).JobInfo.ThreadName = "Thread" + ThreadIndex.ToString
MessageSenders.Item(ThreadIndex).JobInfo.ThreadIndex = ThreadIndex
AddHandler MessageSenders.Item(ThreadIndex).Finish, AddressOf SenderThreadOnFinish
lstStatus.Items.Add("Thread" + ThreadIndex.ToString + ": Ready")
Next
Logging("Threads are being checking to assign job...")
ThreadIndex = 0
While AssignedJob < PostCount
If MessageSenders(ThreadIndex).JobInfo.ThreadStatus = "Ready" Then
AssignJob(ThreadIndex)
End If
If ThreadIndex = MaxThreadCount - 1 Then
ThreadIndex = 0
Else
ThreadIndex = ThreadIndex + 1
End If
Application.DoEvents()
End While
Status = "Ready"
Logging("Mailing have completed")
End Sub
Private Sub tmrThreadStatus_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrThreadStatus.Tick
Dim ThreadIndex As Integer = 0
For ThreadIndex = 0 To Threads.Count - 1
lstStatus.Items(ThreadIndex) = "Thread" + ThreadIndex.ToString + ": " + MessageSenders(ThreadIndex).JobInfo.ThreadStatus
Next
End Sub
End Class
Public Class MessageSender
Public JobInfo As JobInfo = New JobInfo
Public Event Finish(ByVal JobInfo As JobInfo)
Public Sub Send()
JobInfo.ThreadStatus = "Running"
Dim SmtpClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient("0.0.0.0")
Try
Dim Message As New System.Net.Mail.MailMessage("from@example.com", "to@example", "Hi Esref", "Please let me know, when you read the this message.")
SmtpClient.Credentials = New System.Net.NetworkCredential("username", "password")
SmtpClient.Send(Message)
Catch e As Exception
Console.WriteLine(e.Message)
End Try
RaiseEvent Finish(JobInfo)
JobInfo.ThreadStatus = "Ready"
End Sub
End Class
Public Class JobInfo
Public ThreadName As String
Public ThreadIndex As Integer
Public ThreadStatus As String = "Ready"
Public PostIndex As Integer
End Class



