ANPOP Developers Center > Using ANPOP in Multiple Threading .NET Application
You should download the anpop installer and install it on your machine at first. If you want to distribute or deploy anpop without anpop installer, please click here to learn more.
Why use multi-threading?
Multi-threading application is the ability to execute more that one tasks simultaneous in a single application which subsequently enhance the application performance.
Create thread in .NET
It is very easy to create thread in .NET application. First you need to define a static (shared) method, this method would be the entry of worker thread. The following code demonstrates how to create a worker thread in Visual Basic.NET
Imports System
Imports System.Threading
Imports ANPOPLib
Public Class SimpleThreadClass
'The method that will be called when the thread is started
Public Shared Sub RetrieveEmail()
Dim oPop3 As New POPMAINClass()
oPOp3.Connect( "mail.adminsystem.net", "test@adminsystem.net", "test" )
'Do something
End Sub
Public Shared Sub Main()
' using a ThreadStart delegate.
Dim t As New Thread(New ThreadStart(AddressOf RetrieveEmail))
t.Start()
'Do something
End Sub 'Main
End Class
Now, we can use ANPOP to retrieve email by RetrieveEmail method. As we create multi-threading in this application, we can access multiple pop3 accounts concurrently.
Thread model
Objects of ANPOP are based on Apartment Threading. So we suggest you create each object instance for each thread just like the above sample code. And do not use the object instance which is not created in current thread.
Thread communication and synchronization
How can we transfer data to a sub-thread and how can we assure that the data can only be accessed by one thread? In the following code, we use static ArrayList to share data in sub-thread and lock data via Mutex.
Imports System
Imports System.Threading
Imports ANPOPLib
Public Class SimpleThreadClass
Public Class Pop3AccountClass
Public m_pop3Server As String
Public m_userName As String
Public m_password As String
End Class
Private Shared m_hMutex As New Mutex(False)
Private Shared m_arTaskList As New ArrayList()
'The method that will be called when the thread is started
Public Shared Sub RetrieveEmail()
Dim oPop3 As New POPMAINClass()
Dim pop3Account As Pop3AccountClass
Do While True
pop3Account = PopAccount() 'Get pop3 account from ArrayList
If Not(pop3Account Is Nothing) Then
oPOp3.Connect( pop3Accout.m_pop3Server, _
pop3Account.m_userName, pop3Account.m_password )
'Do something
Else
Thread.Sleep( 100 )
End If
Loop
End Sub
'Lock ArrayList
Private Shared Sub LockTaskList()
m_hMutex.WaitOne()
End Sub
'Unlock ArrayList
Private Shared Sub UnlockTaskList()
m_hMutex.ReleaseMutex()
End Sub
'Push a pop3 account into ArrayList
Public Shared Function PushAccount(ByRef pop3Account As Pop3AccountClass)
LockTaskList()
m_arTaskList.Add(pop3Account)
UnlockTaskList()
End Function
'Pop a pop3 account from ArrayList
Public Shared Function PopAccount() As Pop3AccountClass
LockTaskList()
If m_arTaskList.Count > 0 Then
PopAccount = m_arTaskList(0)
m_arTaskList.RemoveAt(0)
Else
PopAccount = Nothing
End If
UnlockTaskList()
End Function
Public Shared Sub Main()
' create a worker thread, of course you can create multiple worker threads here
Dim t As New Thread(New ThreadStart(AddressOf RetrieveEmail))
t.Start()
Dim pop3Account As New Pop3AccountClass
pop3Account.m_pop3Server = "mail.adminsystem.net"
pop3Account.m_userName = "test@adminsystem.net"
pop3Account.m_password = "test"
PushAccount( pop3Account ) 'submit a pop3 account to sub-thread
'submit other accounts to sub-thread.
End Sub 'Main
End Class
Full Sample
Please refer to POP3QUEUE in ANPOP installation package.
Free Email Support
Not enough? Please contact our technical support team.
Support@EmailArchitect.NET
VIP@EmailArchitect.NET(Registered
User)
Remarks
We usually reply emails in 24hours. The reason for getting no response is
likely that your smtp server bounced our reply. In this case, please try to use
another email address to contact us. Your Hotmail or Yahoo email account is
recommended.
|