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

时间:2015-04-27 18:15:47

标签: c windows multithreading thread-safety

我正在编写一个小应用程序,我想检查特定应用程序是否正在运行。我创建了一个窗口,它有一个ListView子窗口,另一个带有计时器的子窗口,它计数回到0.我想要仔细检查,如果我关闭的进程是关闭的,或者时间已经结束。无论哪种方式,窗户都应关闭。

我的问题是,我想创建一个监视进程的线程,并更新listview。我有一个全局变量ProcArrayCountDisplay,它保存当前显示的项目数(正在运行)。 因为我在线程中更改了这个变量,并且也监视它,所以窗口可以关闭,显然有些错误,因为有时我会得到一个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;
}

1 个答案:

答案 0 :(得分:0)

仅根据您描述的内容(线程和访问冲突),您的数据可能无法受到多个访问它的进程(线程)的保护。如果为true,则反过来可能是您违反访问权限的原因。 Google critical sections semaphores {{3 ,(这些链接只是一个开始)都与为需要在进程间共享的数据实现线程安全性有关。

以下是使用关键部分的示例( mutex

这是最容易看到和理解的方法之一。您基本上定义了受保护的逻辑区域。只应编辑共享数据。在此示例中,每次进入线程处理程序函数时,逻辑将流入临界区,编辑数据,然后逻辑流出临界区。

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;
}

有关如何创建全局(CriticalSection)以及如何在使用前初始化的信息,请参阅上面提供的链接