user3147183
user3147183

Reputation: 13

Button_click in windows form

I have this button5 function below. What I want is when the use wants to click button1 after clicking button5 the while loop in button5 should break, because the choice is now 1. Choice is a global variable set to zero at the start. While the button5 function is running the button1 function will not be called on click. How to solve this problem?

   This is the Answer Thanks Everyone For the Help!!

         private Thread demoThread = null;
                delegate void SetTextCallback(string text);

     private void button1_Click(object sender, EventArgs e)
            {
                    choice = 1;

                System.Console.WriteLine(choice);
            }     
         private void button5_Click(object sender, EventArgs e)
                    {
                        //button1.Enabled = false;
                        button5.Visible = false;
                        panel2.Visible = true;
                        panel1.Visible = true;
                        panel3.Visible = true;
                        label2.Visible = true;
                        button1.Visible = true;
                        button2.Visible = true;
                        button3.Visible = true;
                        button4.Visible = true;

                        this.demoThread = new Thread(new ThreadStart(this.StartForLoop));

                        this.demoThread.Start();



                    }
                    private void StartForLoop()
                    {
                        while (choice != 1 || choice != 2 || choice != 3)
                        {


                           if (choice == 1 )
                            {
                                choice = 1;
                                break;
                            }

                           if (choice == 2)
                           {
                               choice = 2;
                               break;
                           }

                           if (choice == 3)
                           {
                               choice = 3;
                               break;
                           }
                            Application.DoEvents();

                        }
                        System.Console.WriteLine("AAA");
                        if (choice == 3)//why
                        {
                        }
                        if (choice == 1)//true
                        {
                            System.Console.WriteLine("label");

                            this.SetText("Does the animal lay eggs?");
                        }
                        if (choice == 2)//false
                        {
                        }

                    }

Upvotes: 1

Views: 136

Answers (3)

João Pinho
João Pinho

Reputation: 3775

You have a problem with Thread's, the problem is that you program Thread is busy in the loop of your button5 and until it finishes handling button5 code, your thread will not pay attention to more anything.

To solve this, you must run your while loop inside of new thread like this:

Thread t = new Thread (new ThreadStart(delegate(){
    //while goes here along with the if's...
}));
t.Start();

In your button1, when you change the value of your global variables the code inside of a thread launched in the button5 will now be aware of your changes and behave accordingly.

Also be very careful with the following, since choice is a global variable it can be access by two threads now at the same time, the program thread and your new thread, because of this ensure you access the choice variable with mutexs, in c# you can access a thread shared variable like this:

//declare this next to your choice variable.
Object mux_choice = new Object();

lock(mux_choice){
   //changing choice here is thread safe.
}

Since choice seems to be a value type, you must create a object representing the access to your value type variable (http://msdn.microsoft.com/en-us/library/c5kehkcz.aspx).

You have more info about threads in C# here: http://www.albahari.com/threading/

Note: Make sure you protect the choice variable everywhere it is used.

Also, from your comments I assume that you want to modify the Form controls properties, like label2.Text="..." becarefull with that, you will face Cross Thread Exceptions if you do that. To modify a Controls property you must call the Invoke method, that invokes the change in the UI thread, like this:

label2.Invoke((MethodInvoker)(() => label2.Text = "some text"));

Depending on the .NET framework version, here is a code compatible with .NET 2.0:

label2.Invoke(new MethodInvoker(delegate(){ label2.Text = "some text"; }));

Regards.

Upvotes: 1

shujaat siddiqui
shujaat siddiqui

Reputation: 1577

The best way is to run a for each loop into another thread so it will not disturb the UI of form. and user can easily click on button 1.

like just for basic idea :

 private void button5_Click(object sender, EventArgs e)
   {
    button5.Visible = false;
    panel2.Visible = true;
    panel1.Visible = true;
    panel3.Visible = true;
    label2.Visible = true;
    button1.Visible = true;
    button2.Visible = true;
    button3.Visible = true;
    button4.Visible = true;
    Thread thread = new Thread(new ThreadStart(StartForLoop));
    thread.Start();    
    }


 public void StartForLoop()
    {
      while (choice != 1 || choice != 2 || choice != 3)
     {
       if (choice == 1 || choice == 2 || choice == 3)
        {
          choice = 1000;
          break;
        }
       Application.DoEvents();
     }

    if(choice==3)//why
    {
    }
    if(choice==1)//true
    {
    label2.Text = "asdasd";
    }
    if(choice==2)//false
    {
    }
   }

PS : better to implement lock on choice as well

Upvotes: 1

nvoigt
nvoigt

Reputation: 77304

This is a wrong concept. Never use Application.DoEvents();, it's a hack. It does not solve any of your problems.

You do not need a while loop in Button5_Click. Everything in that loop could be handled by code in your other click handler.

Your problem can probably be solved by a state machine. This looks complicated, but it#s not. Try to implement it as simple as possible and ask another question when you encounter problems.

Upvotes: 3

Related Questions