Joseph Jones
asked on
Threading
Hi
The following code works without any issue. However I want to have a separate thread to run these codes. This thread should run whenever new emails are received and other functions to run within the loop. The users have to be however shown a notification icon on their systems tray when all emails are read and processes.
If you can help me on this, that will be great.
Thanks
Joe
a3: Typo corrected in title and code moved to code snippets
The following code works without any issue. However I want to have a separate thread to run these codes. This thread should run whenever new emails are received and other functions to run within the loop. The users have to be however shown a notification icon on their systems tray when all emails are read and processes.
If you can help me on this, that will be great.
Thanks
Joe
Private Sub btnLogin_Click(sender As System.Object, e As System.EventArgs) Handles btnLogin.Click
If Loginhandle = 0 Then
MessageBox.Show("Login failed...!!!", "LOGIN", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtUserName.Text = Nothing
txtPassword.Text = Nothing
txtUserName.Focus()
Exit Sub
Else
MessageBox.Show("Your are logged in succuessfully..." & _
vbCrLf & "This application is running in your" & _
vbCrLf & "System Tray as an Icon...", "JoeApp", MessageBoxButtons.OK, MessageBoxIcon.Information)
Me.HarvestEmail()
AddHandler olApp.NewMail, AddressOf Me.HarvestEmail
End If
MinimizeToSystemTray()
End sub
Public Sub HarvestEmail()
intTotalEmailsReceived = 0
intTotalWoCreated = 0
olNameSpace = olApp.GetNamespace("MAPI")
olFolder = olNameSpace.Folders("GRRTesting@records.nsw.gov.au").Folders("Inbox")
olInboxItems = olFolder.Items
For Each Me.olNewMail In olInboxItems
If Me.olNewMail.UnRead Then
If Me.olNewMail.MessageClass = "IPM.Note.mysecondform" Then
'MsgBox(olNewMail.SenderEmailAddress.ToString)
Call InsertInToWorkorderHeaderTable()
intReturnResult = GettItemQualfiedCount(JoeInt)
'JoeIntRetained = JoeInt
If intReturnResult = 0 Then 'If GetItemQualfied = 0
intReturnResult = GetValuesForWoHeaderAndInsertIntoDB()
If intReturnResult = 0 Then
intReturnResult = GetValuesForWoItemsAndInsertIntoDB(JoeInt)
End If
ElseIf intReturnResult = -1 And JoeOmitErrors = True Then
intReturnResult = GetValuesForWoHeaderAndInsertIntoDB()
'MsgBox(intReturnResult)
If intReturnResult = 0 Then
intReturnResult = GetValuesForWoItemsAndInsertIntoDB(JoeInt)
End If
End If
'olNewMail.Delete()
olNewMail.UnRead = False
intTotalEmailsReceived = intTotalEmailsReceived + 1
End If
End If
Next
NotifyIcon1.ShowBalloonTip(3000, "My Title", "New WO mail received and created. The number of Emails : " & intTotalEmailsReceived, ToolTipIcon.Info)
End Sub
Public Sub MinimizeToSystemTray()
Me.Opacity = 0
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedToolWindow
Me.ShowInTaskbar = False
Me.WindowState = FormWindowState.Minimized
NotifyIcon1.Visible = True
End Sub
a3: Typo corrected in title and code moved to code snippets
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
it will be difficult if the example in the second link is confusing ...
I mean, the code is simple, to declare a thread, tell it what is has to do (which procedure to start), and to actually start it (in background)
Private trd As Thread --> this line on form level, otherwise it would go out of scope ...
trd = New Thread(AddressOf ThreadTask)
trd.IsBackground = True
trd.Start()
please clarify what is "confusing" about this...
I mean, the code is simple, to declare a thread, tell it what is has to do (which procedure to start), and to actually start it (in background)
Private trd As Thread --> this line on form level, otherwise it would go out of scope ...
trd = New Thread(AddressOf ThreadTask)
trd.IsBackground = True
trd.Start()
please clarify what is "confusing" about this...
ASKER
Hi Guy,
I am sorry if I have offended you by using the word "Confusing". I know a little bit how to declare thread and call it for a particular task. In my question I have also asked how to make a thread to start again once the task is finished. And also if you see my HarvestEmail() procedure, it calls other functions also. Will attaching a thread to HarvestEmail() will be sufficient enough to handle all other functions? I also need to show a notification once all the emails are read and the thread should start again after the notification is closed.
Your help on this will be much appreciated.
Thanks
Joe
I am sorry if I have offended you by using the word "Confusing". I know a little bit how to declare thread and call it for a particular task. In my question I have also asked how to make a thread to start again once the task is finished. And also if you see my HarvestEmail() procedure, it calls other functions also. Will attaching a thread to HarvestEmail() will be sufficient enough to handle all other functions? I also need to show a notification once all the emails are read and the thread should start again after the notification is closed.
Your help on this will be much appreciated.
Thanks
Joe
>Will attaching a thread to HarvestEmail() will be sufficient enough to handle all other functions?
yes. all functions called within the thread will be processed by that thread.
>I also need to show a notification once all the emails are read
I assume you know how to actually display the notification icon... and just need to know when to do it, which is by using the WaitCallback delegate, for example:
https://msdn.microsoft.com/en-us/library/system.threading.waitcallback%28v=vs.110%29.aspx
yes. all functions called within the thread will be processed by that thread.
>I also need to show a notification once all the emails are read
I assume you know how to actually display the notification icon... and just need to know when to do it, which is by using the WaitCallback delegate, for example:
https://msdn.microsoft.com/en-us/library/system.threading.waitcallback%28v=vs.110%29.aspx
BTW; I was not offended, by "confusing". I am aware that advanced programming like threading can be confusing and not simple to explain over "plain text" forum
ASKER
Hi Guy,
Thanks for the info. By the way I want this thread to run whenever new emails are received and go to sleep and then run again after new emails are received. Will this WailCallback Delegate will do this job?
Thanks
Joe
Thanks for the info. By the way I want this thread to run whenever new emails are received and go to sleep and then run again after new emails are received. Will this WailCallback Delegate will do this job?
Thanks
Joe
>whenever new emails are received
as you cannot know when actually new emails did arrive without checking the mail box, you must run (start) this process (thread) with a timer:
https://msdn.microsoft.com/en-us/library/system.timers.timer%28v=vs.110%29.aspx
the WaitCallback is to be used from the thread to inform whenver actually an email arrived or when the process completed (even with no new emails)
as you cannot know when actually new emails did arrive without checking the mail box, you must run (start) this process (thread) with a timer:
https://msdn.microsoft.com/en-us/library/system.timers.timer%28v=vs.110%29.aspx
the WaitCallback is to be used from the thread to inform whenver actually an email arrived or when the process completed (even with no new emails)
If you still need help, you are already handling newemail event of outlook which negates the need for a timer. All you need to do, in my opinion, is add another method which is called by the newemail event (instead of harvestemails). In that method, you then create a thread and start it to execute harvestemail.
There would be issues which need considering such as making sure you don't access any UI controls without using delegates and issues around multiple copies of harvestemail running simultaneously.
There would be issues which need considering such as making sure you don't access any UI controls without using delegates and issues around multiple copies of harvestemail running simultaneously.
ASKER
I am completely new to using threads. The examples shows in the link actually confuses me. Can you please help me to sort my issue by explaining specifically for my app.
Thanks for your help. Much appreciated.
Cheers,
Joe