The signature of the thread function is wrong. It needs to match the one described in the documentation.
Furthermore, you should not call CreateThread directly. Call BeginThread instead. It will correctly mark your program as being multithreaded so that the memory manager knows to use proper protection for all further allocation operations.
That's all independent of anything you do in the thread. When we get to that, there are other problems. One is that you call Application.ProcessMessages. Message queues belong to specific threads. The Application object assumes it's processing the main VCL thread's message queue. When you call ProcessMessages, you're using the VCL-thread-processing code to process a different thread's message queue, and then all the assumptions that code makes about which queue it's processing are wrong. If you want to process messages in your worker threads, then you'll need to write the message-processing code yourself. Begin by reading about GetMessage, PeekMessage, and DispatchMessage. (Even if you're not using multiple threads, the presence of Application.ProcessMessages is an indication that a program has design problems. Threads just compound it.)
You don't need to call ExitThread. If your thread routine were a function like it's supposed to be, then you could just return a value like any ordinary function.
You haven't said where you get the "invalid window handle" exception. If you get it in the ProcessMessages call, then that's probably where the problem is. But if you get the error in one of the other functions you call, like Connect or SyncText, then you'll need to take a look there, too. Maybe you're accessing properties of a window that doesn't exist anymore, or maybe you're using an uninitialized variable.