0


the two virtual mouse image mission is move horizontal movement and click the destination point . in the following code is running successfully in Sequence (one by one ). i want to edit this code to working parallel (the two image working in the same time ) .

the mouseimg1 refer to => mouse 1 and mouseimg2 refer to => mouse 2 .

public partial class Form1 : Form
{
    public delegate void delMouse();
    public delMouse delMouse1; enter code here
    public delMouse delMouse2;
    private Thread t1, t2;
    public Form1()
    {
        InitializeComponent();
        delMouse1 = new delMouse(mouse1);
        delMouse2 = new delMouse(mouse2);


    }


    private void button1_Click(object sender, EventArgs e)
    {
        t1 = new Thread(new ThreadStart(delMouse1));
        t1.Start();

    }
    private void button2_Click(object sender, EventArgs e)
    {


        t2 = new Thread(new ThreadStart(delMouse2));
        t2.Start();

    }
    void mouse2()
    {
        if (this.mouseimg2.InvokeRequired)
        {
            this.Invoke(delMouse2);
        }
        else
        {
            int destinval2 = int.Parse(textBox2.Text);
            while (mouseimg2.Location.Y != destinval2)
            {
                if (mouseimg2.Location.Y == 250)
                    mouseimg2.Location = new Point(mouseimg2.Location.X, 15);
                if (mouseimg2.Location.Y < destinval2)
                    mouseimg2.Location = new Point(mouseimg2.Location.X, mouseimg2.Location.Y + 1);
                else
                    mouseimg2.Location = new Point(mouseimg2.Location.X, mouseimg2.Location.Y - 1);
            }
            LeftClick(mouseimg2.Location.X, mouseimg2.Location.Y);
        }
    }

    void mouse1()
    {
        if (this.mouseimg1.InvokeRequired)
        {
            this.Invoke(delMouse1);
        }
        else
        {
            int destinval1 = int.Parse(textBox1.Text);

            while (mouseimg1.Location.Y != destinval1)
            {

                if (mouseimg1.Location.Y < destinval1)
                    mouseimg1.Location = new Point(mouseimg1.Location.X, mouseimg1.Location.Y + 1);
                else
                    mouseimg1.Location = new Point(mouseimg1.Location.X, mouseimg1.Location.Y - 1);
            }
            LeftClick(mouseimg1.Location.X, mouseimg1.Location.Y);
        }
    }



           [DllImport("user32.dll")]
    static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);

    [Flags]
    public enum MouseEventFlags
    {
        LEFTDOWN = 0x00000002,
        LEFTUP = 0x00000004,
        MIDDLEDOWN = 0x00000020,
        MIDDLEUP = 0x00000040,
        MOVE = 0x00000001,
        ABSOLUTE = 0x00008000,
        RIGHTDOWN = 0x00000008,
        RIGHTUP = 0x00000010
    }
    public static void LeftClick(int x, int y)
    {
        Cursor.Position = new System.Drawing.Point(x, y);
        mouse_event((int)(MouseEventFlags.LEFTDOWN), 0, 0, 0, 0);
        mouse_event((int)(MouseEventFlags.LEFTUP), 0, 0, 0, 0);
    }


}


screenshot

Ahmed Samir
  • 53
  • 10

1 Answers1

1

In general in Windows you should not access a window from a thread other than the one which created it. WinForms enforces this rule, as you've found.

I'm not really clear what you want to do, but creating two threads is almost certainly the wrong way to go about it.

Behaviour that looks a bit like multithreading but isn't is often accomplished using timers, if that's at all helpful to you.

Will Dean
  • 39,055
  • 11
  • 90
  • 118