Event Handling in Console application?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • federico

    #1

    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


  • rowe_newsgroups

    #2
    Re: Event Handling in Console application?

    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]

    Comment

    • sloan

      #3
      Re: Event Handling in Console application?


      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" <rowe_email@yah oo.comwrote in message
      news:b99d1894-870a-491b-91cc-0738d1dde03f@j2 0g2000hsi.googl egroups.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]

      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: Event Handling in Console application?

        "federico" <federico@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/>

        Comment

        • federico

          #5
          Re: Event Handling in Console application?

          Thanks so much. The flag works nicely.


          "federico" <federico@micho sa.comwrote in message
          news:%23iXz3AwU IHA.4448@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
          >

          Comment

          Working...