One event wait for other event to occur

142 views Asked by At

So... The user has to click on the button, after he clicks it, the program should wait for another click on Panel and get the coordinates of that click. But as soon as I click the button, everything becomes unresponsive. Am I doing something wrong?

    private void Surbutton_Click(object sender, EventArgs e)
    {
        panel1.Cursor = Cursors.Cross;
        Cursor.Position = new Point(Left + panel1.Left + panel1.Width / 2, Top + panel1.Top + panel1.Height / 2);
        ziskavanie_pozicie = true;

        //Button ABCD = sender as Button;
        string ABCD = ((Button)sender).Name;

        switch (ABCD)
        {
            case "button_A":
                //cakaj.WaitOne();
                cakaj_manual.WaitOne();
                suradnica_Ax.Text = x.ToString();
                suradnica_Ay.Text = x.ToString();
                break;

            case "button_B":
                suradnica_Bx.Text = x.ToString();
                suradnica_By.Text = x.ToString();
                break;

            case "button_C":
                suradnica_Cx.Text = x.ToString();
                suradnica_Cy.Text = x.ToString();
                break;

            case "button_D":
                suradnica_Dx.Text = x.ToString();
                suradnica_Dy.Text = x.ToString();
                break;
        }
    }

    public void panel1_MouseClick(object sender, MouseEventArgs e)
    {
        MessageBox.Show("Hehe");
        if (ziskavanie_pozicie == true)
        {
            x = e.X;
            y = e.Y;
            //panel1.PointToClient(Cursor.Position);

            ziskavanie_pozicie = false;
            panel1.Cursor = Cursors.Default;
            //cakaj.Set();      
            cakaj_manual.Set();
        }
    }
2

There are 2 answers

4
AudioBubble On

If you just want to know for the reason your programs freezes, It's because you using for some reason the

ManualResetEvent or AutoResetEvent

which are not expected to be used on the main thread as it's not the reason they created for. If you say for example manualResetEvent.WaitOne(); on the main thread, everything will freeze. They are just made for synchronizing threads just like Mutex, Semphores, TPL->Await etc..

0
Matej On

So when the user clicks on the button shown by cursor on the image (or any of the buttons with the arrow) the cursor is gonna be moved to center of the white panel. At that point I need the user to click somewhere on the panel and get the coordinates of that click. When I have the coordinates, I assign them to textboxes to the left side of the buttons.

https://imgur.com/a/il1FI