为什么System.Diagnostic.Process在使用Windows任务栏图标切换应用程序时抛出异常?(Why does System.Diagnostic.Process throw exc

编程入门 行业动态 更新时间:2024-10-18 05:54:50
为什么System.Diagnostic.Process在使用Windows任务栏图标切换应用程序时抛出异常?(Why does System.Diagnostic.Process throw exception when switching apps using windows task bar icon? [duplicate])

这个问题在这里已有答案:

什么是NullReferenceException,我该如何解决? 33个答案

我目前正在开发一个使用System.Diagnostics.Process的应用程序来获取当前活动应用程序的窗口标题或ForeGround上的应用程序。 现在,这很好,并使用我当前的代码,直到我遇到这个问题。 如果我使用任务栏图标而不是应用程序窗口的最小化按钮切换我的应用程序,则会发生此NullReferenceException 。

编辑:如果我不提高它可能会令人困惑。 所以基本上NullReferenceException是由(title.Equals(System.Diagnostics.Process.GetCurrentProcess().MainWindowTitle))

title变量因null而为null

String title = GetActiveWindowTitle();

GetActiveWindowTitle()返回null。 这使用Window.GetForegroundWindow(); 并且由于某些原因使用任务栏图标来切换,这个Window.GetForegroundWindow(); 方法什么都不返回

本来我用过这个

[DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow();

得到我需要的东西。 但是,当使用任务栏图标切换应用程序时,我仍然遇到同样的问题。 使用此代码的唯一区别是异常是这个A first chance exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll

我的操作系统是Windows 8.1。

非常感谢任何投入。

更新 @KcDoD的答案有点正确。 由于NullReferenceException实际上是我的问题的一部分,我将它标记为答案。 它还指导我了解发生了什么。

基本上,在我的应用程序中,我在WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)调用此方法GetActiveWindowTitle() WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime) 。 WinEventProc()是一种在切换窗口时触发的委托方法。 事实证明,通过调用GetForegroundWindow(); 或GetActiveWindowTitle()进程未处于活动状态,这就是为什么当它获取Active应用程序或Process它给出/返回null。

最好的祝福

This question already has an answer here:

What is a NullReferenceException, and how do I fix it? 31 answers

I'm currently developing an app that uses System.Diagnostics.Process to get the Window Title of the Current Active App or the App on the ForeGround. Now, this is fine and working with the current code I have until I came across with this issue. This NullReferenceException occurs if I switch my app using the task bar icon instead of the minimized button of the app window.

Edit: It maybe confusing if I don't raise this up. So basically NullReferenceException is caused by (title.Equals(System.Diagnostics.Process.GetCurrentProcess().MainWindowTitle))

title variable is null due to

String title = GetActiveWindowTitle();

GetActiveWindowTitle() returns null. And this uses Window.GetForegroundWindow(); And for some reason using taskbar icons to switch up, this Window.GetForegroundWindow(); method returns nothing.

Originally I used this

[DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow();

To get what I need. But I'm still having the same issue when switching app using the task bar icon. The only difference with using this code is that the exception is this A first chance exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll

My operating system is Windows 8.1.

Any inputs is greatly appreciated.

Update The answer from @KcDoD was somewhat correct. And since the NullReferenceException was actually part of my question I will mark it as the Answer. And it also guides me a lot to figure out what's going on.

Basically, on my application I call this method GetActiveWindowTitle() within WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime). WinEventProc() is a delegate method that triggers when you switch window. And it turns out that by calling GetForegroundWindow(); or GetActiveWindowTitle() the process is not active yet that's why when it goes to get the Active application or Process it gives/return null.

Best regards

最满意答案

您的问题与您尝试与MainWindowTitle等于的字符串有关。 这意味着title变量/字段。

在尝试使用Equal之前,您应该检查tite为null

另外:这是一个使用此处答案的程序

static void Main(string[] args) { String answ; while (true) { answ = GetActiveWindowTitle(); if (answ == null) { Console.WriteLine("NO active program"); } else { Console.WriteLine(answ); } Thread.Sleep(1000); } } [DllImport("user32.dll")] static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll")] static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count); static private string GetActiveWindowTitle() { const int nChars = 256; StringBuilder Buff = new StringBuilder(nChars); IntPtr handle = GetForegroundWindow(); if (GetWindowText(handle, Buff, nChars) > 0) { return Buff.ToString(); } return null; }

答案:如果没有选择进程,则返回null。 我认为这是你问题的答案。

Your Problem is related to the String you are trying to Equal with MainWindowTitle . That means the title variable/field.

You should check tite for null before trying to use Equal

Additionally : Here is a program using answer from here

static void Main(string[] args) { String answ; while (true) { answ = GetActiveWindowTitle(); if (answ == null) { Console.WriteLine("NO active program"); } else { Console.WriteLine(answ); } Thread.Sleep(1000); } } [DllImport("user32.dll")] static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll")] static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count); static private string GetActiveWindowTitle() { const int nChars = 256; StringBuilder Buff = new StringBuilder(nChars); IntPtr handle = GetForegroundWindow(); if (GetWindowText(handle, Buff, nChars) > 0) { return Buff.ToString(); } return null; }

Answer : If there is no process selected then this will return null. I think that's the answer for your question.

更多推荐

本文发布于:2023-08-07 14:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464555.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:抛出   任务栏   应用程序   图标   异常

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!