d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help Wit Thread Class In C#
Add Reply New Topic New Poll
Member
Posts: 27,086
Joined: Mar 7 2008
Gold: 685.00
Apr 23 2013 09:51pm
It's a game that I move a character using arrows and I have to avoid wall and moving objetcs. I'm using Thread class with rectangle intercept method to test if the char is in collision with something. In that case, it will get out of the loop then calling the function LifeLost(). In that function, number of life will decrement and the value will be convert into a label text. But, I get that error message :"InvalidOperationException". In the same function, if the number of life = 0, it spam a "game over" message then closing the form. But, wile closing the form, I got the same error message.

here is the code:





Code
public partial class frmGame : Form
   {        
       public Boolean ok = false;
       public Boolean door = false;
        public Boolean Exit = false;
       public Thread Th;
       ...
   

       public void TestCollision()
       {
           int i = 2;
           while (ok == false && door == false)
           {
               while (i < 7 )
               {
                   if (Rectangle.Intersect(new Rectangle(m_X - 20, m_Y, 50, 90), new Rectangle(tMur[i, 0], tMur[i, 1], tMur[i, 2], tMur[i, 3])) != Rectangle.Empty)
                   {
                       ok = true;
                       MessageBox.Show("Boom");                        
                   }
                   i++;
               }
               
               if ((Rectangle.Intersect(new Rectangle(m_X - 20, m_Y, 50, 90), new Rectangle(m_XO, m_YO, 25, 25)) != Rectangle.Empty) || (Rectangle.Intersect(new Rectangle(m_X - 20, m_Y, 50, 90), new Rectangle(m_XO2, m_YO2, 25, 25)) != Rectangle.Empty) || (Rectangle.Intersect(new Rectangle(m_X - 20, m_Y, 50, 90), new Rectangle(m_XO3, m_YO3, 25, 25)) != Rectangle.Empty))
               {
                   MessageBox.Show("Boom");
                   ok = true;                    
               }

               if (Exit == false)  
               {
                   if ((Rectangle.Intersect(new Rectangle(m_X - 20, m_Y, 50, 90), new Rectangle(tMur[0, 0], tMur[0, 1], tMur[0, 2], tMur[0, 3])) != Rectangle.Empty) || (Rectangle.Intersect(new Rectangle(m_X - 20, m_Y, 50, 90), new Rectangle(tMur[1, 0], tMur[1, 1], tMur[1, 2], tMur[1, 3])) != Rectangle.Empty))
                   {
                       MessageBox.Show("Boom");                        
                       ok = true;                        
                   }

               }
               else
               {
                   if (Rectangle.Intersect(new Rectangle(m_X - 20, m_Y, 50, 90), new Rectangle(1150, 0, 50, 50)) != Rectangle.Empty)
                   {
                       MessageBox.Show("Level cleared!");
                       door = true;
                   }                    
               }
               i = 2;
               Thread.Sleep(50);
           }
           if (ok)
           {
               LifeLost();              
           }
           
       }

       private void btnBegin_Click(object sender, EventArgs e)
       {
           panGame.Refresh();
           tmObs1.Start();
           tmObs2.Start();
           tmObs3.Start();          
           Th = new Thread(TestCollision);                    
           Th.Start();          
       }

       private void LifeLost()  //Fonction quand le bonhomme perd une vie
       {            
           tmObs1.Stop();
           tmObs2.Stop();
           tmObs3.Stop();
           Th = null;            
           Life--;
           txtLife.Text = Convert.ToString(Life);         // Bug here        
           if (Life == 0)
           {
               MessageBox.Show("Game over!");                
               this.Close();                                               // Bug here
           }


This post was edited by eric838 on Apr 23 2013 09:52pm
Member
Posts: 27,086
Joined: Mar 7 2008
Gold: 685.00
Apr 24 2013 10:46pm
InvalidOperationException , "Control control name accessed from a thread other than the thread it was created on."
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Member
Posts: 27,086
Joined: Mar 7 2008
Gold: 685.00
Apr 24 2013 11:03pm
Quote (carteblanche @ 24 Apr 2013 23:51)


ty I'l check tomorrow
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll