Solved

Generate clicks for winform app

Posted on 2012-03-25
7
552 Views
Last Modified: 2012-03-27
I have a winform app (C# 4.0). i'm testing a race condition and need to click repeatedly very fast on a form panel, faster than I can click on my mouse. Is there a way to do this?

This click utility should be external to the program itself, since I'll need to run it on the current production version as well as the corrected development version. If the solution involves writing a separate program, would prefer C#.
0
Comment
Question by:BlearyEye
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 350 total points
ID: 37762902
Have you heard of AutoIT? I just tested it out for your specific scenario. It would be one less thing you have to write (well, you'd only have to write a quick script for the clicking).

To simulate your example, I wrote the following in AutoIT's editor:

For $i = 0 To 10000
  ControlClick("Form1", "", "[ID:329122]")
Next

Open in new window


In my test, my form had the title (Text property) of "Form1". You would change this to exactly match the text of your form. The ID was attained by using the Window Info tool which comes with AutoIT:

Window Info Tool
Within this tool, there is a small target icon. If you've ever used Spy++, then you should have an idea of what this feature does. If you haven't, then you can actually click and drag the little target to any form on your desktop. As you mouse over a form, the border of that form will be highlighted. This will be your active target. Further, you can select specific controls from a form. Likewise to the form, a control's border will highlight when you mouse over it. When you let go of your mouse button, the currently highlighted control's information will be populated within the Window Info window.

The "Title" field is the same text as what I mentioned above for the form, so you could also use this tool to grab that value as well. If you switch to the "Control" tab, as I have in the screenshot above, you will get access to the ID property for the control you selected. This value is what you would put in the script above.

The above script is going to execute "faster than [you] can click with a mouse," and if you need to slow it down a bit, you can include a Sleep call in your script.

e.g.

For $i = 0 To 10
  ControlClick("Form1", "", "[ID:656868]")
  Sleep(1000) ' in milliseconds
Next

Open in new window


Realize that the scripting language is VB/VBA-esque. The tool has decent documentation, and since it's similar to VB, you can adapt a lot of VB code to be used within the tool. The tool itself is pretty popular, so even if you get stuck, you should be able to find help with any problems you encounter.

At least it's an option  = )
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 150 total points
ID: 37762917
If you want to do it manually, then you'll need to obtain a handle the panel first.  Do you know how to that?

Assuming so, then you'd use SendMessage() to send a mouse down message (and possibly a follow-up mouse up message depending on what you're doing):

        private const int WM_LBUTTONDOWN = 0x201;

        [DllImport("User32.DLL")]
        private static extern int SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        private void button1_Click(object sender, EventArgs e)
        {
            SendMessage(panel1.Handle, WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
        }
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37762934
For Idle_Mind's method, I believe you would also need FindWindow (maybe FindWindowEx) since you mention the click utility's "external" requirement.
0
Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

 
LVL 40
ID: 37763553
If this apply to your situation, typically if no other mouse events are involved, you can call the click event from inside the application itself.

Simply add a button or menu on the form to start your test, something that is visible only by an administrator or through a back door if you do not want the users to see it.

Prepare a loop, maybe with a short Sleep instruction to be closer to a reall life situation, and call your event handler in every loop:

Button1_Click (null, null);
0
 
LVL 1

Author Comment

by:BlearyEye
ID: 37771808
Am checking into AutoIT ... JamesBurger: can't add a button to the app since I need to test a deployed version.
0
 
LVL 40
ID: 37771852
As I told you, the button can be hidden by a backdoor so that only you have access to it for your tests.

A nice thing about that feature is that you can still test at the users station after the application is deployed if you find problems on some of the computers. The user will never see the button. And a hidden button or menu with the simple code that the test would require does not add a significant overhead to the application.
0
 
LVL 1

Author Closing Comment

by:BlearyEye
ID: 37775187
After a bit of effort, AutoIT did the trick. Nice tool. I had to use MouseClick rather than ControlClick, for some reason that I did not quite divine; the control in question has a MouseDown event that changes the focus, as well as a Click event.

Idle_Mind's solution would also have worked, tho it would have required a C# app which is a bit more complex.
0

Featured Post

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question