c++ - 有 cin 功能时检查用户连接
问题描述
嗨,今天我试图在有 cin 时不断检查用户的连接性。
这是代码:
while (true)
{
if (InternetCheckConnection(networkBuffer, FLAG_ICC_FORCE_CONNECTION, 0) != false)
{
while (cin >> userInput)
{
cout << "> " << userInput << endl;
}
} else {
break;
}
}
我确定问题出在 cin 上,但我必须不断检查用户的连接性(同时还有 cin 正在进行中)。
. . .
有些人在这里(在stackoverflow上)告诉我使用线程......只是我不知道如何使用它们来解决这个问题。所以我尝试了这个方法
void test()
{
if (InternetCheckConnection(networkBuffer, FLAG_ICC_FORCE_CONNECTION, 0) != false
{
cout << "success" << endl;
} else {
exit(0);
}
}
int main()
{
thread first;
cout << "1: " << first.joinable() << endl;
first = thread(test);
cout << "2: " << first.joinable() << endl;
string s;
cin >> s;
first.join();
cout << "3: " << first.joinable() << endl;
system("PAUSE");
}
但是这段代码的唯一问题是他在 cin 之前检查,而不是在 cin 存在时检查。
谢谢。
解决方案
推荐阅读
- ios - 当泛型具有默认值时,使用泛型创建结构实例
- google-apps-script - 在 Google App Script 中将文件的所有权转让给我单位以外的用户
- android - 从片段过渡到片段时工具栏闪烁
- python - 数据集的最小距离
- python - Python 应用程序和 docker/AWS 设计问题
- mysql - 创建触发器但出现未知列错误。if 语句有问题?
- angular - Angular 材料步进器在 ng 服务期间不会编译
- javascript - XPages 动态内容控件:事件处理程序被错误触发
- java - 使用没有操作栏的主题时,如何在 Android 中隐藏应用程序的名称?
- mysql - 使用 XAMPP 找不到服务器 IP 地址