Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How can I flush events in .Net?

How can I flush events in .Net?

Scheduled Pinned Locked Moved C#
questioncsharpdotnetcomdata-structures
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JoeRip
    wrote on last edited by
    #1

    I'm looking for something like the old Windows API FlushEvents(). I have a form which creates a COM object like such: COMServerObject foo = new COMServerObject; While my code waits for this singular statement to return, Windows makes my form window modal. If it takes a while for this statement to return - up to 20 seconds for .NET to give up and throw an exception - Windows queues up mouse clicks and keyboard events on my form, and then plays them back once the statement returns. This is nasty, as the user hammers away at my unresponsive window, even trying to close it. So once the statement returns, my window can suddenly fly around the screen, resize, minimize, and even close. I've tried disabling the Form while while I wait: ... this.Enabled = false; COMServerObject foo = new COMServerObject; this.Enabled = true; ... And this prevents clicks on my various buttons and menus. However, it does NOT prevent clicks on the window frame or title bar or close controls, so they still get queued up. How can I prevent these from queuing? In the old Windows API, you could call FlushEvents when you got back from a modal state, which would remove all Windows events from it's message queue. I can't find anything analogous in the .Net framework. Anybody know the proper technique, here?

    N 1 Reply Last reply
    0
    • J JoeRip

      I'm looking for something like the old Windows API FlushEvents(). I have a form which creates a COM object like such: COMServerObject foo = new COMServerObject; While my code waits for this singular statement to return, Windows makes my form window modal. If it takes a while for this statement to return - up to 20 seconds for .NET to give up and throw an exception - Windows queues up mouse clicks and keyboard events on my form, and then plays them back once the statement returns. This is nasty, as the user hammers away at my unresponsive window, even trying to close it. So once the statement returns, my window can suddenly fly around the screen, resize, minimize, and even close. I've tried disabling the Form while while I wait: ... this.Enabled = false; COMServerObject foo = new COMServerObject; this.Enabled = true; ... And this prevents clicks on my various buttons and menus. However, it does NOT prevent clicks on the window frame or title bar or close controls, so they still get queued up. How can I prevent these from queuing? In the old Windows API, you could call FlushEvents when you got back from a modal state, which would remove all Windows events from it's message queue. I can't find anything analogous in the .Net framework. Anybody know the proper technique, here?

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      JoeRip wrote:

      Anybody know the proper technique, here?

      Yeah, some asynchronous coding methods. Put the call to your COM object in background thread so the primary thread is free to handle the events.


      only two letters away from being an asset

      J 1 Reply Last reply
      0
      • N Not Active

        JoeRip wrote:

        Anybody know the proper technique, here?

        Yeah, some asynchronous coding methods. Put the call to your COM object in background thread so the primary thread is free to handle the events.


        only two letters away from being an asset

        J Offline
        J Offline
        JoeRip
        wrote on last edited by
        #3

        So, nobody knows any way to flush events, then?

        L 1 Reply Last reply
        0
        • J JoeRip

          So, nobody knows any way to flush events, then?

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          With a decent design, nobody ever needs to flush events. An app is not supposed to ignore its user, if it can't or won't respond to some event, it should disable the event sources to indicate so. :|

          Luc Pattyn [Forum Guidelines] [My Articles]


          Happy Holidays!


          J 1 Reply Last reply
          0
          • L Luc Pattyn

            With a decent design, nobody ever needs to flush events. An app is not supposed to ignore its user, if it can't or won't respond to some event, it should disable the event sources to indicate so. :|

            Luc Pattyn [Forum Guidelines] [My Articles]


            Happy Holidays!


            J Offline
            J Offline
            JoeRip
            wrote on last edited by
            #5

            Luc Pattyn wrote:

            With a decent design, nobody ever needs to flush events.

            This is an overly simplistic statement. Both the Mac OS and Win32 API's support a Flush Events procedure, which is recommended to prevent users from inadvertently losing work or destroying data. In the professional productivity applications that I was personally involved in shipping, we frequently flushed queued up events when a dialog opened, to prevent a user who was typing in word processor from overwriting original values in the dialog controls without intending to do so. While you may not agree that the situation I described needs an event flush, your statement that it is never necessary is incorrect.

            L 1 Reply Last reply
            0
            • J JoeRip

              Luc Pattyn wrote:

              With a decent design, nobody ever needs to flush events.

              This is an overly simplistic statement. Both the Mac OS and Win32 API's support a Flush Events procedure, which is recommended to prevent users from inadvertently losing work or destroying data. In the professional productivity applications that I was personally involved in shipping, we frequently flushed queued up events when a dialog opened, to prevent a user who was typing in word processor from overwriting original values in the dialog controls without intending to do so. While you may not agree that the situation I described needs an event flush, your statement that it is never necessary is incorrect.

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              Well, if you want your app to suddenly open a dialog, give it focus, and hijack all on-going user input, then you are on your own I guess. I will try and avoid ever using such an app, the way I see it the user is in control, not the app. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              Happy Holidays!


              J 1 Reply Last reply
              0
              • L Luc Pattyn

                Well, if you want your app to suddenly open a dialog, give it focus, and hijack all on-going user input, then you are on your own I guess. I will try and avoid ever using such an app, the way I see it the user is in control, not the app. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                Happy Holidays!


                J Offline
                J Offline
                JoeRip
                wrote on last edited by
                #7

                How bizarre that you would suggest that I want this. Where are you reading that I want an application to suddenly open a dialog? Can you not simply admit that your statement was overly simplistic and incorrect?

                M 1 Reply Last reply
                0
                • J JoeRip

                  How bizarre that you would suggest that I want this. Where are you reading that I want an application to suddenly open a dialog? Can you not simply admit that your statement was overly simplistic and incorrect?

                  M Offline
                  M Offline
                  Mark Churchill
                  wrote on last edited by
                  #8

                  Hrmmm I should chuck my 2c in here. Sometimes (probably due to a less than ideal architecture) you may find a whole bunch of events in your queue. The correct behaviour here is to process the events. You may choose to fast-discard keypresses and clicks - but the decision to flush the queue may cause your application to lose critical information. You could lose network connectivity change notifications, system shutdown notifications... Particularily if you are intending to have your application run on Vista - which is very very quick to detect applications not pumping their message queue, and very quick to suggest they are hanging and should be closed. So I don't think it was "overly simplistic and incorrect". It was good advice, on par with "do not call GC.Collect()". Sure - you could think up a scenario where it might be useful - but in 99% of cases you should just do it right. If you need to flush your message queue then use PInvoke to call the API function. If the framework supports this it will be in the Application or Form class. It probably doesnt, because its bad practice. HTH

                  Mark Churchill Director Dunn & Churchill Diamond Binding: Zero to Data Layer in 3 mins

                  J 1 Reply Last reply
                  0
                  • M Mark Churchill

                    Hrmmm I should chuck my 2c in here. Sometimes (probably due to a less than ideal architecture) you may find a whole bunch of events in your queue. The correct behaviour here is to process the events. You may choose to fast-discard keypresses and clicks - but the decision to flush the queue may cause your application to lose critical information. You could lose network connectivity change notifications, system shutdown notifications... Particularily if you are intending to have your application run on Vista - which is very very quick to detect applications not pumping their message queue, and very quick to suggest they are hanging and should be closed. So I don't think it was "overly simplistic and incorrect". It was good advice, on par with "do not call GC.Collect()". Sure - you could think up a scenario where it might be useful - but in 99% of cases you should just do it right. If you need to flush your message queue then use PInvoke to call the API function. If the framework supports this it will be in the Application or Form class. It probably doesnt, because its bad practice. HTH

                    Mark Churchill Director Dunn & Churchill Diamond Binding: Zero to Data Layer in 3 mins

                    J Offline
                    J Offline
                    JoeRip
                    wrote on last edited by
                    #9

                    All arguments aside, here's an odd behavior: Application.DoEvents() basically does the flush for me. If I do this: myForm.Enabled = false; COMServerObject foo = new COMServerObject(); Application.DoEvents(); myForm.Enabled = true; Then the queued clicks on the titlebar/closebox/minimize box, etc, do NOT get received by my app. If I reverse the order of the last two statement (enable the form, then call DoEvents()), then the messages DO get received by my app. Weird. Noted just for interest. Not advocating this as a method to flush events.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups