This is the function in question...

What happens is the user opens notepad, if notepad is open then they are required to type a password.. If its invalid, notepad is closed.. if the password is valid, it continues.. else if the user hasn't entered a password within 10 seconds, timeout..

So the function works when the password is valid/invalid.. the problem is when the user times out, and they open notepad again, they cannot enter the password as it wont allow them..

Try compiling the attached .cpp file and see what I mean..

void pwget()
{    
     string password;
     password.clear();
     REPEAT
     char cProcess[80] = "notepad.exe";
     DWORD dwReturn;
     dwReturn = CountProcesses(cProcess);
     if(dwReturn != -1)
     {
                           if(dwReturn == 1)
                           {
                              cout<<"User Attempted to Open Program!\n\n";
                              
                              using namespace jsw::threading;
                              char pword[1024];
                              auto_event e;
                              LPVOID args[2] = {pword, &e};
                              thread worker = thread::start(get_password, args);
                              
                              if (e.wait(10000)){
                              string password(pword);
                                 if(password == "brandon")
                                 {
                                  system("cls");
                                  cout<<"Password was entered correctly.\n\n";
                                  Sleep(1000);
                                  system("cls");
                                  break;
                                  }
                                  else
                                  {
                                      system("cls");
                                      cout<<"Password is Invalid...\n\n";
                                      Sleep(1000);
                                      system("taskkill /IM notepad.exe");
                                      cout<<"\n";
                                      system("cls");
                                      cout<<"Welcome to GameBlock v1.0\n\n";
                                      pwget();
                                  }
                              }
                              else {
                              worker.abort(NO_ERROR);
                              cout<<"\n\nUser Timedout..\n\n";
                              Sleep(1000);
                              system("taskkill /IM notepad.exe");
                              cout<<"\n";
                              system("cls"); 
                              cout<<"Welcome to GameBlock v1.0\n\n";
                              pwget();
                              }
                           }
                           else if(dwReturn == 0)
                           {
                               cout<<"Program Not Running\n";
                               Sleep(1000);
                               main();
                           }
     }
    UNTIL((password == "brandon"));
    password.clear();
    cout<<"In Minutes, enter the length of time the program should run: ";
    cin>>x;
    cin.ignore();
    x = x*60;
}

Extra problems but not required to help fix: Timer Pause function doesnt work.. Find by me for now. I would just like help on why they cannot enter the password when it timesout.

Instead of worker.abort(), use this:

keybd_event(VK_RETURN, 0x3d, 0, 0);
keybd_event(VK_RETURN, 0x3d, KEYEVENTF_KEYUP, 0);
worker.join();

The problem was that stdin was locked by getch, and terminating the thread didn't release the lock. The simplest solution I can think of is signaling a keyboard event to unlock stdin and stop the thread normally rather than abort it.

Your code is somewhat scary too, but I'll refrain from overloading you with information.

:O Didnt think of that... Yeah it looks scary with all the process checking and loops and stuff.. I'll clean it up as soon as I solve all the bugs. Usually while im coding it gets out of control and then when the program is done, it gets all neat.

Thanks again! I had a feeling you'd be the only one to reply -_-

btw how did your learn about threads?? I mean I know a little bit of it after you showed me an example but there isnt very good tutorials out there :S Or maybe Im just being difficult or blind.

btw how did your learn about threads??

Trial and error...and error, and error, and error. I'll let you know when the error part gives way to better understanding. I'm far from confident when it comes to concurrency. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.