gpt4 book ai didi

c++ - 从工作线程调用主线程回调函数

转载 作者:可可西里 更新时间:2023-11-01 10:29:48 26 4
gpt4 key购买 nike

场景:我有一个 C++ DLL。在这个 DLL 中,我创建了一个工作线程。在工作线程中,我有一个循环等待用户通过 USB 硬件设备输入。只有当 USB 设备上的用户输入满足某些条件时,循环才会结束。另外,我需要将USB设备的用户使用反馈实时反馈到屏幕上。它使用 Delphi GUI 进行反馈。

当用户使用USB 设备时,Windows 系统会产生一个回调函数。此回调函数写在同一个 C++ DLL 文件中,并作为参数传入 USB 设备的初始化函数。

我在 DLL 中使用一个全局变量作为标志来确定何时必须退出此循环。

我还从 Delphi DLL 加载这个 C++ DLL。Delphi DLL -> C++ DLL反馈显示来自Delphi DLL。

基本上,我现在面临的问题是函数ptr,funcptr,根本调用不了。屏幕上没有实时反馈。这是 Delphi DLL 中的一个函数。这是代码行:

(*(reinterprete_cast<FUNCPTR>(funcPtr)))("this is the feedback msg displayed on Delphi GUI");

有人对此有解决方案吗?

我是新手,非常感谢任何答案。感谢您的帮助。

    //Global variable
BOOL flag = TRUE;

//A function type in Delphi calling app
typedef void (__stdcall *FUNCPTR)(PCHAR);

//Functions start here.....
DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
do {} while (flag);
}

function_1st_CalledFromDelphiDLL(FUNCPTR funcPtr)
{
Initialize_USBDevice(handleUSBDeviceEvent_callback, funcPtr);
}

function_2nd_CalledFromDelphiDLL()
{
DWORD threadID;
HANDLE hWorkerThread;

hWorkerThread = CreateThread(NULL,0,ThreadProc, 0, 0 , &threadID);

if (hWorkerThread!=NULL)
{
WaitForSingleObject(hWorkerThread, 30000);
}
}

//This is the callback function, called by Windows system when user meddles with the USB device
handleUSBDeviceEvent_callback(void *funcPtr)
{
flag = FALSE; //so loop in ThreadProc can exit
//The following code cannot be executed at all. Even when i Try MessageBox( NULL,L"msg",NULL,NULL), the message box doesn't popup too. But, I can write something to a filestream here.
(*(reinterprete_cast<FUNCPTR>(funcPtr)))("this is the feedback msg displayed on Delphi GUI");
}

最佳答案

首先,我不建议使用变量在线程之间进行通信。为了您的目的,使用一个事件。

你的动态链接库:

HANDLE _exitNow = NULL;
HANDLE _queueLock = NULL; // for part 2 below

// call this from your main at start
bool DLL_Initialize()
{
_exitNow = CreateEvent(...);
_queueLock = CreateMutex(...);
... initialize your device, add the callback ...
}

// call this from your main at exit
void DLL_Shutdown()
{
SetEvent(_exitNow);
}

// your worker thread
void DLL_Worker()
{
// use options so WaitFor...() does not block
int result = WaitForSingleObject(_exitNow, ...);
if(result indicates _exitNow was fired)
{
CloseHandle(_exitNow);
... shutdown sequence, unhook your callback, deinit your device ...
CloseHandle(_queueLock);
}
}

这会处理 init/shutdown/worker 位。现在是困难的部分。

首先,您无法从工作线程中操作 UI 位。我不记得确切的原因——它与主线程拥有的 Windows 消息队列的所有权有关。如果您所要做的只是显示一些应该更新的内容,您应该执行以下操作:

  • 声明一个输出数据队列。这可能只是一个循环管理的数组。什么都行。
  • 声明一个互斥量来保护它。如果您的队列已经是线程安全的,则可选。
  • 声明 get 函数和一个在访问前检查互斥量的 put 过程。
  • 声明一个自定义窗口,即使您可以在 Windows 消息队列中发布。 (检查 msdn 中的自定义窗口消息)。并在您的主窗口中声明一个处理程序,该处理程序使用您的 get() 并更新显示。

假设以上,其余代码变为...(请注意,我已经有一段时间没有这样做了,所以名称和声明可能略有偏差,但原理是一样的)

你的主程序:

// double check how to do this exactly. I haven't done this in a long time. 
const CUSTOM_WINDOW_EVENT = WINDOWS_CUSTOM_MESSAGE + [SOMETHING];

// check for proper syntax
function Form.CustomHandler: Integer; handles CUSTOM_WINDOW_EVENT;
var
S: String;
begin
S := GetDataFromDLL();
... update display based on S ...
end;

你的动态链接库:

const CUSTOM_WINDOW_EVENT = WINDOWS_CUSTOM_MESSAGE + [SOMETHING]; 
TQueue queue; // find a suitable type. std::queue<> works fine

// delphi will call this
<String-type> DLL_GetStatus()
{
... wait on mutex using WaitForSingleObject() ...
... read one entry from queue ...
... release mutex ...
... return it ...
}

void PutStatus(String statusData)
{
... wait on mutex using WaitForSingleObject() ...
... write to queue ...
... release mutex ...
... push the custom message to the windows message queue, use PostMessage() IIRC ...
}

<whatever> handleUSBDeviceEvent_callback(void *funcPtr)
{
... read device, compose status data ...
PutStats(statusData);
}

我是靠内存完成所有这些工作的,所以我敢肯定会有什么地方不对劲。希望您无论如何都能掌握这些原则。

关于c++ - 从工作线程调用主线程回调函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3350375/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com