-3

So im trying to operate threads with key events, I managed to make them start by hitting key E and R( two thread), but i can't stop them by realeasing keys, keyup event doesnt work for some reason... (after starting, stopping i would like to restart them when needed... so no timer...)

C# WINDOWS APPLICATION Code:(method I'm using)

    private void Yawspeed_KeyDown(object sender, KeyEventArgs e)
    {
        Thread yawspeedRightThread = new Thread(new ThreadStart(YawspeedRightThread));
        Thread yawspeedLeftThread = new Thread(new ThreadStart(YawspeedLeftThread));

        if (e.KeyCode == Keys.E)
        {
            yawspeedRightThread.Start();
        }
        if (e.KeyCode == Keys.R)
        {
            yawspeedLeftThread.Start();
        }
    }

((Method would be something like that?:

private void Yawspeed_KeyUp(object sender, KeyEventArgs e)
{
   if (e.KeyCode == Keys.E)
   {
       yawspeedRightThread.Abort();
   }
   if (e.KeyCode == Keys.R)
   {
       yawspeedLeftThread.Abort();
   }
}

(Ending...)

    private void Yawspeed_Load(object sender, EventArgs e)
    {

    }

    #region Thread Functions
    /// <summary>
    /// This thread will move your cursor
    /// </summary>
    public static void YawspeedRightThread()
    {


        while (true)

...(Rest of the code, thread itself, functions...)

H H
  • 263,252
  • 30
  • 330
  • 514
Aron_t
  • 13
  • 4
  • Could you provide a clearer description of what you intend to do? It seems somewhat unusual... – Kris Sep 13 '14 at 12:00
  • 6
    You already asked this question. Looks like you deleted the previous one, also destroying the comments added by SO users. This is not helpful to anyone, please don't do this. – Hans Passant Sep 13 '14 at 12:03
  • http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keyup%28v=vs.110%29.aspx Control.KeyUp Event "Occurs when a key is released while the control has focus.", does it still have focus? – Paul Zahra Sep 13 '14 at 12:09

1 Answers1

0

First and foremost you cannot restart a thread. If you start a thread, the thread finishes, and then you try to start that same thread object a second time it will throw an exception telling you that's not possible. You should look into using a Thread Pool instead.

Now assuming you're going to ignore the above and try to do this anyway. Two things to check would be that:

  1. You've registered the key up event with the control/form.
  2. The control/form actually has focus while you're performing the test.

Lastly, make sure that the key up actually isn't getting called. The reason I say this is that Thread.Abort() isn't actually guaranteed to end the thread's execution. It simply throws an exception in the thread. This means that if you did something like added a try catch inside of the thread's function that caught an "Exception" object, the try catch will catch the exception that Abort forced into it and allow the thread to continue executing. This fact is one reason why Abort isn't recommended and people are encouraged to use other mechanisms for ending a thread, like tripping a Boolean.

OxCantEven
  • 629
  • 4
  • 9