Strange Situation

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

    #1

    Strange Situation

    Hi

    I have occured a very strange situation.

    The scenario is as follows. I have two buttons in the form. First
    button is Load button and the second one is Delete button. As the name
    suggests when 'Load' button is clicked, some data is loaded on the
    screen. When the Delete button is hit, this data is removed and the
    user does not have access to it further. Load usually takes time and in
    some extreme situations it takes 4-5 seconds to load the complete data.
    User is not allowed to have Load and Delete operation being executed
    simultaneously. Hence while the Load processing is going on, the Delete
    button is disabled. Even though the user clicks on the disabled
    (Delete) button, its handler is executed and the data is removed from
    the screen.

    The code in Load button click event handler looks like this:

    void Load_Click()
    {
    DeleteButton.En abled = false;
    ....
    ....
    //Do heavy processing here
    ....
    ....
    DeleteButton.En abled = false;
    }


    Now consider this scenario. The user pressed Load button. It took 5
    seconds to processing. In the first line, the Load button is disabled
    and the button is indeed disabled visibly.

    Within the 5 second time (i.e. during the Delete button is disabled),
    the user clicks on the Delete button.

    Now, the windows puts this Delete_Click event in the queue. After the
    processing for Load_Click is done, it handles Delete_Click event. At
    this time, it finds that the DeleteButton.En abled is true (it was set
    to true in the last statement of Load_Click). And hence it happily
    executes the Delete_Click event handler and removes all the data from
    the screen!!

    Can anyone tell, how to resolve this situation? I require that the user
    clicks should be ignored when the button is disabled.

    Best regards
    Amit Dedhia

  • Jon Skeet [C# MVP]

    #2
    Re: Strange Situation

    Amit <amitdedhia@yah oo.com> wrote:

    <snip>
    [color=blue]
    > Can anyone tell, how to resolve this situation? I require that the user
    > clicks should be ignored when the button is disabled.[/color]

    Sure - don't do your heavy processing within the UI thread. During that
    time, the clicks aren't actually being processed, they're just being
    queued up. By the time they're processed, the delete button is enabled
    again.

    You should do the heavy processing in another thread, which calls back
    to the UI thread to re-enable the delete button afterwards.

    See http://www.pobox.com/~skeet/csharp/t...winforms.shtml for more
    details.

    --
    Jon Skeet - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too

    Comment

    • Paul Henderson

      #3
      Re: Strange Situation

      Perhaps actually hide the button during processing, rather than just
      disabling it...

      Comment

      • Registered User

        #4
        Re: Strange Situation

        On 30 Dec 2005 03:06:25 -0800, "Amit" <amitdedhia@yah oo.com> wrote:
        [color=blue]
        >Hi
        >
        >I have occured a very strange situation.
        >
        >The scenario is as follows. I have two buttons in the form. First
        >button is Load button and the second one is Delete button. As the name
        >suggests when 'Load' button is clicked, some data is loaded on the
        >screen. When the Delete button is hit, this data is removed and the
        >user does not have access to it further. Load usually takes time and in
        >some extreme situations it takes 4-5 seconds to load the complete data.
        >User is not allowed to have Load and Delete operation being executed
        >simultaneously . Hence while the Load processing is going on, the Delete
        >button is disabled. Even though the user clicks on the disabled
        >(Delete) button, its handler is executed and the data is removed from
        >the screen.
        >
        >The code in Load button click event handler looks like this:
        >
        >void Load_Click()
        >{
        > DeleteButton.En abled = false;
        >...
        >...
        >//Do heavy processing here
        >...
        >...
        > DeleteButton.En abled = false;
        >}
        >
        >
        >Now consider this scenario. The user pressed Load button. It took 5
        >seconds to processing. In the first line, the Load button is disabled
        >and the button is indeed disabled visibly.
        >
        >Within the 5 second time (i.e. during the Delete button is disabled),
        >the user clicks on the Delete button.
        >
        >Now, the windows puts this Delete_Click event in the queue. After the
        >processing for Load_Click is done, it handles Delete_Click event. At
        >this time, it finds that the DeleteButton.En abled is true (it was set
        >to true in the last statement of Load_Click). And hence it happily
        >executes the Delete_Click event handler and removes all the data from
        >the screen!!
        >
        >Can anyone tell, how to resolve this situation? I require that the user
        >clicks should be ignored when the button is disabled.
        >[/color]
        When the button is disabled remove the OnClick event handler, this
        should be in a try block. In the finally block re-enable the control
        and re-assign the event handler.

        One thing to question is why the UI and processing code are
        co-mingled. The two types of functionality call out for two distinct
        objects.

        regards
        A.G.

        Comment

        • Ignacio Machin \( .NET/ C# MVP \)

          #5
          Re: Strange Situation

          Hi,

          "Amit" <amitdedhia@yah oo.com> wrote in message
          news:1135940785 .026950.40500@o 13g2000cwo.goog legroups.com...

          [color=blue]
          >
          > Can anyone tell, how to resolve this situation? I require that the user
          > clicks should be ignored when the button is disabled.[/color]

          Why dont you just use a WaitCursor ? in this way your form cannot receive
          any click.

          Also, consider a more userfriendly interface, like using a thread to load
          the data this will prevent the locking of the UI



          --
          Ignacio Machin,
          ignacio.machin AT dot.state.fl.us
          Florida Department Of Transportation


          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Strange Situation

            <"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.mach in AT
            dot.state.fl.us >> wrote:[color=blue][color=green]
            > > Can anyone tell, how to resolve this situation? I require that the user
            > > clicks should be ignored when the button is disabled.[/color]
            >
            > Why dont you just use a WaitCursor ? in this way your form cannot receive
            > any click.[/color]

            Yes it can. Try this:

            using System;
            using System.Windows. Forms;
            using System.Drawing;
            using System.Threadin g;

            public class Test
            {
            static int count;

            [STAThread]
            static void Main()
            {
            Form f = new Form();
            f.Size = new Size (200, 200);
            f.Location = new Point (100, 100);

            Button b = new Button();
            b.Size = new Size (190, 190);
            b.Location = new Point (0, 10);
            b.Text = "Click me";
            b.Click += new EventHandler(Cl ickHandler);

            f.Controls.Add( b);

            Application.Run (f);
            }

            static void ClickHandler(ob ject sender, EventArgs e)
            {
            Control c = (Control) sender;
            count++;
            c.Text = count.ToString( );
            Cursor.Current = Cursors.WaitCur sor;
            Thread.Sleep (2000);
            }
            }

            Start this up, and then click rapidly times on the button, several
            times. You'll see the event getting fired multiple times, even though
            most of the clicks are performed when the cursor is a wait cursor.
            [color=blue]
            > Also, consider a more userfriendly interface, like using a thread to load
            > the data this will prevent the locking of the UI[/color]

            That's the right answer :)

            --
            Jon Skeet - <skeet@pobox.co m>
            http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
            If replying to the group, please do not mail me too

            Comment

            • Ignacio Machin \( .NET/ C# MVP \)

              #7
              Re: Strange Situation

              Hi,

              [color=blue]
              > Start this up, and then click rapidly times on the button, several
              > times. You'll see the event getting fired multiple times, even though
              > most of the clicks are performed when the cursor is a wait cursor.[/color]

              Interesting, I had never tried that

              I was about to post it when I though that the above problem will happens no
              matter what. if you are quick enough you can send a couple of events to the
              queue before the handler is even invoked.


              [color=blue][color=green]
              >> Also, consider a more userfriendly interface, like using a thread to load
              >> the data this will prevent the locking of the UI[/color]
              >
              > That's the right answer :)[/color]

              More like part of it, still the UI needs to reflect the fact that an action
              is being performed in the background at least to disable/enable the controls
              accordingly.


              --
              Ignacio Machin,
              ignacio.machin AT dot.state.fl.us
              Florida Department Of Transportation


              Comment

              • Ignacio Machin \( .NET/ C# MVP \)

                #8
                OT for Jon

                Hey Jon,

                If you are in UK it should be night already , why aren;t you drinking a
                pint?

                It's just 2PM here and I have one more hour to leave :(




                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: Strange Situation

                  <"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.mach in AT
                  dot.state.fl.us >> wrote:[color=blue][color=green]
                  > > Start this up, and then click rapidly times on the button, several
                  > > times. You'll see the event getting fired multiple times, even though
                  > > most of the clicks are performed when the cursor is a wait cursor.[/color]
                  >
                  > Interesting, I had never tried that
                  >
                  > I was about to post it when I though that the above problem will happens no
                  > matter what. if you are quick enough you can send a couple of events to the
                  > queue before the handler is even invoked.[/color]

                  Yes, that's true. Possibly the easiest way to solve that problem is to
                  check whether the "sender" of the event is enabled, and to ignore it if
                  it's not. I *think* that would work...
                  [color=blue][color=green][color=darkred]
                  > >> Also, consider a more userfriendly interface, like using a thread to load
                  > >> the data this will prevent the locking of the UI[/color]
                  > >
                  > > That's the right answer :)[/color]
                  >
                  > More like part of it, still the UI needs to reflect the fact that an action
                  > is being performed in the background at least to disable/enable the controls
                  > accordingly.[/color]

                  Oh certainly.

                  --
                  Jon Skeet - <skeet@pobox.co m>
                  http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                  If replying to the group, please do not mail me too

                  Comment

                  • Jon Skeet [C# MVP]

                    #10
                    Re: OT for Jon

                    <"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.mach in AT
                    dot.state.fl.us >> wrote:[color=blue]
                    > If you are in UK it should be night already , why aren;t you drinking a
                    > pint?[/color]

                    I'm still on Christmas holiday - back to work on Tuesday.
                    [color=blue]
                    > It's just 2PM here and I have one more hour to leave :([/color]

                    LOL. These days I post more from home than from work anyway - hence my
                    rather higher number of posts this month than normal. (I don't know
                    what Nick's excuse is :)

                    --
                    Jon Skeet - <skeet@pobox.co m>
                    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                    If replying to the group, please do not mail me too

                    Comment

                    • Nicholas Paldino [.NET/C# MVP]

                      #11
                      Re: OT for Jon

                      Don't let Jon tell you that he doesn't go out for a pint. I know first
                      hand that he can handle it.

                      As for my excuse, I like to let the computer screen warm me on those
                      cold, lonely nights. =)


                      --
                      - Nicholas Paldino [.NET/C# MVP]
                      - mvp@spam.guard. caspershouse.co m

                      "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                      news:MPG.1e1fa2 2ee8e9abbf98cc2 e@msnews.micros oft.com...[color=blue]
                      > <"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.mach in AT
                      > dot.state.fl.us >> wrote:[color=green]
                      >> If you are in UK it should be night already , why aren;t you drinking a
                      >> pint?[/color]
                      >
                      > I'm still on Christmas holiday - back to work on Tuesday.
                      >[color=green]
                      >> It's just 2PM here and I have one more hour to leave :([/color]
                      >
                      > LOL. These days I post more from home than from work anyway - hence my
                      > rather higher number of posts this month than normal. (I don't know
                      > what Nick's excuse is :)
                      >
                      > --
                      > Jon Skeet - <skeet@pobox.co m>
                      > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                      > If replying to the group, please do not mail me too[/color]


                      Comment

                      Working...