【问题标题】:What is the correct way to update a ListView continously?连续更新 ListView 的正确方法是什么?
【发布时间】:2015-04-27 18:15:47
【问题描述】:

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

我的问题是,我想创建一个线程来监视进程并更新列表视图。我有一个全局变量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;
}

【问题讨论】:

  • 您闪烁的列表视图是独一无二的。您是在使用主要的 Windows 操作系统还是他们的嵌入式紧凑型产品线?我使用过他们的嵌入式产品线,silverlight 框架带来了许多图形问题(包括许多列表视图问题)。如果是这样,那么没有真正的解决方案,只能停止使用 listview,因为这都是微软的实现。
  • @Javia1492:我正在开发一个主要的 Windows 操作系统。我相信闪烁与我更新 Listview 的方式有关。如果出现滚动条,它就会闪烁。

标签: c windows multithreading thread-safety


【解决方案1】:

仅从您描述的内容(线程和访问冲突)来看,您的数据可能无法防止多个进程(线程)访问它。如果属实,这反过来很可能是您的访问违规的原因。谷歌 critical sectionssemaphoresmutex ,(这些链接只是一个开始)都与为需要在进程之间共享的数据实现线程安全有关。

这是一个使用关键部分的示例 (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;
}

查看提供的链接上面了解如何创建全局(CriticalSection),以及如何在使用前进行初始化。

【讨论】:

  • 我知道我必须使用它们,但我只是不知道如何使用 :( 你能举个例子吗?
  • InterlockedIncrement 不应该和互斥量和信号量一样吗?
  • 我没有使用 InterlockedIncrement 的经验。简而言之,它将对单个变量执行特定的原子增量(32 位或 64 位)边界。它可能对其他数据类型没有那么有用,例如结构或列表类型。
  • 我重写了我的代码以使用临界区,但没有运气。但是我在调​​试过程中注意到了一些东西。窗口关闭,窗口关闭后出现访问冲突,调试器跳转到线程,到行if ( !IsProcessRunning( ProcArrayQuery[ i ]-&gt;ProcessName, ProcArrayQuery[ i ]-&gt;DisplayName ) )。所以这不是可能的吗,不知何故线程没有结束,并试图访问已经 free()'d 的内存?
  • 您是否将关键部分放入线程处理函数(在您的示例中为 Thread() )? (如果这是更改受保护数据的位置,则需要在此位置。)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-01
相关资源
最近更新 更多