ali
ali

Reputation: 11045

C++ asynchronous function in Windows

I am back with an experimental project that starts with this: I have an array of 10000 elements of POINT type. They are supposed to be pixels with x and y coordinate, to be drawn on a window (SetPixel()). I have created a simple function that creates the DC, get each POINT from the array and draws it on the screen:

void draw_points() {
    HDC hdc = GetDC(hWnd);
    for (int i = 0; i < 10000; i++) {
        SetPixel(hdc, points[i].x, points[i].y, RGB(0, 0, 0));
    }
    ReleaseDC(hWnd, hdc);
}

Well, I placed this function inside the main loop of the WinMain() function. It works. I can see the points being drawn on the screen. The problem is that while the points are being displayed I can't do anything else, so I found out that I would need asynchronous functions, like in Java. That's because I would like to be able to add, remove, modify points from the array while the draw_points() function is running.

I don't need any result from it, I just want it to run in another thread while I do whatever I want with other functions. So, my question: what does Windows API offer me for this? Which is the usual way to do it? Do I need some external library? I just don't know how to start. I hope you understand what I want. Thanks!

Upvotes: 0

Views: 226

Answers (2)

Ryand
Ryand

Reputation: 436

The reason you cannot do anything is because you're not responding to Windows messages. You should put a PeekMessage() call in your loop to periodically check the message queue. When you get one, you need to call TranslateMessage() and DispatchMessage().

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409414

You shouldn't call this from the main loop. Instead you should call it when you get a WM_PAINT event in your window message loop.

Upvotes: 1

Related Questions