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

问题描述

使用以下code

    [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时将挂起(IE:从不返回),如果传递的句柄最近关闭的应用程序。 (这是奇怪的我,因为我还以为它只是一个零值返回)

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)

在随机手柄传递,如新的IntPtr(123456)成功,没有价值的回报。

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

任何人都可以请解释这种现象?

Could anyone please explain this behavior?

推荐答案

在这里阅读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时挂了"封闭"处理,但不与一个随机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 06:21