简体   繁体   English

在Visual Studio中使用C ++创建Windows窗体应用程序时的最佳线程化方法是什么?

[英]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. 我正在尝试创建一个Windows窗体应用程序,该应用程序等待使用套接字的来自客户端的消息。

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++ 但是Visual Studio 2010在用C ++开发时具有有限的线程选项

Pls help 请帮助

There is no limitation for threading in MSVC++. MSVC ++中的线程没有限制。 You can use CreateThread or boost::thread , just::thread or even std::thread (if it supports) as your requirement. 您可以根据需要使用CreateThreadboost::threadjust::thread甚至std::thread (如果支持)。

You can create a thread for waiting to receive network packets and make free the main thread for windows messages. 您可以创建一个线程来等待接收网络数据包,并释放Windows消息的主线程。

在此处输入图片说明

This Microsoft Developer Network article Safe and Simple Multithreading in Windows Forms should provide you the gateway to understanding. Microsoft开发人员网络文章Windows窗体中的安全和简单多线程应该为您提供了解的门户。 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. 执行此操作的另一种可能方法是使用非阻塞套接字,并使用select()函数在尝试进行套接字读取之前检查套接字上是否有要读取的内容。

Also take a look at the following resources. 另请参阅以下资源。

Code Project Threads with Windows Forms Controls in Managed C++ . 托管C ++中Windows窗体控件的代码项目线程

statckoverflow question C++ Multithreading with Visual Studio 2010 forms application which contains a link to additional material. statckoverflow问题使用Visual Studio 2010的C ++多线程表单应用程序包含指向其他材料的链接。

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. 在Visual Studio 2010中不支持 std::thread 。您可以根据需要使用其他任何线程方式,例如boost::thread (免费和开源)或just::thread (商业)。

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. 就个人而言,除非需要后期转换为c ++ 11,否则我会使用boost::thread ,在这种情况下,我将just::thread使用std just::thread 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 . 我没有太多关于boost::thread的C ++ 11兼容性的信息 Notice that the concept are common to the two libraries 请注意,该概念是两个库共有的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Visual Studio C ++中创建应用程序图标的最佳方法 - Best way to create an application-icon in visual studio c++ 使用 Git 处理 C++ Visual Studio 2019 解决方案的外部依赖源代码控制的最佳方法是什么? - What is the best way to handle source control of an external dependency for a C++ Visual Studio 2019 Solution using Git? 使用Visual Studio c ++的Windows窗体应用程序,该应用程序链接到CUDA项目 - Windows form application using Visual Studio c++, that links to a CUDA project 使用Visual C ++在“ Windows窗体应用程序”中设置像素值 - Set value of pixels in a “Windows Form Application” using Visual C++ 如何使用 c++ 和使用 Visual Studio 2017 菜单编辑器创建带有 Windows 菜单的 win32 应用程序 - how to create a win32 application with Windows menus using c++ and using the visual studio 2017 menu editor 在Visual Studio 2010 Professional for C ++ Windows窗体应用程序下更改启动窗体 - Change startup form under Visual Studio 2010 Professional for C++ Windows Form Application Windows中使用C ++进行线程化 - Threading in Windows using C++ 如何制作,Visual Studio 2010 c ++鼠标点击windows窗体应用程序? - How to make, Visual studio 2010 c++ mouse click in windows form application? 在Visual Studio C ++中显示JPEG流的最佳方法 - Best way to display a jpeg stream in visual studio c++ 使用 Visual Studio C++ 20 导入公共模块文件夹的最佳方法 - Best way to import public module folder using visual studio C++ 20
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM