I've been trying to create a test application for something I'm working on at work. It's a sockets application and normally I'd just create a quick program in C that runs from the command line, but I thought I'd use it as an excuse to try some Windows programming using forms.
It all started out well enough, I added some controls to my form and tweaked them to get it doing what I wanted, then the problems started. Normally, most sockets apps spend their time in an infinite loop processing data that comes in. I tried adding a loop like this and it locked up the UI when the function is called, I'm assuming because it's called from the form and it never returns.
So I had a look online and tried to follow this tutorial, which uses WSAAsyncSelect to send a message when an event occurs on the socket. After much faffing around I couldn't get this to call my processing function (WndProc in his example).
Instead, I thought I could get around the problem of the locked up UI by placing my processing function in its own thread. I began looking into the CreateThread function, but you can't use a member function as the thread function unless you make it static. Obviously though, I'd like to be able to update controls on my form when things happen on my socket, like data received, peer closed connection etc, so I looked into how I can pass the this pointer as a parameter to the function.
In the examples I've seen they do something like this:
class A
{
......
};
A a;
Handle hThread = CreateThread(0, 0, ThreadFunc, &a, 0, 0);
So far I've not had much luck getting this to compile, mostly because I don't know how to get a pointer to the form that was created by Windows. I tried simply casting the this pointer, but the compiler wasn't too happy.
I guess what I'm looking for is either:
A way to pass the this pointer to my ThreadFunc, so I can update my controls when an event occurs.
Or, the proper way to use WSAAyncSelect to send a message to your handler when an event occurs on the socket.
Hopefully that makes sense. If you need any more info, code, screenshots etc let me know.
Question
ViZioN
Hi guys,
I've been trying to create a test application for something I'm working on at work. It's a sockets application and normally I'd just create a quick program in C that runs from the command line, but I thought I'd use it as an excuse to try some Windows programming using forms.
It all started out well enough, I added some controls to my form and tweaked them to get it doing what I wanted, then the problems started. Normally, most sockets apps spend their time in an infinite loop processing data that comes in. I tried adding a loop like this and it locked up the UI when the function is called, I'm assuming because it's called from the form and it never returns.
So I had a look online and tried to follow this tutorial, which uses WSAAsyncSelect to send a message when an event occurs on the socket. After much faffing around I couldn't get this to call my processing function (WndProc in his example).
Instead, I thought I could get around the problem of the locked up UI by placing my processing function in its own thread. I began looking into the CreateThread function, but you can't use a member function as the thread function unless you make it static. Obviously though, I'd like to be able to update controls on my form when things happen on my socket, like data received, peer closed connection etc, so I looked into how I can pass the this pointer as a parameter to the function.
In the examples I've seen they do something like this:
class A { ...... }; A a; Handle hThread = CreateThread(0, 0, ThreadFunc, &a, 0, 0);So far I've not had much luck getting this to compile, mostly because I don't know how to get a pointer to the form that was created by Windows. I tried simply casting the this pointer, but the compiler wasn't too happy.
I guess what I'm looking for is either:
Hopefully that makes sense. If you need any more info, code, screenshots etc let me know.
Cheers,
ViZioN
Link to comment
Share on other sites
2 answers to this question
Recommended Posts