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. events

events

Scheduled Pinned Locked Moved C#
question
11 Posts 6 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.
  • M Offline
    M Offline
    michaelgr1
    wrote on last edited by
    #1

    Hello, If i my application handles an event in the middle of other code running, does it return to previously running code after the event was handled? if not, how can i do it?

    S L B 3 Replies Last reply
    0
    • M michaelgr1

      Hello, If i my application handles an event in the middle of other code running, does it return to previously running code after the event was handled? if not, how can i do it?

      S Offline
      S Offline
      SledgeHammer01
      wrote on last edited by
      #2

      If you are writing a GUI application, the main GUI thread is event driven and will handle an event and execute all code for that handler before returning to the code that raised the event. If you are writing a multithreaded application, the background thread works independently of the main thread and would not be interrupted unless its listening to events as well.

      L 1 Reply Last reply
      0
      • M michaelgr1

        Hello, If i my application handles an event in the middle of other code running, does it return to previously running code after the event was handled? if not, how can i do it?

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

        1. your threads do whatever you make them do, and will continue to do so. And events get handled by either the main aka GUI thread, or one of the threadpool threads. See here[^]. 2. If you have long winding code that executes on the main thread (e.g. in a button click handler), then other GUI events (and Windows.Forms.Timer tick events, and more) will be stalled. That basically is why long winding operations should NOT be executed by the main thread. 3. If other threads need to access WinForm GUI components (Controls), you need to use Invoke. See here[^]. :)

        Luc Pattyn [My Articles] Nil Volentibus Arduum


        Fed up by FireFox memory leaks I switched to Opera and now CP doesn't perform its paste magic, so links will not be offered. Sorry.

        1 Reply Last reply
        0
        • S SledgeHammer01

          If you are writing a GUI application, the main GUI thread is event driven and will handle an event and execute all code for that handler before returning to the code that raised the event. If you are writing a multithreaded application, the background thread works independently of the main thread and would not be interrupted unless its listening to events as well.

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

          AFAICT your suggestion has two major drawbacks, memory-wise: 1. it repeats the leading symbols over and over; 2. for each new chunk of license plates it costs a pointer. I'd consider an N-tier approach; for N=2 that would be some data structure for the rightmost say 3 symbols, and then a second layer of structure to hold all of those, representing the remaining symbols. The first one could be a run-length as you described, or a linked list, or switch from one to the other scheme depending on the population. The second one could be a full array, or maybe a dictionary. Maybe a 3-tier approach would be optimal: an array for 2 symbols (36^2 elements), pointing to an array for 2 symbols, pointing to an array for 2 symbols, holding a 36-bit bitmask for the last symbol. (Yes it would work better if there were only 32 symbols in the alphabet used). Note: the "pointers" above don't have to be real pointers, they could be indexes into a huge array, requiring say 4B each rather than 8B. And the simplest approach that might be good enough would be a Dictionary<string,long> where the key holds the first N-1 symbols, and the value is a bitmask covering the last symbol. :) PS: this is what I was going to reply to your vanishing question in Algos

          Luc Pattyn [My Articles] Nil Volentibus Arduum


          Fed up by FireFox memory leaks I switched to Opera and now CP doesn't perform its paste magic, so links will not be offered. Sorry.

          S 1 Reply Last reply
          0
          • L Luc Pattyn

            AFAICT your suggestion has two major drawbacks, memory-wise: 1. it repeats the leading symbols over and over; 2. for each new chunk of license plates it costs a pointer. I'd consider an N-tier approach; for N=2 that would be some data structure for the rightmost say 3 symbols, and then a second layer of structure to hold all of those, representing the remaining symbols. The first one could be a run-length as you described, or a linked list, or switch from one to the other scheme depending on the population. The second one could be a full array, or maybe a dictionary. Maybe a 3-tier approach would be optimal: an array for 2 symbols (36^2 elements), pointing to an array for 2 symbols, pointing to an array for 2 symbols, holding a 36-bit bitmask for the last symbol. (Yes it would work better if there were only 32 symbols in the alphabet used). Note: the "pointers" above don't have to be real pointers, they could be indexes into a huge array, requiring say 4B each rather than 8B. And the simplest approach that might be good enough would be a Dictionary<string,long> where the key holds the first N-1 symbols, and the value is a bitmask covering the last symbol. :) PS: this is what I was going to reply to your vanishing question in Algos

            Luc Pattyn [My Articles] Nil Volentibus Arduum


            Fed up by FireFox memory leaks I switched to Opera and now CP doesn't perform its paste magic, so links will not be offered. Sorry.

            S Offline
            S Offline
            SledgeHammer01
            wrote on last edited by
            #5

            Lol... saw that forum was dead, so I moved it over here :).

            L 1 Reply Last reply
            0
            • S SledgeHammer01

              Lol... saw that forum was dead, so I moved it over here :).

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

              That is exactly how forums die. :doh:

              Luc Pattyn [My Articles] Nil Volentibus Arduum


              Fed up by FireFox memory leaks I switched to Opera and now CP doesn't perform its paste magic, so links will not be offered. Sorry.

              B 1 Reply Last reply
              0
              • M michaelgr1

                Hello, If i my application handles an event in the middle of other code running, does it return to previously running code after the event was handled? if not, how can i do it?

                B Offline
                B Offline
                Bernhard Hiller
                wrote on last edited by
                #7

                Beware of Application.DoEvents()! Using such a terrible line of code can cause chaos, e.g. a second click on the same button can "overtake" the first click somewhere in the middle of execution. While you can avoid those calls in your code, you'll never be safe when you have to use ThirdParty (often closed-source) code.

                M 1 Reply Last reply
                0
                • L Luc Pattyn

                  That is exactly how forums die. :doh:

                  Luc Pattyn [My Articles] Nil Volentibus Arduum


                  Fed up by FireFox memory leaks I switched to Opera and now CP doesn't perform its paste magic, so links will not be offered. Sorry.

                  B Offline
                  B Offline
                  BillWoodruff
                  wrote on last edited by
                  #8

                  Luc Pattyn wrote:

                  That is exactly how forums die.

                  "Not with a ! (bang), but by forum transmigration," T.S. Elliot did not write in "Hollow Men."

                  "Our life is a faint tracing on the surface of mystery, like the idle, curved tunnels of leaf miners on the surface of a leaf. We must somehow take a wider view, look at the whole landscape, really see it, and describe what's going on here. Then we can at least wail the right question into the swaddling band of darkness, or, if it comes to that, choir the proper praise." Annie Dillard

                  1 Reply Last reply
                  0
                  • B Bernhard Hiller

                    Beware of Application.DoEvents()! Using such a terrible line of code can cause chaos, e.g. a second click on the same button can "overtake" the first click somewhere in the middle of execution. While you can avoid those calls in your code, you'll never be safe when you have to use ThirdParty (often closed-source) code.

                    M Offline
                    M Offline
                    michaelgr1
                    wrote on last edited by
                    #9

                    ok. can i raise event in a thread (Task) and handle this event in the main thread? so task will keep running when event is handled?

                    B 1 Reply Last reply
                    0
                    • M michaelgr1

                      ok. can i raise event in a thread (Task) and handle this event in the main thread? so task will keep running when event is handled?

                      B Offline
                      B Offline
                      BobJanova
                      wrote on last edited by
                      #10

                      Not exactly (event handlers are always called in the same thread that called them) but if you're talking about a GUI application you can have event handlers that marshal the call to the UI thread if they're called on a different one:

                      void MyEventHandler(object sender, EventArgs ea){
                      if(InvokeRequired) {
                      BeginInvoke(new EventHandler(MyEventHandler), new object[] { sender, ea });
                      return;
                      }

                      // ... normal, in-UI-thread code here
                      }

                      Using BeginInvoke (instead of Invoke) causes the call to be asynchronous, i.e. the calling thread continues executing while the event handler runs. This is what you requested, but be aware of synchronisation issues with any form of parallel execution. Because you usually don't care about the result of a delegate call, you can ignore the return value and not bother calling EndInvoke at any point.

                      M 1 Reply Last reply
                      0
                      • B BobJanova

                        Not exactly (event handlers are always called in the same thread that called them) but if you're talking about a GUI application you can have event handlers that marshal the call to the UI thread if they're called on a different one:

                        void MyEventHandler(object sender, EventArgs ea){
                        if(InvokeRequired) {
                        BeginInvoke(new EventHandler(MyEventHandler), new object[] { sender, ea });
                        return;
                        }

                        // ... normal, in-UI-thread code here
                        }

                        Using BeginInvoke (instead of Invoke) causes the call to be asynchronous, i.e. the calling thread continues executing while the event handler runs. This is what you requested, but be aware of synchronisation issues with any form of parallel execution. Because you usually don't care about the result of a delegate call, you can ignore the return value and not bother calling EndInvoke at any point.

                        M Offline
                        M Offline
                        michaelgr1
                        wrote on last edited by
                        #11

                        OK. So what will happen if i call an event that is handled in the main thread (not UI thread)?

                        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