0

Hey,

I make a project of a chess board connected through serial port to the pc. Chess board sends commands like 'g1f3'. I'd like to make a clicker app which works in the background and clicks the move I made on the real board. I stucked at recognizing chess client window title. I've got this code:

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        while (true)
        {
            backgroundWorker1.ReportProgress(0);
            System.Threading.Thread.Sleep(1000);
        }
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        windowName = GetActiveWindowTitle();
        for (int i = 0; i <= windowName.Length - 3; i++)
        {
            if (windowName[i] == 'v' && windowName[i + 1] == 's' && windowName[i + 2] == '.')
            {
                textBox1.Text = "Game active";
                break;
            }
            else
                textBox1.Text = "Game not recognized";
        }
    }

Chess client names the window like: "xxxxx vs. xxxxx". This runs "almost" fine. "Almost" because sometimes (if app windows changed fast or randomly after even 1 switch delayed in time) it throws:

An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll

Additional information: Exception has been thrown by the target of an invocation.

Anyone could give me some hint how to handle this?

Edit: I got the code for GetActiveWindowTitle() from here: How do I get the title of the current active window using c#?

It throws null sometimes, but why is that?
Community
  • 1
  • 1
h2pc
  • 3
  • 3
  • An exception occurs in your `ProgressChanged` event handler; inspect the Inner Exception to see what is *actually* going wrong. – MicroVirus Apr 10 '16 at 21:40
  • It says: Object reference not set to an instance of an object. It's something with getting active window title. I put windowName on the textbox and saw that once in a while it is none. So I guess it's null sometimes. But why is that? I get the code for active window title from here:[link](http://stackoverflow.com/questions/115868/how-do-i-get-the-title-of-the-current-active-window-using-c) – h2pc Apr 10 '16 at 21:56
  • 1
    My magic ball predicts: `GetActiveWindowTitle` does not return a valid string, but rather `null`. – MicroVirus Apr 10 '16 at 21:58
  • 1
    Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – MicroVirus Apr 10 '16 at 21:59
  • @MicroVirus - I'm new to stackoverflow and I didn't knew if I edit my post I can't confirm your answer anymore. I was sleepy yesterday and it was very simple thing, as you said - GetActiveWindowTitle returned null sometimes. I changed 'return null' to 'return "error"' and everything works fine. Thanks for answer. Is there a possibility to mark your comment as an answer? – h2pc Apr 11 '16 at 14:00
  • It's not possible to mark a comment as the answer, no. You can either put the gist of the comment into your own answer and mark that as accepted, or ask the other person (me) to post it as such. I'll add my comment as an answer, so you can accept it if you want. – MicroVirus Apr 11 '16 at 15:06

1 Answers1

0

The exception you are seeing is because the handler, backgroundWorker1_ProgressChanged, is throwing an exception; you can then check the inner exception to see the original exception.

In your case, GetActiveWindowTitle returns null, but your code doesn't handle it, so you get a NullReferenceException. A reason that it might return null is if there is no active window or it could not get the window text. The best solution for your code is to check if it returns null, and if so handle the situation appropriately (such as putting a warning to the user in textBox1).

MicroVirus
  • 5,324
  • 2
  • 28
  • 53