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 to disable event handler from its execution

How to disable event handler from its execution

Scheduled Pinned Locked Moved C#
csharphelptutorialquestion
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.
  • S Offline
    S Offline
    sandeepkavade
    wrote on last edited by
    #1

    Hi I am having button on form in C#. It does take 2-3 seconds to complete the operation meanwhile the button is disabled. But if the user clicks on that button when it is disabled, after completion of first operation it will again try to execute the handler again. how should I avoid the execution of event handler when the button is disabled? here is the code snippet: public partial class Form1 : Form { int i = 0; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { this.button1.Enabled = false; System.Threading.Thread.Sleep(3000); i++; this.label1.Text = "Total Calls: " + i.ToString(); this.button1.Enabled = true; } } when the button is disabled and you click on it, you can see the count gets increased. Please help

    L P L S 4 Replies Last reply
    0
    • S sandeepkavade

      Hi I am having button on form in C#. It does take 2-3 seconds to complete the operation meanwhile the button is disabled. But if the user clicks on that button when it is disabled, after completion of first operation it will again try to execute the handler again. how should I avoid the execution of event handler when the button is disabled? here is the code snippet: public partial class Form1 : Form { int i = 0; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { this.button1.Enabled = false; System.Threading.Thread.Sleep(3000); i++; this.label1.Text = "Total Calls: " + i.ToString(); this.button1.Enabled = true; } } when the button is disabled and you click on it, you can see the count gets increased. Please help

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You can unbind the click event at the time of disabling the button. Try following code.. button1.Click -= new System.EventHandler(this.button1_Click); And you can rebind the click even on making it enable. button1.Click += new System.EventHandler(this.button1_Click); Hope this will help!

      Jinal Desai - LIVE Experience is mother of sage....

      S 1 Reply Last reply
      0
      • L Lost User

        You can unbind the click event at the time of disabling the button. Try following code.. button1.Click -= new System.EventHandler(this.button1_Click); And you can rebind the click even on making it enable. button1.Click += new System.EventHandler(this.button1_Click); Hope this will help!

        Jinal Desai - LIVE Experience is mother of sage....

        S Offline
        S Offline
        sandeepkavade
        wrote on last edited by
        #3

        Tried but no success still the same behavior The event handler now looks like this: private void button1_Click(object sender, EventArgs e) { this.button1.Enabled = false; this.button1.Click -= new System.EventHandler(this.button1_Click); System.Threading.Thread.Sleep(3000); i++; this.label1.Text = "Total Calls: " + i.ToString(); this.button1.Enabled = true; this.button1.Click += new System.EventHandler(this.button1_Click); } still count is increasing if I click disabled button

        1 Reply Last reply
        0
        • S sandeepkavade

          Hi I am having button on form in C#. It does take 2-3 seconds to complete the operation meanwhile the button is disabled. But if the user clicks on that button when it is disabled, after completion of first operation it will again try to execute the handler again. how should I avoid the execution of event handler when the button is disabled? here is the code snippet: public partial class Form1 : Form { int i = 0; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { this.button1.Enabled = false; System.Threading.Thread.Sleep(3000); i++; this.label1.Text = "Total Calls: " + i.ToString(); this.button1.Enabled = true; } } when the button is disabled and you click on it, you can see the count gets increased. Please help

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          Add an if ( this.button1.Enabled ) ?

          S 1 Reply Last reply
          0
          • P PIEBALDconsult

            Add an if ( this.button1.Enabled ) ?

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

            Tried that also. The problem is that the event handler is getting called after first execution and at the end of it I am setting this.button1.Enabled = true; so this is not going to solve my problem..

            P 1 Reply Last reply
            0
            • S sandeepkavade

              Tried that also. The problem is that the event handler is getting called after first execution and at the end of it I am setting this.button1.Enabled = true; so this is not going to solve my problem..

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              Are you only clicking the button once? Have you attached the handler more than once?

              S 1 Reply Last reply
              0
              • P PIEBALDconsult

                Are you only clicking the button once? Have you attached the handler more than once?

                S Offline
                S Offline
                sandeepkavade
                wrote on last edited by
                #7

                I have not attached the handler more than once. After first click the button is disabled for 3 seconds when I click on the disabled button somewhere it is queuing that event and after execution of first event handler it is calling that event handler again that I don't want as I clicked on disabled button. I don't know how to attach the file here. PM provide me your email id I'll send you the test application I created to reproduce the problem.

                1 Reply Last reply
                0
                • S sandeepkavade

                  Hi I am having button on form in C#. It does take 2-3 seconds to complete the operation meanwhile the button is disabled. But if the user clicks on that button when it is disabled, after completion of first operation it will again try to execute the handler again. how should I avoid the execution of event handler when the button is disabled? here is the code snippet: public partial class Form1 : Form { int i = 0; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { this.button1.Enabled = false; System.Threading.Thread.Sleep(3000); i++; this.label1.Text = "Total Calls: " + i.ToString(); this.button1.Enabled = true; } } when the button is disabled and you click on it, you can see the count gets increased. Please help

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

                  Hi, it does not work like that. Those Enabled changes will take effect only after your handler finishes, and putting a Thread.Sleep in a handler is a big no no. A handler should never last more than some 10 milliseconds, your app's GUI should remain responsive at all times. What you need is either another thread (assuming the Sleep is emulating some real action), or a timer (assuming you really just want a delay). For timers, your best bet would be a System.Windows.Forms.Timer; you disable the button and launch the timer inside the button click handler, then re-enable the button and stop the timer inside the timer's tick handler. Works like a charm, without blocking anything but the button itself. :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                  I only read formatted code with indentation, so please use PRE tags for code snippets.


                  I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                  D 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    Hi, it does not work like that. Those Enabled changes will take effect only after your handler finishes, and putting a Thread.Sleep in a handler is a big no no. A handler should never last more than some 10 milliseconds, your app's GUI should remain responsive at all times. What you need is either another thread (assuming the Sleep is emulating some real action), or a timer (assuming you really just want a delay). For timers, your best bet would be a System.Windows.Forms.Timer; you disable the button and launch the timer inside the button click handler, then re-enable the button and stop the timer inside the timer's tick handler. Works like a charm, without blocking anything but the button itself. :)

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                    I only read formatted code with indentation, so please use PRE tags for code snippets.


                    I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                    D Offline
                    D Offline
                    dybs
                    wrote on last edited by
                    #9

                    Luc Pattyn wrote:

                    Those Enabled changes will take effect only after your handler finishes

                    Does this only apply to the Enabled property of controls, or others as well, and why wait until the handler completes? I've been seeing a different behavior for something like the ComobBox.SelectedIndex property. I have an app that, when you click a button, sets some ComboBox selections, does some processing, and maybe changes the ComboBox selection again (to indicate to the user that something is actually going on). This is all happenning in a function called by my button handler on the GUI thread, and I see the ComboBox values update while it runs. Yes, I know the fact that such a long procedure occurs in the click handler isn't the best solution (it was fine with the original program design, but we're in the process of moving this all out to a separate thread). Thanks, Dybs

                    The shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen

                    L 1 Reply Last reply
                    0
                    • S sandeepkavade

                      Hi I am having button on form in C#. It does take 2-3 seconds to complete the operation meanwhile the button is disabled. But if the user clicks on that button when it is disabled, after completion of first operation it will again try to execute the handler again. how should I avoid the execution of event handler when the button is disabled? here is the code snippet: public partial class Form1 : Form { int i = 0; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { this.button1.Enabled = false; System.Threading.Thread.Sleep(3000); i++; this.label1.Text = "Total Calls: " + i.ToString(); this.button1.Enabled = true; } } when the button is disabled and you click on it, you can see the count gets increased. Please help

                      S Offline
                      S Offline
                      Stanciu Vlad
                      wrote on last edited by
                      #10

                      How about using a flag in order to suppres the event?

                      private bool suppresEvent = false;
                      private void button1_Click(object sender, EventArgs e)
                      {
                      if(suppresEvent)
                      return;

                      suppresEvent = true;

                      // do your hard work here
                      System.Threading.Thread.Sleep(3000);

                      suppresEvent = false;
                      }

                      I have no smart signature yet...

                      1 Reply Last reply
                      0
                      • D dybs

                        Luc Pattyn wrote:

                        Those Enabled changes will take effect only after your handler finishes

                        Does this only apply to the Enabled property of controls, or others as well, and why wait until the handler completes? I've been seeing a different behavior for something like the ComobBox.SelectedIndex property. I have an app that, when you click a button, sets some ComboBox selections, does some processing, and maybe changes the ComboBox selection again (to indicate to the user that something is actually going on). This is all happenning in a function called by my button handler on the GUI thread, and I see the ComboBox values update while it runs. Yes, I know the fact that such a long procedure occurs in the click handler isn't the best solution (it was fine with the original program design, but we're in the process of moving this all out to a separate thread). Thanks, Dybs

                        The shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen

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

                        dybs wrote:

                        Does this only apply to the Enabled property of controls

                        I don't know; I don;t think it is documented, and I don't need to know as I keep my handlers short. :)

                        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                        I only read formatted code with indentation, so please use PRE tags for code snippets.


                        I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                        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