如果查找方法是在不使用System.Diagnostics.Process.Responding响应 [英] Find if process is responding without using System.Diagnostics.Process.Responding

查看:363
本文介绍了如果查找方法是在不使用System.Diagnostics.Process.Responding响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天每个人。



这是问题的它为已经解决了一个又一个时,我意识到,我认为这是问题毕竟它没有。还是感谢,我已经学到了两件事情。



我的应用程序与IE和不时的工作负荷,IE被重定向到一个网站,一些不良JavaScript代码,最终阻止IE浏览器界面。因此挡住了我的应用程序也曾经在我的应用程序一切都在同一个运行。



要应对这一问题在启动我的应用程序运行在另一个发一个静态方法 即每15秒做了简单的检查,如果IE浏览器响应或没有,如果IE没有响应,他将关闭其所有进程的,在我的应用程序主然后我的应用程序可以恢复其工作,解放了锁。



要找到,如果IE进程的正在响应我有一个简单的代码是这样的:

 布尔终止= FALSE; 
的foreach(exe文件的System.Diagnostics.Process在System.Diagnostics.Process.GetProcesses())
{
如果(exe.ProcessName.StartsWith(IEXPLORE))
{
如果(exe.Responding ==假)
{
终止= TRUE;
中断;
}
}
}
//代码以关闭所有IE进程的......

为了给 Process.Responding 属性发现该进程是否响应,并根据有关的 MSDN ,这个属性需要命名的另一个属性 MainWindowHandle 为了完成检查的过程中是可用的。如果 MainWindowHandle 不可用 Process.Responding 总是返回,即使该过程没有响应正确的。



和出于某种原因,我不知道。在Windows XP中 MainWindowHandle 不可有这么响应是不准确的。



这就是为什么我需要知道的另一种方式找到,如果一个特定的进程在Windows XP响应或没有。



任何帮助表示赞赏,感谢



PS:如果您寻找一个网站,以冻结IE这里有云:的 http://aboutmycollege.com/



编辑:继0xA3执行建议:



我经历了所有IE进程的支票去了,如果他们有 MainWindowHandle 属性,那些谁了该属性我送他们响应属性,一个MessageBox和他们的报告正确地当IE未在Windows 7,但不能在XP响应。



我执行该代码每15秒:

 的foreach(exe文件的System.Diagnostics.Process在System.Diagnostics.Process.GetProcesses())
{
如果(exe.ProcessName.StartsWith(IEXPLORE))
{
如果(exe.MainWindowHandle == IntPtr.Zero)
{
System.Windows.Forms.MessageBox.Show(过程不具有MainWindowHandle);
}
,否则
{
System.Windows.Forms.MessageBox.Show(过程回应:+ exe.Responding.ToString());
}
}
}

在Windows 7和XP的他报告IE进程的不具备 MainWindowHandle 属性,在Windows 7中,他还报告正确时,IE没有响应。但在XP中所有的IE进程与 MainWindowHandle 当他们不总是甚至响应。


解决方案

IE浏览器是特殊的,因为每个标签都有它自己的进程再加上有一个额外的父IE进程。事实上,只有父进程将会有一个有效的 MainWindowHandle



你检查是否 MainWindowHandle 为空为所有这些过程?如果不是,我认为你的代码应工作在XP如预期好。



更新



由于检查所有的IE实例没有帮助,接下来的事情我会尝试是修改所使用的 Process.Responding 超时。酒店内部调用 SendMessageTimeout API函数,然后检查返回值是否发生超时。如果是这样,则假设进程挂。超时是5秒的硬编码值。

您可以致电 SendMessageTimeout 自己使用P / Invoke并改变超时

