Prevent command from running multiple times in a row, by fast clicking of user

Prevent command from running multiple times in a row, by fast clicking of user

Ertqwa
Advocate Advocate
1,126 Views
9 Replies
Message 1 of 10

Prevent command from running multiple times in a row, by fast clicking of user

Ertqwa
Advocate
Advocate

Hello Forum,

 

I have a command that initializes some stuff, which can take a few seconds, and then shows a dialog form.

Because the initialization can take a few seconds, the user can click the AutoCAD button (wich triggers the command) multiple times if he is fast enough. This results in being the command run multiple times in a row.

 

How can I prevent this?

 

I've tried checking "GetSystemVariable("CMDNAMES")" to see if the command is already running, but this does not work because the commands do not run simultaneously, but after each other.

 

Thank you.

0 Likes
1,127 Views
9 Replies
Replies (9)
Message 2 of 10

SENL1362
Advisor
Advisor
The CommandFlags.NoMultiple may prevent this:

[CommandMethod("Startup",CommandFlags.NoMultiple)]
0 Likes
Message 3 of 10

Ertqwa
Advocate
Advocate

Hello,

 

I've tried it, but the command flag 'NoMultiple' does not solve this problem:

 

"The CommandFlags.NoMultiple indicates that the command will not be possible to repeat itself through the MULTIPLE command."

 

Thank you for the response.

0 Likes
Message 4 of 10

SENL1362
Advisor
Advisor
ok, sorry about that, just a quick guess.
Other possibilities are a:
-check CMDACTIVE
-set a static (global) var when starting
0 Likes
Message 5 of 10

Ertqwa
Advocate
Advocate

Hello,

 

Doing some sort of check if the same command is already active, doesn't work in my case, because the commands run after each other, not simultaneously.

 

I have cancel in my button (^C^C) but that doesn't help, I think because the command shows a dialog form.

 

Thank you for the response.

0 Likes
Message 6 of 10

jeff
Collaborator
Collaborator

Can't you just use a static counter?

        public static int numOfTimes = 0;

        [CommandMethod("NumTest")]
        public void NumTest()
        {
            numOfTimes++;

            if (numOfTimes == 1)
            {
                Ed.WriteLine("First time ran");
            }
            Ed.WriteLine("{0} time ran", numOfTimes);
        }

 

You can also find your answers @ TheSwamp
0 Likes
Message 7 of 10

dgorsman
Consultant
Consultant

Or structure the function such that it first checks to see if the initialization is already done (presumably, data is added where it was missing before?) and only proceeds if initialization is required.

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


0 Likes
Message 8 of 10

Ertqwa
Advocate
Advocate

Hi,

 

I dont think I can use a counter, because I can't tell the difference between 2 situations:

- The user clicked the button fast 2 times in a row (the second time the command should not run).

- The user activated the command 2 times in a row on purpose (the second time the command should run).

 

Thank you for your response.

0 Likes
Message 9 of 10

Ertqwa
Advocate
Advocate

Hi,

 

The initialization needs to be done every time, because it shows files, and those files may have been altered between each session.

 

I will try to change the structure of the function in such a matter, that initialization is done in a later stage of the command, and the command is already running and the user can't click fast enough to activate it again  (but this is more of a 'trick' then a solution to my problem).

 

Thank you for your response.

0 Likes
Message 10 of 10

Anonymous
Not applicable

Would it help to change the cursor to a wait cursor and then back again with:

       System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor

''''' Your oode here

        System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default

 At the very least, it will notify the user that something is happening. Alternatively, open your dialog immediately with minimal data and update it once you've finished initializing it.

0 Likes