Event Handling in Console application?

F

federico

Hello, I am trying to setup a Visual Basic "Console" application for
searching Outlook folders. To this end I am trying to implement a Handler
for the Outlook.Application.AdvancedSearchComplete Event. The problem I
have is that the application finishes and exits before the AdvanceSearch, so
the Event Handler in never called. If I delay the end of the application,
for example, by having message box right after the call to AdvancedSearch,
the search completes, its handler is executed, and it correctly reports the
number of matched emails. Thus, this works:

Imports Microsoft.Office.Interop
Module Module1
Sub Main()
Dim App As New Outlook.Application
AddHandler App.AdvancedSearchComplete, AddressOf
App_AdvancedSearchComplete
App.AdvancedSearch("'Inbox'", "urn:schemas:httpmail:subject LIKE
'%test'", False, "TestSearch")
MsgBox("pause")
End Sub
Public Sub App_AdvancedSearchComplete(ByVal oSearch As Outlook.Search)
MsgBox("Search done. Found " & oSearch.Results.Count & " emails")
End Sub
End Module

However, if I remove the msgbox call under Main, App_AdvancedSearchComplete
is not executed.

I tried the same code on a "Windows" application, and it works fine, but I
would prefer to use a console application.

I would appreciate any suggestions anyone would have.

Thanks in advance.

federico
 
R

rowe_newsgroups

Hello, I am trying to setup a Visual Basic "Console" application for
searching Outlook folders. To this end I am trying to implement a Handler
for the Outlook.Application.AdvancedSearchComplete Event. The problem I
have is that the application finishes and exits before the AdvanceSearch, so
the Event Handler in never called. If I delay the end of the application,
for example, by having message box right after the call to AdvancedSearch,
the search completes, its handler is executed, and it correctly reports the
number of matched emails. Thus, this works:

Imports Microsoft.Office.Interop
Module Module1
Sub Main()
Dim App As New Outlook.Application
AddHandler App.AdvancedSearchComplete, AddressOf
App_AdvancedSearchComplete
App.AdvancedSearch("'Inbox'", "urn:schemas:httpmail:subject LIKE
'%test'", False, "TestSearch")
MsgBox("pause")
End Sub
Public Sub App_AdvancedSearchComplete(ByVal oSearch As Outlook.Search)
MsgBox("Search done. Found " & oSearch.Results.Count & " emails")
End Sub
End Module

However, if I remove the msgbox call under Main, App_AdvancedSearchComplete
is not executed.

I tried the same code on a "Windows" application, and it works fine, but I
would prefer to use a console application.

I would appreciate any suggestions anyone would have.

Thanks in advance.

federico

Personally, I also use "Console.Read()" to stop a console application
from exiting. This will cause the console app to wait for the user to
hit the enter key before exiting. You could also set a boolean flag
that is checked on a loop (with a thread sleep call to reduce memory/
resource use) that is only set after your handler for the
AdvancedSearchComplete event has fired.

Thanks,

Seth Rowe [MVP]
 
S

sloan

Ditto.

Either put a

Console.WriteLine("Press ENTER to Exit");
Console.ReadLine();

at the end of your program...or set a flag that only allows exist after the
handler has executed.
 
H

Herfried K. Wagner [MVP]

federico said:
Hello, I am trying to setup a Visual Basic "Console" application for
searching Outlook folders. To this end I am trying to implement a Handler
for the Outlook.Application.AdvancedSearchComplete Event. The problem I
have is that the application finishes and exits before the AdvanceSearch,
so the Event Handler in never called. If I delay the end of the
application, for example, by having message box right after the call to
AdvancedSearch, the search completes, its handler is executed, and it
correctly reports the number of matched emails. Thus, this works:

Imports Microsoft.Office.Interop
Module Module1
Sub Main()
Dim App As New Outlook.Application
AddHandler App.AdvancedSearchComplete, AddressOf
App_AdvancedSearchComplete
App.AdvancedSearch("'Inbox'", "urn:schemas:httpmail:subject LIKE
'%test'", False, "TestSearch")
MsgBox("pause")
End Sub
Public Sub App_AdvancedSearchComplete(ByVal oSearch As Outlook.Search)
MsgBox("Search done. Found " & oSearch.Results.Count & " emails")
End Sub
End Module

You can use 'ManualResetEvent' for this purpose. This doesn't prevent the
application from terminating after the event occured (which using
'Console.Read'/'Console.ReadLine' does).

\\\
Imports System.Threading

Friend Module Program
Public Sub Main()
Dim EventOccured As New ManualResetEvent(False)
Dim Worker As New Worker(EventOccured)
Dim WorkerThread As New Thread(AddressOf Worker.DoWork)
Console.WriteLine("Work started at {0}.", Now)
WorkerThread.Start()
EventOccured.WaitOne()
Console.WriteLine("Work finished at {0}.", Now)
End Sub
End Module

Friend Class Worker
Private m_EventOccured As ManualResetEvent

Public Sub New(ByVal Notifier As ManualResetEvent)
m_EventOccured = Notifier
End Sub

Public Sub DoWork()
Thread.Sleep(5000)
m_EventOccured.Set()
End Sub
End Class
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top