。可能较小的值将使Windows XP上的效果会更好:

 函数[DllImport(user32.dll中,字符集= CharSet.Auto )
静态外部的IntPtr SendMessageTimeout(
HandleRef的hWnd,
INT味精,
的IntPtr的wParam,
IntPtr的lParam的,
INT标志,
INT超时,
OUT的IntPtr pdwResult);

const int的SMTO_ABORTIFHUNG = 2;

布尔IsResponding(工艺过程)
{
HandeRef handleRef =新HandleRef(过程,process.MainWindowHandle);

INT超时= 2000;
IntPtr的lpdwResult;

IntPtr的LRESULT = SendMessageTimeout(
handleRef,
0,
IntPtr.Zero,
IntPtr.Zero,
SMTO_ABORTIFHUNG,
超时,
OUT lpdwResult);

返回LRESULT = IntPtr.Zero!;
}


Good day everyone.

This problem was part of another one which it as been solved, i realized that what i thought it was the problem after all, it wasn't. Still thanks to that I've learned a couple things.

My application does loads of work with IE and from time to time, IE is redirected to a website with some bad Javascript code that ends up blocking IE interface. And consequently blocking my application too once everything on my application is running on the same Thread.

To counteract that problem, at startup my application runs a static method in another Thread that every 15 seconds does a simple check if IE is responding or not, and if IE isn't responding, he closes all its process's, liberating the lock on my application main Thread and then my application can resume its work.

To find if IE process's are responding i had a simple code like this:

bool terminate = false;
foreach (System.Diagnostics.Process exe in System.Diagnostics.Process.GetProcesses())
{
    if (exe.ProcessName.StartsWith("iexplore"))
    {
        if (exe.Responding == false)
        {
            terminate = true;
            break;
        }
    }
}
// Code to close all IE process's...

In order to the Process.Responding property finds if the process is responding, and according to information on MSDN, this property needs another property named MainWindowHandle to be available in order to complete the process of checking. And if MainWindowHandle isn't available Process.Responding always returns true even if the process isn't responding.

And for some reason which i don't know. In Windows XP MainWindowHandle isn't available there so Responding isn't accurate.

Thats why i need to know another way to find if a specific process is responding or not in Windows XP.

Any help is appreciated, thanks.

PS: If your looking for a website to freeze IE here goes: http://aboutmycollege.com/

EDIT: Following 0xA3 suggestion:

I went through all IE process's checking if they had the MainWindowHandle property, those who had that property i send they Responding property to a MessageBox and they report correctly when IE isn't responding on Windows 7 but not on XP.

I executed this code every 15 seconds:

        foreach (System.Diagnostics.Process exe in System.Diagnostics.Process.GetProcesses())
        {
            if (exe.ProcessName.StartsWith("iexplore"))
            {
                if (exe.MainWindowHandle == IntPtr.Zero)
                {
                    System.Windows.Forms.MessageBox.Show("Process doesn't have MainWindowHandle");
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("Process Responding: " + exe.Responding.ToString());
                }
            }
        }

In Windows 7 and Xp he reports the Process's of IE that don't have the MainWindowHandle property, and in Windows 7 he also reports correctly when IE isn't responding. But in XP all IE process's with MainWindowHandle are always responding even when they aren't.

解决方案

IE is special because each tab has its own process plus there is an additional parent IE process. In fact, only the parent process will have a valid MainWindowHandle.

Did you check whether MainWindowHandle is null for all these processes? If it isn't I think your code should work as expected on XP as well.

Update

Since checking all IE instances didn't help, the next thing I would try is to modify the timeout that is used by Process.Responding. The property internally calls the SendMessageTimeout api function and then checks the return value whether a timeout occurred. If so, the process is assumed to be hanging. The timeout is a hard-coded value of 5 seconds.

You can call SendMessageTimeout yourself using P/Invoke and vary the timeout. Possibly a shorter value would give better results on Windows XP:

[DllImport("user32.dll", CharSet=CharSet.Auto)]
static extern IntPtr SendMessageTimeout(
    HandleRef hWnd, 
    int msg, 
    IntPtr wParam, 
    IntPtr lParam, 
    int flags, 
    int timeout, 
    out IntPtr pdwResult);

const int SMTO_ABORTIFHUNG = 2;

bool IsResponding(Process process)
{
    HandeRef handleRef = new HandleRef(process, process.MainWindowHandle);

    int timeout = 2000;
    IntPtr lpdwResult;

    IntPtr lResult = SendMessageTimeout(
        handleRef, 
        0, 
        IntPtr.Zero, 
        IntPtr.Zero, 
        SMTO_ABORTIFHUNG, 
        timeout, 
        out lpdwResult);

    return lResult != IntPtr.Zero;
}

这篇关于如果查找方法是在不使用System.Diagnostics.Process.Responding响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