473,761 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Applica tion.AdvancedSe archComplete 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.Offic e.Interop
Module Module1
Sub Main()
Dim App As New Outlook.Applica tion
AddHandler App.AdvancedSea rchComplete, AddressOf
App_AdvancedSea rchComplete
App.AdvancedSea rch("'Inbox'", "urn:schemas:ht tpmail:subject LIKE
'%test'", False, "TestSearch ")
MsgBox("pause")
End Sub
Public Sub App_AdvancedSea rchComplete(ByV al 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_AdvancedSea rchComplete
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 5694
On Jan 9, 3:42 pm, "federico" <feder...@micho sa.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.Applica tion.AdvancedSe archComplete 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.Offic e.Interop
Module Module1
Sub Main()
Dim App As New Outlook.Applica tion
AddHandler App.AdvancedSea rchComplete, AddressOf
App_AdvancedSea rchComplete
App.AdvancedSea rch("'Inbox'", "urn:schemas:ht tpmail:subject LIKE
'%test'", False, "TestSearch ")
MsgBox("pause")
End Sub
Public Sub App_AdvancedSea rchComplete(ByV al 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_AdvancedSea rchComplete
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
AdvancedSearchC omplete event has fired.

Thanks,

Seth Rowe [MVP]
Jan 9 '08 #2

Ditto.

Either put a

Console.WriteLi ne("Press ENTER to Exit");
Console.ReadLin e();

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

"rowe_newsgroup s" <ro********@yah oo.comwrote in message
news:b9******** *************** ***********@j20 g2000hsi.google groups.com...
On Jan 9, 3:42 pm, "federico" <feder...@micho sa.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.Applica tion.AdvancedSe archComplete 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.Offic e.Interop
Module Module1
Sub Main()
Dim App As New Outlook.Applica tion
AddHandler App.AdvancedSea rchComplete, AddressOf
App_AdvancedSe archComplete
App.AdvancedSea rch("'Inbox'", "urn:schemas:ht tpmail:subject LIKE
'%test'", False, "TestSearch ")
MsgBox("pause")
End Sub
Public Sub App_AdvancedSea rchComplete(ByV al 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_AdvancedSe archComplete
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
AdvancedSearchC omplete event has fired.

Thanks,

Seth Rowe [MVP]

Jan 9 '08 #3
"federico" <fe******@micho sa.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.Applica tion.AdvancedSe archComplete 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.Offic e.Interop
Module Module1
Sub Main()
Dim App As New Outlook.Applica tion
AddHandler App.AdvancedSea rchComplete, AddressOf
App_AdvancedSea rchComplete
App.AdvancedSea rch("'Inbox'", "urn:schemas:ht tpmail:subject LIKE
'%test'", False, "TestSearch ")
MsgBox("pause")
End Sub
Public Sub App_AdvancedSea rchComplete(ByV al oSearch As Outlook.Search)
MsgBox("Search done. Found " & oSearch.Results .Count & " emails")
End Sub
End Module
You can use 'ManualResetEve nt' for this purpose. This doesn't prevent the
application from terminating after the event occured (which using
'Console.Read'/'Console.ReadLi ne' does).

\\\
Imports System.Threadin g

Friend Module Program
Public Sub Main()
Dim EventOccured As New ManualResetEven t(False)
Dim Worker As New Worker(EventOcc ured)
Dim WorkerThread As New Thread(AddressO f Worker.DoWork)
Console.WriteLi ne("Work started at {0}.", Now)
WorkerThread.St art()
EventOccured.Wa itOne()
Console.WriteLi ne("Work finished at {0}.", Now)
End Sub
End Module

Friend Class Worker
Private m_EventOccured As ManualResetEven t

Public Sub New(ByVal Notifier As ManualResetEven t)
m_EventOccured = Notifier
End Sub

Public Sub DoWork()
Thread.Sleep(50 00)
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******@micho sa.comwrote in message
news:%2******** ********@TK2MSF TNGP03.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.Applica tion.AdvancedSe archComplete 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.Offic e.Interop
Module Module1
Sub Main()
Dim App As New Outlook.Applica tion
AddHandler App.AdvancedSea rchComplete, AddressOf
App_AdvancedSea rchComplete
App.AdvancedSea rch("'Inbox'", "urn:schemas:ht tpmail:subject LIKE
'%test'", False, "TestSearch ")
MsgBox("pause")
End Sub
Public Sub App_AdvancedSea rchComplete(ByV al 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_AdvancedSea rchComplete 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
2888
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code that applies to all these events, but I need to have specific code execute when the form closes. The properties for this method are sender (the originator) and e (event arguments). I know how to get typeof (sender) to determine what form or...
11
3643
by: Rohit | last post by:
Hi, Threads in the .NET Framework 1.1 (and possibly in 1.0 also) leak "Event" handles, by Event handles I mean Win32 Event handles which can be monitored using the ProcessExplorer from www.sysinternals.com, or even simply look at the Handle count in the good old windows TaskManager. To demonstrate the problem, all I did was created a basic Win Forms application and with Main implemented as:
2
5520
by: Wayne | last post by:
I have an application where I added a comment as the first line of my Config file, as I found out I can't do this. However, an entry was made in the event log about the error (Source = .net Runtime): The description for Event ID ( 0 ) in Source ( .NET Runtime ) cannot be found. The local computer may not have the necessary registry information or message DLL files to display messages from a remote computer. You may be able to use the...
4
2525
by: aaj | last post by:
Hi all I have an automated application, that runs in the middle of the night. If certain 'non system' errors occur (things like malformed files, missing files etc..), I send an automatic Email and write a record to the database. This is handled in a class. When these errors occur, once Emailed and written I want to just end the App, simple as that.
4
21493
by: brandon | last post by:
Anybody out there know how I could go about handling, in a systray'd app, the windows message that the desktop is going to be locked? This way the application can react whenever the Win+L or Ctrl+Alt+Del+L is used to lock the computer. I realize this will probably involve some Windows API work, and am aware of the LockWorkStation inside the User32.dll which causes the locking of the computer, but don't know where to start to find out how...
3
5541
by: geskerrett | last post by:
We have been asked to develop and application for a client that is a 'notification" system. We would like to use python, but are struggling to find the right starting point. Any suggestions, tips or sample code would be appreciated. Application outline; Machine A is running a "listener" application that is connected to a another device via the serial post and waits for events. We have not problem working with the serial port, or...
22
16943
by: dvestal | last post by:
Suppose I have this: class C { public delegate void MyEventHandler(); public event MyEventHandler MyEvent; public void foo() { MyEvent(); // NullReferenceException? } }
0
1618
by: marian.kovac | last post by:
Hi, I have this simple sample code: class SearchRange { static int m = 0; static void Main(string args) { Discover discover = new Discover(); // Add discovered device handler
6
11782
by: cnixuser | last post by:
Hello, I have a basic application written which is designed to data over a serial cable and then receive a response back. I am not getting any triggers to my data received event. I have tried connecting the pc I am running this application on to another PC which is also sending hex characters ;however, when I run the application I am getting no trigger of the data received event and am completely clueless as to why. Any help that could be...
0
9554
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10136
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9925
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9811
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5266
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3913
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.