Bortek23
Bortek23

Reputation: 33

how to leave the loop when you click the mouse?

I have my loop and want to leave it when I click the mouse

do
{
}
while (!mouseClicked);

And I have my event

private void Form1_MouseClick(object sender, MouseEventArgs e) {
    clickedX = e.X;
    clickedY = e.Y;
    clickedX = MatchField(clickedX);
    clickedY = MatchField(clickedY);
    if (player1[clickedX, clickedY] == 0 && player2[clickedX, clickedY] == 0 && clickedX != 5 && clickedY != 5) mouseClicked = true;
}

How can I execute it at the same time ?

Upvotes: 1

Views: 116

Answers (1)

Theunis
Theunis

Reputation: 338

I believe this will work

public static bool MouseClicked { get; set; }
        static void Main(string[] args)
        {
            MouseClicked = false;
            Thread myClickedThread = new Thread(() =>
            {
                do
                {
                    //Your Code
                    //If this is a WPF application you will require a Dispacther.Invoke -> This is to access the main thread were the View resides.
                } while (!MouseClicked);
            });
            myClickedThread.Start(); //Remember to start the thread
        }

        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            MouseClicked = true;
            clickedX = e.X;
            clickedY = e.Y;
            clickedX = MatchField(clickedX);
            clickedY = MatchField(clickedY);
            if (player1[clickedX, clickedY] == 0 && player2[clickedX, clickedY] == 0 && clickedX != 5 && clickedY != 5) mouseClicked = true;
        }

Upvotes: 3

Related Questions