0

I have 2 questions.

  1. The first main question is how do I fix the error I have when trying to invoke the method that contain KeyEventArgs e as argument. I only know how to deal with arguments when they are variables.

  2. The Second is just a quick question if the while loop will do so the program wait for (keydown) before printing the text(label)

    namespace Test2014
    {
        public partial class Form1 : Form
        {
            List<Action> methods = new List<Action>();
    
            public Form1()
            {
                InitializeComponent();
    
                // 1. Error invalid arguments
                methods.Insert(0, test); // 1.  passing ( KeyEventArgs e ) as argument ?
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                methods[0].Invoke();
    
            }
    
    
            void test(KeyEventArgs e) // 1. passing ( KeyEventArgs e ) as argument ?
            {          
                while (true) 
                {
    
                    if (e.KeyCode == Keys.A)  // 2. do this work as wait for keydown before moving down ?
                        break;
    
                } // 2.
                label_test.Text = "When Loop is broken (key down(A)) change this label";
            }
        }
    }
    
mollmerx
  • 648
  • 1
  • 5
  • 18
FunBugs
  • 1
  • 1
  • Don't use `while (true)` in the event handler. It will loop indefinitely unless the right key is pressed. – D Stanley Apr 24 '14 at 15:09
  • @DStanley Isn't that the point? – Ben Aaronson Apr 24 '14 at 15:11
  • @BenAaronson I think D Stanley meant by that that an event driven approach might be better. From my experience such loops will take at least 50% CPU time even if there's nothing to be processed in it other than this if block. – Francis Ducharme Apr 24 '14 at 15:13
  • @FrancisDucharme Yeah, I was assuming this was more of a toy example than anything else, but I guess it *is* worth emphasising that it's not a good approach in a real application – Ben Aaronson Apr 24 '14 at 15:15
  • Can we have the definition of `test` that you are trying to add to the list and later call its `Invoke` method? – Francis Ducharme Apr 24 '14 at 15:19
  • @BenAaronson If `test` is called and passed a key _other_ than "A" the function will loop indefinitely and the program will "hang". The OP seems to think that he/she is setting up a "message loop" that will infinitely loop and only respond if the right key is pressed. That's not what's happening here - control is never passed back to the calling method. – D Stanley Apr 24 '14 at 15:27

0 Answers0