MFC 编程:编译时出错:线程代码中的错误
MFC programming: error while compiling: error in code of a thread
我在以下代码中遇到错误。
DWORD WINAPI CMbPoll::testThread(LPVOID lpVoid)
{
DWORD dwWaitResult;
while(1)
{
dwWaitResult = WaitForSingleObject(ghSemaphore, INFINITE/*0L*/);
if (connectionSuccessful == 1)
{
staticConnectionStatus.ShowWindow(FALSE);
}
else
{
staticConnectionStatus.ShowWindow(TRUE);
}
MessageBoxW(L"hi");
switch (dwWaitResult)
{
case WAIT_OBJECT_0:
Read_One_t(pollSlaveId[0], pollAddress[0], 0);
temporaryCount++;
break;
case WAIT_TIMEOUT:
temporaryCount++;
break;
default:
break;
}
}
}
错误是:
我。
在staticConnectionStatus.ShowWindow(FALSE);
错误 C2228:".ShowWindow'必须具有类/结构/联合
第二。
在MessageBoxW(L"hi");
错误 C2352:"CWnd::消息框W":非法调用非静态成员函数
我无法理解为什么会出现这些错误。
我对testThread
的声明是:
static DWORD WINAPI testThread(LPVOID lpVoid);
staticConnectionStatus
是 MFC 中窗体上静态文本标签的成员变量。
DDX_Control(pDX, IDC_STATIC_CONFIG6, staticConnectionStatus);
提前谢谢你。
这是因为testThread是静态的。静态方法无法访问类的实例变量。
解决方案(最近出现了很多)是使testThread非静态,并使用回调函数启动线程并调用CMbPoll::testThread
,使用传递给CreateThread
的this
指针。
DWORD WINAPI thread_starter(LPVOID lpVoid)
{
return ((CMbPoll*)lpVoid)->testThread();
}
CreateThread(..., thread_starter, this, ...);
我假设您从 CMbPoll
方法中的代码启动线程,如果没有,则将 this
替换为CMbPoll
对象的地址。
相关文章:
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- ASIO signal_set多个 IO 线程不可靠,具体取决于代码顺序?
- 如何分析代码的哪一部分创建了线程?
- 如何完全关闭 TBB 代码中的线程
- 提供对不同类型的数据(建议、代码审查)的线程安全访问的类
- 我有一个线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x8)错误.我认为这是由于内存管理不好.我可以
- C++11:具有互斥锁的线程看到原子变量的值发生变化,尽管这是唯一可以改变它的代码
- 琐碎并发代码的吞吐量不会随着线程数量的增加而增加
- 删除原子多线程代码中的容器
- Visual Studio 发布模式阻止在调试模式下执行的代码.使用 WinHTTP 和多线程
- 开始多线程代码之前的初始化
- 使用 std::chrono::steady_clock 对线程/异步中的代码进行基准测试
- 实现在多线程代码中安全恢复的断点
- 一个简单的 win32 多线程代码.这能行吗?
- 终止调用本机代码的 .Net 线程
- 线程 1:xcode 中出现EXC_BAD_ACCESS(代码 = 1,地址 = 0x0)错误
- 有没有更好的方法可以使此代码线程安全?线程局部静态似乎是一个生硬的工具
- 如何使用 openMP 使此代码线程安全
- ls 此代码线程安全
- 如何使这个代码线程安全?ID + +;Print_Value (ID);