Code works with a messagebox but no no?

Asked

Viewed 83 times

3

Good afternoon, I’m doing a little school work to learn how to control radio Buttons with a switch in winforms

public Form1()
    {
        InitializeComponent();
        Form1.CheckForIllegalCrossThreadCalls = false;

        Thread th = new Thread(() =>
        {

            while (true)
            {
                RadioButton btn = this.tabPage1.Controls.OfType<RadioButton>().Where(x => x.Checked).FirstOrDefault();


                if (btn != null && btn.Checked)
                {
                    switch (btn.Name)
                    {
                        case "radioButton1":
                            MessageBox.Show("Ronaldo");
                            pictureBox1.Visible = true;
                            lblNome1.Visible = true;
                            lblPos1.Visible = true;
                            break;

                        case "radioButton2":
                            MessageBox.Show("Messi");
                            pictureBox2.Visible = true;
                            lblNome2.Visible = true;
                            lblPos2.Visible = true;
                            break;
                    }
                }

            }
        });

        th.Start();




    }

There are no syntax errors only visibles are enabled only if there is a messagebox however this one like this within an infinite cycle of challenge, will always appear and to my knowledge has no way to stop

I thank those who can help me to solve this problem or who tried.

  • What is the need to use an infinite loop thread for these validations?

  • To not have to click a button to perform the switch action, it’s the only way I know so far so the code is always running as if it were live

  • In addition to what @Pedropaulo has already said, are sure you need the switch this is also probably a mistake. It seems to be wanting to use all possible tools, even if they are inadequate. Almost all use of thread that I’ve seen is wrong, it’s unlikely that your case is right. Tip, only use what you dominate, if you don’t dominate, don’t use, or dominate before using.

  • The exercise was given by my teacher, control rdb with a switch, I just joined the thread to not have to click a button, I don’t even know to use threads, true, but that’s how I saw online, and no, I didn’t domino c# but I liked to dominate and so I’m learning

1 answer

4


Winforms is based on Rapid application Development and many operations in graphical interface are related to the events that occur in the interface.

As an example, I "tied" below the effect of clicking the radio button on an event handler called RadioClicado(object, System.EventArgs):

public Form1()
{
    InitializeComponent();

    // Amarração do evento de clique dos radio buttons ao manipulador.
    radioButton1.Click += RadioClicado;
    radioButton2.Click += RadioClicado;
}

// Manipulador de evento de clique em radio button.
public void RadioClicado(object sender, System.EventArgs e)
{
    if (sender is RadioButton)
    {
        RadioButton rb = sender as RadioButton;
        switch (rb.Name)
        {
            case "radioButton1":
                //MessageBox.Show("Ronaldo");
                pictureBox1.Visible = true;
                lblNome1.Visible = true;
                lblPos1.Visible = true;
                break;

            case "radioButton2":
                //MessageBox.Show("Messi");
                pictureBox2.Visible = true;
                lblNome2.Visible = true;
                lblPos2.Visible = true;
                break;
        }
    }
}

Normally, GUI operations should take place in the thread interface itself. Maybe that’s why thread that you open to check the state of the radio button is causing race condition (race condition), and the use of a Messagebox mitigates this condition by including a pause in the thread competitor.

  • 2

    Thank you very much, it really worked and it’s true, I researched more and was in a race condition, thank you very much for your time

Browser other questions tagged

You are not signed in. Login or sign up in order to post.