469,615 Members | 1,615 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,615 developers. It's quick & easy.

Event Handling in Console application?

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
Jan 9 '08 #1
4 5338
On Jan 9, 3:42 pm, "federico" <feder...@michosa.comwrote:
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]
Jan 9 '08 #2

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.

"rowe_newsgroups" <ro********@yahoo.comwrote in message
news:b9**********************************@j20g2000 hsi.googlegroups.com...
On Jan 9, 3:42 pm, "federico" <feder...@michosa.comwrote:
>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]

Jan 9 '08 #3
"federico" <fe******@michosa.comschrieb:
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
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Jan 9 '08 #4
Thanks so much. The flag works nicely.
"federico" <fe******@michosa.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
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

Jan 9 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

18 posts views Thread by Christopher W. Douglas | last post: by
2 posts views Thread by Wayne | last post: by
4 posts views Thread by aaj | last post: by
3 posts views Thread by geskerrett | last post: by
22 posts views Thread by dvestal | last post: by
reply views Thread by marian.kovac | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.