简体   繁体   中英

One event wait for other event to occur

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();
        }
    }

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..

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM