简体   繁体   English

一个事件等待其他事件发生

[英]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 ManualResetEvent或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(); 例如,如果您说manualResetEvent.WaitOne(); on the main thread, everything will freeze. 在主线程上,所有内容都会冻结。 They are just made for synchronizing threads just like Mutex, Semphores, TPL->Await etc.. 它们仅用于同步线程,例如Mutex, Semphores, TPL->Await等。

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 https://imgur.com/a/il1FI

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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