【发布时间】: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, ¶ms, 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