-2

So now I have this: hooked hotkeys and a volatile bool that is checking in a while loop.

UserActivityHook actHook;
private void Form1_Load(object sender, EventArgs e)
{
    actHook = new UserActivityHook();
    actHook.KeyPress += new KeyPressEventHandler(klawisze);
    actHook.KeyDown += new KeyEventHandler(klawisze2);
    actHook.Start();
}

public void pw()
{
    while (!stopIt)
    {
        SetCursorPos(576, 363);
        Thread.Sleep(50);
        SetCursorPos(650, 363);
        Thread.Sleep(50);
        SetCursorPos(650, 430);
        Thread.Sleep(50);
    }   
}
public void Stop()
{
    stopIt = true;
}
private volatile bool stopIt;

public void dw()
{
    while (!stopIt)
    {
        SetCursorPos(860, 350);
        Thread.Sleep(50);
        SetCursorPos(890, 350);
        Thread.Sleep(50);
        SetCursorPos(890, 380);
        Thread.Sleep(50);
    }
}

public void klawisze(object sender, KeyPressEventArgs e)
{
    Thread w1 = new Thread(new ThreadStart(pw));
    Thread w2 = new Thread(new ThreadStart(dw));

    try
    {
        if (e.KeyChar == 21) //CTRL + SHIFT + U
        {       //1

            stopIt = false;
            w1.Start();
            if (e.KeyChar == 9)
            {
                Stop();
                w1.Join();
            }
        }
        if (e.KeyChar == 9) //CTRL + SHIFT + I
        {   //2

            stopIt = false;
            w2.Start();
            if (e.KeyChar == 21)
            {
                Stop();
                w2.Join();
            }
        }
    }

It's not closing first drawing and in effect when i press 1st hotkey and then second its drawing 2 triangles at same time. What is wrong here?

1 Answers1

0

I hope that is the answer for your question. You should make another thread,or this will not work right. However i would send the boolean 'firstLoop' from the winform to your other thread. My solution is not 100% correct...

Boolean firstLoop = false;

if (e.KeyChar == 21) //CTRL + SHIFT + U
{
    firstLoop = true

    while (firstLoop == true)
    {        
          SetCursorPos(576, 363);
          Thread.Sleep(50);
          SetCursorPos(650, 363);
          Thread.Sleep(50);
          SetCursorPos(650, 430);
          Thread.Sleep(50);
     }
}
if (e.KeyChar == 9) //CTRL + SHIFT + I
{  
    firstLoop = false;

    while (firstLoop == false)
    {
       SetCursorPos(860, 350);
       Thread.Sleep(50);
       SetCursorPos(890, 350);
       Thread.Sleep(50);
       SetCursorPos(890, 380);
       Thread.Sleep(50);
    }
}
Jens Kloster
  • 11,099
  • 5
  • 40
  • 54
C0d1ngJammer
  • 550
  • 1
  • 6
  • 21
  • You're right, its not correct... what other thread u mean ? thx – user3057699 Dec 03 '13 at 08:29
  • If you use Thread.Sleep() the whole winform is 'locked'.To bypass that i would do the loops in another thread. just my idea – C0d1ngJammer Dec 03 '13 at 08:42
  • Iam busy right now. Here are some examples
    -[How to start with threads](http://www.csharp-examples.net/create-new-thread/)
    -[invoke](http://stackoverflow.com/questions/14569482/c-sharp-threading-using-invoke-freezing-the-form)
    – C0d1ngJammer Dec 03 '13 at 10:15