处理但不是随机的

处理但不是随机的

本文介绍了为什么 GetWindowText 挂起“关闭"?处理但不是随机的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码

    [DllImport("user32.dll", EntryPoint = "GetWindowText", ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
    private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpWindowText, int nMaxCount);

    public static String GetWindowText(IntPtr hWnd)
    {
        StringBuilder title = new StringBuilder(MAX_TITLE_LENGTH);
        int titleLength = WinAPI.GetWindowText(hWnd, title, title.Capacity + 1);
        title.Length = titleLength;
        return title.ToString();
    }

如果将句柄传递给最近关闭的应用程序,GetWindowText 将挂起(即:永不返回).(这对我来说很奇怪,因为我原以为它只会返回零值)

GetWindowText will hang (IE: never return) if passed a handle to a recently closed application. (Which is odd to me because I would have thought it would just return with a zero value)

传入诸如 new IntPtr(123456) 之类的随机句柄成功并返回无值.

Passing in random handle such as new IntPtr(123456) succeeds and returns with no value.

谁能解释一下这种行为?

Could anyone please explain this behavior?

推荐答案

在此处阅读 GetWindowText 卧底说明:GetWindowText 的秘密生活.

Read here a description of GetWindowText undercovers: The secret life of GetWindowText.

我不认为你会得到更好的 :-)如果你真的想 100% 确定你不会挂起调用它,你需要在另一个你可以管理的线程上做它(即:如果需要,杀死)

I don't think you'll ever get a better one :-)If you really want to be 100% sure you won't hang calling it, you need to do it on another thread that you can manage yourself (ie: kill if you need to)

这篇关于为什么 GetWindowText 挂起“关闭"?处理但不是随机的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 14:52