Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Imports System.Threading
Public Class Form1
   Dim counter As Integer
   Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       If counter = 0 Or counter = Nothing Then
           counter = 1
           Label2.Text = "ON"
           Label2.BackColor = Color.Lime

           Thread.Sleep(Integer.Parse(TextBox1.Text))
           macro()
       Else
           counter = 0
           Label2.Text = "OFF"
           Label2.BackColor = Color.Red
       End If
   End Sub

   Private Sub macro()
       Windows.Forms.Cursor.Position = New System.Drawing.Point(170, 340)
       SendMessage(&H2, 0, 0, 0)
       Thread.Sleep(200)
       SendMessage(&H4, 0, 0, 0)
   End Sub
End Class

 

Right now i can trigget my mouse to move by itself to the location i want, but i cannot trigger it to click and vs.net pop out an exception : PInvokeStackImbalance

 

How to fix this guys? :confused:

  • Administrators
Posted

Firstly change all the 'As Long' bits of your declare to 'As Integer' - that should remove the problem of it crashing.

 

You might find it easier to just use the Button1.PerformClick() method however to simulate a button click rather than relying on SendMessage.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted (edited)

Thanks! Wat i'm trying to do is not click any buttons on the form but clicking some other things outside of the application itself, so i need the mouse_event api

 

Oh yea one more thing... when i use thread.sleep i cannot seems to do anything to the form (e.g. i can't press button to stop it). Is there any idea i can fix this?

Edited by Souma
Posted
Yea thanks for the resource! mouse_event works fine anyway, but how can i fix the thread.sleep problem that causes my app to stop responding while the thread is running?
  • Administrators
Posted

Don't sleep on the UI thread is the simple answer, if the UI thread is asleep it cannot respond to any input therefore it stops responding.

 

Ideally you would implement some other mechanism here - a timer + callback would possibly be the best choice, or you could always perform a loop with a Application.DoEvents() inside it but this is a poor technique as it can burn a lot of CPU time to do nothing at all.

 

Out of curiosity is there a reason for the Sleep being there?

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

lol threading eats cpu... ur idea also eats cpu... wat's the point :D

 

yea i making my own macro program that do interval clicking on other applications buttons, so the first thing that comes to my mind is using thread.sleep to so call "pause" for a specific time between the next action.

 

is there any other way? geez... if u ever played with macro programs(e.g. Macro Express, AutoIt, ACTool) you should know they have delay function to pause the action for a specified time... that's wat i wanted to achieve

Posted

I miss the old win3.1 macro recorder :(.

 

Using doevents in a loop would keep the cpu at 100% load so that would be 'less than ideal'. Using timer+callback is a bit harder to code but would be very light on the cpu. with almost 0 overhead.

 

Another way could be to put the 'macro execution' part of your code in a separate thread. That way if it goes to sleep the UI thread keeps running. Although this might cause problems if you update the UI from the 'macro execution'.

Nothing is as illusive as 'the last bug'.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...