1

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 ?

Jenish Rabadiya
  • 6,708
  • 6
  • 33
  • 62
Bortek23
  • 33
  • 3
  • 4
    Why do you want to do that? Why do you have a loop at all? Why don't you have code on an event that reacts to the mouse click instead? – Lasse V. Karlsen Dec 07 '15 at 13:19
  • Execute which at the same time? –  Dec 07 '15 at 13:22
  • @LasseV.Karlsen - this code may seem strange at first, but may in fact be a perfectly reasonable approach depending on the context. He *does* have an event that reacts to a mouse click. If he needs something to loop in the mean time this code is ok. –  Dec 07 '15 at 13:22
  • 1
    it would help if you share whats inside the loop. maybe we can help you fit it elsewhere – Banana Dec 07 '15 at 13:23
  • I want to execute the rest of my method and in the loop program wait for user reaction – Bortek23 Dec 07 '15 at 13:24
  • @Bortek23 - that can only be achieved with multi-threading. –  Dec 07 '15 at 13:25
  • your loop is most likely located in a method in the form's main class. this will cause the loop to run forever. you need to execute the loop on a different thread, or look into parallel function concept – Banana Dec 07 '15 at 13:25
  • [Application.DoEvents();](https://msdn.microsoft.com/en-us/library/system.windows.forms.application.doevents(v=vs.110).aspx) – bansi Dec 07 '15 at 13:26
  • 2
    Be careful with `DoEvents`; http://stackoverflow.com/a/5183623/1017882 –  Dec 07 '15 at 13:26
  • @bansi doevents will most likely cause a laggy ui – Banana Dec 07 '15 at 13:27
  • In UI programming it is almost never appropriate to have a busy-loop, everything is event-driven so hook up the code that should execute on the mouse click to the mouse click event, that is the correct way to do this. In other words, whatever comes after the loop should be relocated to the mouse click event handler instead. – Lasse V. Karlsen Dec 07 '15 at 13:34

1 Answers1

3

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;
        }
Theunis
  • 238
  • 1
  • 15
  • you should declare the `MouseClicked` field as volatile – Banana Dec 07 '15 at 13:27
  • @Banana volatile is not recommended. May halt all processes? I believe he is trying to write a mouse-logger app, like a key-logger so absolute positions are not required. I will never suggest using volatile. – Theunis Dec 07 '15 at 13:29
  • Won't this hammer CPU with a busy loop or will compiler take care of that? – DavidG Dec 07 '15 at 13:31
  • @DavidG i suppose it depends on whats in the loop – Banana Dec 07 '15 at 13:32
  • @DavidG No not much effect, this is how many pizza applications work. Almost all business applications with auto update would use this approach. You could always use a `Thread.Sleep()` but in this case he wants a direct feedback. [Here](http://stackoverflow.com/questions/72275/when-should-the-volatile-keyword-be-used-in-c) is a article about the volatile keyword @Banana. – Theunis Dec 07 '15 at 13:33
  • Like those used in pizza stores @DavidG, meaning orders that are listed. They do in general use a `Thread.Sleep()` so that their is not that much interference on the view end. – Theunis Dec 07 '15 at 13:37
  • Guys, ez. The loop is really empty. I pause executing the code using loop, cause I dont know how to do it in the other way – Bortek23 Dec 07 '15 at 13:38
  • @Theunis I really don't think that you can say "almost all business applications" work this way. In fact, I'd say the opposite. Having a tight loop is completely unnecessary and a waste of system resources. OP is already sitting in an event based system so should take advantage of that. – DavidG Dec 07 '15 at 13:46
  • @DavidG agreed. This was just a solution to his specific question not necessarily the only solution. – Theunis Dec 07 '15 at 13:50
  • @Theunis Wait, you agree that what you said was wrong? :) – DavidG Dec 07 '15 at 13:52
  • @DavidG Yes I do, as I based it on my experience and the code bases that I previously worked on. I can't generalize a comment to all software developers and their experiences. – Theunis Dec 07 '15 at 13:54