Event that informs main thread on error (but not an exception)

Hi,

i have a question on how to exchange information between my main thread and worker threads. I have a worker thread that writes data to file and i want it to inform my main thread via "Event" handle (old school win32 event) that something failed while writing. How can i achieve that?
I have a class "WriteContainer" in file "WriteContainer.h" and i declared

extern HANDLE g_Event in that header.

In my main.cpp i defined the HANLDE g_Event.
In the main function i Called "CreateEvent( ... )

hoping this leads to ONE handle that i can "signal" with "setEvent" function from all instances of my "WriteContainer" class in all the different threads.


This does not work, so i think i was wrong. Does anyone have any idea on this?

Thanks and regards
You can provide a name in CreateEvent( ... ). See:

https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-createeventa

In other threads you can use OpenEvent(...) with this name. See:

https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-openeventa

This way you don't need a global variable.

On the other side: Why can't you use g_Event for your purpose?
Hi,

thanks for answering my question. I don't know why this does not work. I set the event in my parallel thread, but in my main thread, it won't be recognized by
"WaitForSingleObject" (although i am not 100% sure if this would be the right way to "listen" for that event)
I can try to rproduce my issue in a more simple example, but was not able to post code this morning, so i didn't yet try ...
OpenEvent might work. the thing is: the event handle itself is visible and accessable every where, so i think the "global variable" is not my issue ...

Thanks a lot so far
That is the right way. Did you check return error codes?

Here's an example. Output goes to the debug console.:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#ifndef UNICODE
#define UNICODE
#define _UNICODE
#endif
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <tchar.h>
#include <process.h>

unsigned __stdcall worker(void*);

const TCHAR* STOP_EVENT_NAME = _TEXT("StopEvent");

int main() {
	DWORD dwReturn{};

	if (HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE, STOP_EVENT_NAME)) {
		unsigned threadid;
		_beginthreadex(nullptr, 0, worker, nullptr, 0, &threadid);

		dwReturn = WaitForSingleObject(hEvent, INFINITE);
		CloseHandle(hEvent);
	}
	else {
		OutputDebugString(_TEXT("error: CreateEvent failed\n"));
		return 1;
	}
	if (dwReturn != WAIT_OBJECT_0) {
		OutputDebugString(_TEXT("error: WaitForSingleObject failed\n"));
		return 1;
	}
	OutputDebugString(_TEXT("Ok\n"));
}

unsigned __stdcall worker(void*) {
	if (HANDLE hEvent = OpenEvent(EVENT_ALL_ACCESS, TRUE, STOP_EVENT_NAME)) {
		Sleep(10*1000);
		SetEvent(hEvent);
		CloseHandle(hEvent);
	}
	else
		OutputDebugString(_TEXT("error: OpenEvent failed\n"));
	return 0;
}
Last edited on
Hi everyone.
I tried with "OpenEvent" and it works just fine. Thanks.
I just don't know what's the difference with the global Variable ...
Anyway, i have a way to do what i wanted, thus, thanks a lot.

Regards,
fluppe
Topic archived. No new replies allowed.