持续更新 ListView 的正确方法是什么?

What is the correct way to update a ListView continously?

我正在编写一个小应用程序,我想在其中检查特定应用程序是否 运行。我创建了一个 window,它有一个 ListView 子 window,还有另一个带有计时器的子 window,它倒计时为 0。我想不断地检查两个进程是否关闭我监控的,或者时间到了。无论哪种方式,window 都应该关闭。

我的问题是,我想创建一个线程来监视进程并更新列表视图。我有一个全局变量 ProcArrayCountDisplay,它包含当前显示的项目数(运行)。 由于我在线程中更改了这个变量,并且也对其进行了监视,因此 window 可以关闭,显然出了点问题,因为有时我会得到一个 Access Violation

我尝试在函数中使用 InterlockedIncrement( &ProcArrayCountDisplay );InterlockedDecrement( &ProcArrayCountDisplay );,但没有成功。

所以很明显我做错了什么。但是我应该如何正确地做呢?有时,Listview 会闪烁。我可以做些什么吗?

提前致谢!

我的代码:

// globals
typedef struct
{
   BOOL bKill;
}PARAMS, *PPARAMS;

struct ProcToDisplay
{
   wchar_t * ProcessName;
   wchar_t * DisplayName;
};
struct ProcToDisplay **ProcArrayDisplay = NULL;
int ProcArrayCountDisplay = 0;

BOOL InitInstance( HINSTANCE hInstance, int nCmdShow )
{
g_hInst_Main = hInstance;
int Width, Height, xPos, yPos;

Width = 400;
Height = 400;
xPos = ( GetSystemMetrics( SM_CXSCREEN ) / 2 ) - ( Width / 2 );
yPos = ( GetSystemMetrics( SM_CYSCREEN ) / 2 ) - ( Height / 2 );

g_hWnd_Main = CreateWindowEx( WS_EX_TOPMOST, WindowClassName, WindowName, WS_OVERLAPPEDWINDOW, xPos, yPos, Width, Height, NULL, NULL, hInstance, NULL );
if ( !g_hWnd_Main )
{
    return FALSE;
}

ShowWindow( g_hWnd_Main, nCmdShow );
UpdateWindow( g_hWnd_Main );

return TRUE;
}

LRESULT CALLBACK MainWndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
static HBRUSH hbrBackground;

PAINTSTRUCT ps;
HDC hdc;
static unsigned ThreadId;
static HANDLE hThread = NULL;
static PARAMS params;

switch ( message )
{
    case WM_CREATE:
    {
        INITCOMMONCONTROLSEX iccx;
        iccx.dwSize = sizeof( INITCOMMONCONTROLSEX );
        iccx.dwICC = ICC_LISTVIEW_CLASSES;

        if ( !InitCommonControlsEx( &iccx ) )
        {
            // handle error
        }

        g_hWnd_ListView = CreateWindow( WC_LISTVIEW, NULL, WS_BORDER | WS_CHILD | WS_VISIBLE | LVS_REPORT, xStartPos, yStartPos, MaxWidth, MaxHeight, hWnd, NULL, g_hInst_Main, NULL );
        if ( !g_hWnd_ListView )
        {
            // handle error
        }

        HWND hWnd_Counter = CreateWindow( WindowClassNameChild, NULL, WS_VISIBLE | WS_CHILD | SS_LEFT | WS_BORDER, 10, 370, 380, 17, hWnd, NULL, g_hInst_Main, NULL );
        if ( !hWnd_Counter )
        {
            // handle error
        }
        SetTimer( hWnd_Counter, 1, 1000, NULL );

        hThread = ( HANDLE )_beginthreadex( NULL, 0, Thread, &params, 0, &ThreadId );
    }
    break;
    case WM_PAINT:
    {
        hdc = BeginPaint( hWnd, &ps );
        EndPaint( hWnd, &ps );
    }
    break;
    case WM_CLOSE:
    {
        if ( wParam == 1 )
        {
            DestroyWindow( hWnd );
        }
    }
    break;
    case WM_DESTROY:
        params.bKill = TRUE;
        WaitForSingleObject( hThread, 2000 );
        CloseHandle( hThread );
        FreeStruct();
        PostQuitMessage( 0 );
        break;
    default:
        return DefWindowProc( hWnd, message, wParam, lParam );
}
return 0;
}

LRESULT CALLBACK ChildCounterWndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
PAINTSTRUCT ps;
HDC hdc;

switch ( message )
{
    case WM_PAINT:
    {
        hdc = BeginPaint( hWnd, &ps );

        if ( ProcArrayCountDisplay == 0 )
        {
            EndPaint( hWnd, &ps );
            PostMessage( g_hWnd_Main, WM_CLOSE, 1, 0 );
        }

        RECT clientRect;
        GetClientRect( hWnd, &clientRect );

        //DrawText( hdc, timeString, -1, &clientRect, DT_LEFT | DT_VCENTER | DT_SINGLELINE );
        ExtTextOut( hdc, 0, 0, TA_LEFT | TA_CENTER | ETO_OPAQUE, &clientRect, timeString, wcslen( timeString ), NULL );

        EndPaint( hWnd, &ps );
    }
    break;
    case WM_TIMER:
        InvalidateRect( hWnd, NULL, FALSE );
        break;
    case WM_DESTROY:
        // Destroy the timers. 
        KillTimer( hWnd, 1 );
        PostQuitMessage( 0 );
        break;
    default:
        return DefWindowProc( hWnd, message, wParam, lParam );
}

return 0;
}

unsigned __stdcall Thread( void *ArgList )
{
PPARAMS pparams;

pparams = ( PPARAMS )ArgList;

while ( ( !pparams->bKill ) || ( ProcArrayCountDisplay > 0 ) )
{

    // clear previous data
    ListView_DeleteAllItems( g_hWnd_ListView );
    FreeStructDisplay(); // frees the array and decrements ProcArrayCountDisplay


    for ( int i = 0; i < ProcArrayCountQuery; i++ )
    {
        // querys the processes and adds them to the array
        if ( !IsProcessRunning( ProcArrayQuery[ i ]->ProcessName, ProcArrayQuery[ i ]->DisplayName ) )
        {
            // IsProcessRunning failed
        }
    }

    if ( ProcArrayCountDisplay == 0 )
    {
        // no package to display
        break;
    }

    // display processes
    InsertListViewItems();
    Sleep( 1000 );
}

_endthread();

return 0;
}

仅根据您的描述(线程和访问冲突),您的数据可能无法防止多个进程(线程)访问它。如果为真,这又可能是您访问冲突的原因。 Google critical sections, semaphores, mutex,(这些link只是一个开始)都与实施有关需要在进程之间共享的数据的线程安全。

这里是一个使用临界区的例子(from HERE

这是最容易查看和理解的方法之一。您基本上定义了一个受保护的逻辑区域。只有共享数据才能被编辑。在这个例子中,每次进入线程处理函数,逻辑流入临界区,数据被编辑,然后逻辑流出临界区。

DWORD WINAPI ThreadProc( LPVOID lpParameter )
{
    ...

    // Request ownership of the critical section.
    EnterCriticalSection(&CriticalSection); 

    // Access the shared resource.

    // Release ownership of the critical section.
    LeaveCriticalSection(&CriticalSection);

    ...
return 1;
}

参见link提供的上面如何创建全局(CriticalSection),以及使用前如何初始化。