Search code examples
c++functionasynchronousdrawpoints

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!


Solution

  • 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.