Re: g_io_channel_win32_new_messages() & WaitForSingleObject()



Man, I'm almost there. First, just ignore everything having to do with win32 services. I was misreading the msdn service example. The WaitFor*() is really done on an "event". So here is a stripped down demo of the progress so far. It does what I want, but then crashes before ending the event loop gracefully, which means something needs to be different. I'm sure it has something to do with the size arg to g_source_new() call, since "GSource" is simply:

typedef struct {
} GSource;

Clearly I have no idea what I even want to pass to it.

======= glib_win32_event_test.c =========
#include <glib.h>
#include <windows.h>

HANDLE windows_event = NULL;
GPollFD g_poll_fd;
GSourceFuncs g_source_funcs;
int global_int = 0;

gboolean my_source_prepare(GSource *source, gint *timeout_)
{
g_print("1) my_source_prepare()\n");
*timeout_ = -1;
return FALSE;
}

gboolean my_source_check(GSource *source)
{
g_print("1) my_source_check()\n");
if(global_int)
{
g_print("1) bam!\n"); return TRUE;
}

return FALSE;
}

gboolean timeout_callback(gpointer opaq)
{
g_print("1) still here...\n");

return TRUE;
}

DWORD WINAPI thread_entry(LPVOID lpParameter)
{
g_print("2) I'm thread #%d\n", GetCurrentThreadId());
Sleep(4000);
g_print("2) stuff is going to happen now..\n");
global_int = 1;
SetEvent(windows_event);
g_print("2) I'm done\n");
return 0;
}

int main(int argc, char **argv)
{
DWORD error_code;
gchar *error_string;
GMainLoop *main_loop;
GSource *g_source;

g_print("1) I'm thread #%d\n", GetCurrentThreadId());

if((windows_event = CreateEvent(NULL, TRUE, FALSE, NULL)) == NULL)
{
 error_code = GetLastError();
 error_string = g_win32_error_message(error_code);
 g_print("1) CreateEvent() failed, \"%s\"\n", error_string);
 g_free(error_string);
 return 1;
}

main_loop = g_main_loop_new(NULL, FALSE);

g_timeout_add(1000, timeout_callback, NULL);
g_poll_fd.events = 0;
g_poll_fd.revents = G_IO_IN;
g_poll_fd.fd = windows_event;

memset(&g_source_funcs, 0, sizeof(GSourceFuncs));
g_source_funcs.prepare = my_source_prepare;
g_source_funcs.check = my_source_check;

g_source = g_source_new(&g_source_funcs, sizeof(GSource));
g_source_attach(g_source, NULL);
g_source_add_poll(g_source, &g_poll_fd);

if(CreateThread(NULL, 0, thread_entry, NULL, 0, NULL) == NULL)
{
 error_code = GetLastError();
 error_string = g_win32_error_message(error_code);
 g_print("1) CreateThread() failed, \"%s\"\n", error_string);
 g_free(error_string);
 return 1;
}

g_main_loop_run(main_loop);
return 0;
}
======================== cmd.exe session output: ====
1) I'm thread #3444
1) my_source_prepare()
2) I'm thread #5064
1) my_source_check()
1) still here...
1) my_source_prepare()
1) my_source_check()
1) still here...
1) my_source_prepare()
1) my_source_check()
1) still here...
1) my_source_prepare()
2) stuff is going to happen now..
2) I'm done
1) my_source_check()
1)   bam!
1) still here...
[crash and burn]
=========================

Thoughts?


Tor Lillqvist wrote:
It's best to experiment, as I said I don't recall the details by
heart... and too busy to actually check now.

--tml





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]