• 0

[C++] C++ Form woes


Question

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:

  • 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.

Cheers,

ViZioN

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

I'm not a c++ coder but i think you should be able to use a backgroundworker for this. It allows you to do stuff without locking the main (ui) thread. They're pretty straightforward in c# so i'm assuming that it's similar in c++. I'm sure someone else can shed more light on this if you need them to....

Link to comment
Share on other sites

  • 0
I'm not a c++ coder but i think you should be able to use a backgroundworker for this. It allows you to do stuff without locking the main (ui) thread. They're pretty straightforward in c# so i'm assuming that it's similar in c++. I'm sure someone else can shed more light on this if you need them to....

Thanks very much! Looked it up online and this seems to be exactly what I'm looking for. Hacked around with some of the code on MSDN and managed to get it working, and I can still use all of the controls on my form. I should just be able to implement the polling loop similarly to how I would do it with a command line program and update the controls as I go. Cheers :woot:

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.