12/31/2004

How To Detect If an Application Has Stopped Responding

简介:本文描述了如何使用C++、VB、Windows API和.Net类库判断一个进程是否停止了响应。

没有一个明确的“停止响应”的定义,例如对于Internet Explorer或者Word 2000这样的多顶层窗口应用程序,可能存在部分顶层窗口失去响应的情况,这时很难定义应用程序是否停止了响应。但是一般来说,很多应用程序只有一个标志性窗口(或者叫主窗口)。如果主窗口在一段时间内不响应用户操作的时候,对于用户来说应用程序是停止响应的(例如在Internet Explorer等待远程FTP服务器返回登录结果时)。尽管这经常属于其他应用程序应该妥善考虑的范畴,但是如果自己的应用程序依赖于这样的程序而没有源代码级控制权,那么应该提供一个机会允许用户中断对外部应用程序的等待或者干脆终止外部应用程序。

Windows2000及Windows XP中提供了一个API IsHungAppWindow,但是微软声明此函数可能在后续的Windows版本中删除这个函数。不用遗憾,在Windows 32中有一个特殊的消息WM_NULL,在其文档中说明它可以和SendMessageTimeout一起用于判断某个窗口是否停止了响应。一般来说,我们需要获得进程的标志性窗口(或者叫主窗口)来执行这个判断。2002年7月的MSDN杂志Q&A专栏文章Get the Main Window, Get EXE Name 说明了如何做到这一点。很遗憾,附录中的文章How To Detect If an Application Has Stopped Responding中通过访问浏览器对象的hwnd属性绕过了这个过程,或许是因为在VB中调用EnumWindow比较繁琐;参见附录中的Enumerating Windows Using API(VB)。

另外,.Net Framework中提供了一个Responding属性,可以用于检测进程是否停止了响应。附录中的两篇基于.Net的文章就是检查了这个属性。看起来这似乎是通过检查进程的MainWindowHandle指明的窗口是否立刻响应来解决的。通过这种方法判断的缺点是无法设置等待超时的时间。

参考资料
IsHungAppWindow(MSDN)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/ishungappwindow.asp
WM_NULL(MSDN)
http://msdn.microsoft.com/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowmessages/wm_null.asp
Process.Responding Property (MSDN)
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemDiagnosticsProcessClassRespondingTopic.asp
如何“干净地”终止 Win32 中的应用程序
http://support.microsoft.com/kb/178893/zh-cn
How To Detect If an Application Has Stopped Responding
http://support.microsoft.com/kb/231844
304991 Detect if an Application Has Stopped Responding by Using C#
http://support.microsoft.com/kb/304991
304990 Detect if an Application has Stopped Responding by Using Visual Basic .NET
http://support.microsoft.com/kb/304990
Get the Main Window, Get EXE Name
 http://msdn.microsoft.com/msdnmag/issues/02/07/CQA/
Enumerating Windows Using API(VB)
http://www.vbaccelerator.com/home/VB/Code/Libraries/Windows/Enumerating_Windows/article.asp

12/15/2004

在richedit控件中插入动态GIF

这是CSDN的VC论坛上的一个老FAQ了。我在写自定义在RichEdit中插入对象的图标(http://www.blogcn.com/user3/jiangsheng/blog/1319738.html)这片文章的时候就是想用这个技术做动画GIF的,但是怎么判断一个内嵌在RichEdit的对象是GIF这个问题一直没有解决。好在QQ附带的一个控件支持动画GIF,可以插入这个对象来解决问题。

首先需要一个定时器来定时更新GIF。

public: System::Void OnLoad(System::Object^  sender, System::EventArgs^  e)
   {
   this->typingRichTextBox->RichTextShortcutsEnabled=false;
   this->timer1->Start();
   }

private: System::Void OnFormClosing(System::Object^  sender, System::Windows::Forms::FormClosingEventArgs^  e)
   {
    this->timer1->Stop();
    this->frameClosing=true;
   }

然后在定时器的处理函数里面通知GIF控件更新显示。

private: System::Void OnTimer1Elapsed(System::Object^  sender, System::Timers::ElapsedEventArgs^  e)
   {
    if(this->frameClosing==false)
    UnmanagedGifTriggerFrameChange(this->contentRichTextBox->Handle.ToInt32());
   }

最后的工作就是插入GIF了

private: System::Void smileToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e)
   {
    insertemotion(sender,e,"c:\\Program Files\\Tencent\\QQ\\Face\\20.gif");
   }

private: System::Void insertemotion(System::Object^  sender, System::EventArgs^  e,String ^ gifPath)
   {
   System::Windows::Forms::RichTextBox^  ptypingRichTextBox=this->typingRichTextBox;
   stdcli::language::pin_ptr< wchar_t> wch = PtrToStringChars(gifPath);
   UnmanagedInsertGif(ptypingRichTextBox->Handle.ToInt32(),wch );
   }

为了偷懒起见关键的代码还是用Native C++来写,所以这些函数名全部以Unmanaged开头。

//unmanagedwin32.h
//混合托管和非托管编程的话,编译的时候不能使用/clr:safe和/clr:pure开关,
#pragma once
#pragma unmanaged//用这个开关来切换托管和非托管代码
extern void UnmanagedScrollToButton(int hwndRichEdit);
extern void UnmanagedInsertGif(int hwndRichEdit,wchar_t * pFilePath);
extern void UnmanagedGifTriggerFrameChange(int hwndRichEdit);
#pragma managed

//unmanagedwin32.cc

//使用了Windows 平台SDK
//必须要使用clr:oldSyntax来避免SDK头文件和C++/CLI语法的冲突
//而且在Visual C++ 2005 Express中要添加/d1PrivateNativeTypes 开关以避免混合LNK2022错误。
#define UNICODE
#define _UNICODE
#define  _WIN32_DCOM
#include "windows.h"
#include "Richedit.h"
#include "Richole.h"
#pragma comment( lib, "User32.lib" )
#include "UnmanagedWin32.h"
//纯用C编写自动化操作会死人的,幸好可以自动导入
#import "c:\\Program files\\tencent\\qq\\ImageOle.dll" named_guids
//移动光标到末尾然后调用System::Windows::Forms::RichTextBox的ScrollToCaret方法,需要拖动滚动条才可以看到最后一行之前的文字。老办法,API伺候。
void UnmanagedScrollToButton(int hwndRichEdit)
{
 HWND h=(HWND)hwndRichEdit;
 int line = SendMessage(h, EM_GETFIRSTVISIBLELINE, 0, 0);
 int linecount = SendMessage(h, EM_GETLINECOUNT, 0, 0);
 SendMessage(h, EM_LINESCROLL, 0, (linecount - line - 2));
}

下面的函数UnmanagedInsertGif(int hwndRichEdit,wchar_t * pFilePath)是插入一个ImageOle::GifAnimator对象,UnmanagedGifTriggerFrameChange(int hwndRichEdit)是枚举richedit中已经插入的对象,如果是ImageOle::GifAnimator对象,那么调用其TriggerFrameChange方法。

这两个函数里面的方法也可以用于插入其他类型控件,以及和插入的对象通讯。

使用windows 2000,QQ2004SP1,Visual C++ 2005 Express, Platform SDK (Windows 2003)编译测试。