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
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

Open in new window

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

Open in new window


 
Public Sub MinimizeToSystemTray()

        Me.Opacity = 0
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedToolWindow
        Me.ShowInTaskbar = False
        Me.WindowState = FormWindowState.Minimized
        NotifyIcon1.Visible = True


    End Sub

Open in new window


a3: Typo corrected in title and code moved to code snippets
JOLEEJJAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
this shall be your starting page:
https://msdn.microsoft.com/en-us/library/eed6swsx%28v=vs.90%29.aspx
and this the "hello world" example on how to do it in code:
https://support.microsoft.com/en-us/kb/315577

this should be a good starter
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JOLEEJJAuthor Commented:
Hi
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
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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...
0
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

JOLEEJJAuthor Commented:
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
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
>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
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
0
JOLEEJJAuthor Commented:
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
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
>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)
0
CodeCruiserCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.