简体   繁体   中英

What is the best way of threading while using c++ to create windows form application in visual studio?

I am trying to create a windows form application which waits for a message from a client using socket.

But the program is shown as not responding in the task manager.

So in order to avoid this im planning to use another thread to run the wait process. But visual studio 2010 has limited threading option while developing in c++

Pls help

There is no limitation for threading in MSVC++. You can use CreateThread or boost::thread , just::thread or even std::thread (if it supports) as your requirement.

You can create a thread for waiting to receive network packets and make free the main thread for windows messages.

在此处输入图片说明

This Microsoft Developer Network article Safe and Simple Multithreading in Windows Forms should provide you the gateway to understanding. There is quite a bit of documentation as well as a sample program.

The article describes the actual problem you are running into, your main message loop is being halted due to the socket read.

Another possible way of doing this is to use non-blocking sockets and to use the select() function to check if there is anything to read on the socket before you try to do a socket read.

Also take a look at the following resources.

Code Project Threads with Windows Forms Controls in Managed C++ .

statckoverflow question C++ Multithreading with Visual Studio 2010 forms application which contains a link to additional material.

std::thread is not supported in VisualStudio 2010. You can use any other mean of threading, like boost::thread (free and opensource) or just::thread (commercial) depending on your needs.

Personally, I would go for boost::thread , unless late conversion to c++11 were needed, in which case I would use just::thread , which is std compliant. Both options are terribly easy to get up and running and basic tasks are accomplished with no effort.

I don't have much information about boost::thread 's C++11 compliance . Notice that the concept are common to the two libraries

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM