各位大佬,
小白能力有限,沒搞懂VS環境下除錯和直接運行exe檔案到底區別在哪兒,卡在這一個多星期了,還請大佬們幫忙看看,不甚感激。
問題描述:通過C# UIA的方法撰寫了一個Winform程式,其中一部分功能是通過撰寫的程式呼叫電腦上另一個軟體的UI控制元件,實作自動化點擊按鈕選擇檔案輸入等功能。在VS2019環境下點擊start除錯時沒有報錯,但是從debug檔案夾把exe檔案單獨拿出來運行時發現程式報錯(偶爾運氣好的話也不會報錯)。通過在winform界面增加文本輸出發現是在查找電腦上被呼叫的程式的視窗界面時沒找到就報錯。
報錯代碼段:
private void GetEcuInfo(String EcuAddress)
{
AutomationElement formDPS = null;
//AutomationElement formProgramming = null;
AutomationElement formSelectJ2534 = null;
AutomationElement formGCIErrorTip = null;
AutomationElement NoVINTip = null;
AutomationElement formSecurityFacility = null;
AutomationElement formEcuInfo = null;
string successTip = null;
AutomationElement root = AutomationElement.RootElement;
AutomationElementCollection coll = root.FindAll(TreeScope.Children, new
PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window));
txt_Status.AppendText("Start find DPS window" + "\r\n");(exe檔案運行時輸出Start find DPS Window 后就報錯了)
for (int numWait = 0; formDPS == null && numWait < 500; numWait++)
{
foreach (AutomationElement i in coll)
{
string name1 = (string)(i.GetCurrentPropertyValue(AutomationElement.NameProperty));
if (name1.Contains("Development Programming System"))
//由于打開子視窗后第一層視窗的名稱會“Development Programming System-Programming”,因此此處通過命名包含關系查找
{
formDPS = i;
Console.WriteLine(formDPS.GetCurrentPropertyValue(AutomationElement.NameProperty) + "for GCI");
}
}
}
exe檔案運行報錯截圖如下所示:

上圖報錯的詳細資訊如下:
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.Windows.Automation.ElementNotAvailableException: The target element corresponds to UI that is no longer available (for example, the parent window has closed).
at MS.Internal.AutomationProxies.Misc.ThrowWin32ExceptionsIfError(Int32 errorCode)
at MS.Internal.AutomationProxies.Misc.GetClassName(IntPtr hwnd)
at MS.Internal.AutomationProxies.WindowsFormsHelper.IsWindowsFormsControl(IntPtr hwnd, FormControlState& state)
at MS.Internal.AutomationProxies.ProxyHwnd.GetElementProperty(AutomationProperty idProp)
at MS.Internal.AutomationProxies.ProxySimple.System.Windows.Automation.Provider.IRawElementProviderSimple.GetPropertyValue(Int32 propertyId)
at MS.Internal.Automation.UiaCoreApi.CheckError(Int32 hr)
at System.Windows.Automation.AutomationElement.GetCurrentPropertyValue(AutomationProperty property, Boolean ignoreDefaultValue)
at System.Windows.Automation.AutomationElement.GetCurrentPropertyValue(AutomationProperty property)
at Panel.Pannel.GetEcuInfo(String EcuAddress)
at Panel.Pannel.Programming()
at Panel.Pannel.<btnStart_Click>d__47.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.<ThrowAsync>b__6_0(Object state)
************** Loaded Assemblies **************
mscorlib
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4180.0 built by: NET48REL1LAST_B
CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
Panel
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///D:/sgmuserprofile/s9b5ga/Desktop/Panel.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4180.0 built by: NET48REL1LAST_B
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4001.0 built by: NET48REL1LAST_C
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
Assembly Version: 4.0.0.0
Win32 Version: 4.8.3761.0 built by: NET48REL1
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
Assembly Version: 4.0.0.0
Win32 Version: 4.8.3761.0 built by: NET48REL1
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Core
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4180.0 built by: NET48REL1LAST_B
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
System.Xml
Assembly Version: 4.0.0.0
Win32 Version: 4.8.3761.0 built by: NET48REL1
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
Accessibility
Assembly Version: 4.0.0.0
Win32 Version: 4.8.3761.0 built by: NET48REL1
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Accessibility/v4.0_4.0.0.0__b03f5f7f11d50a3a/Accessibility.dll
----------------------------------------
UIAutomationClient
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4180.0 built by: NET48REL1LAST_B
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/UIAutomationClient/v4.0_4.0.0.0__31bf3856ad364e35/UIAutomationClient.dll
----------------------------------------
UIAutomationTypes
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4180.0 built by: NET48REL1LAST_B
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/UIAutomationTypes/v4.0_4.0.0.0__31bf3856ad364e35/UIAutomationTypes.dll
----------------------------------------
UIAutomationProvider
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4180.0 built by: NET48REL1LAST_B
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/UIAutomationProvider/v4.0_4.0.0.0__31bf3856ad364e35/UIAutomationProvider.dll
----------------------------------------
UIAutomationClientsideProviders
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4180.0 built by: NET48REL1LAST_B
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/UIAutomationClientsideProviders/v4.0_4.0.0.0__31bf3856ad364e35/UIAutomationClientsideProviders.dll
----------------------------------------
WindowsBase
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4180.0 built by: NET48REL1LAST_B
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/WindowsBase/v4.0_4.0.0.0__31bf3856ad364e35/WindowsBase.dll
----------------------------------------
************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.
For example:
<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>
When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.
報錯代碼段
private void GetEcuInfo(String EcuAddress)
{
txt_Status.AppendText("Start GetEcuInfo" + "\r\n");
AutomationElement formDPS = null;
//AutomationElement formProgramming = null;
AutomationElement formSelectJ2534 = null;
AutomationElement formGCIErrorTip = null;
AutomationElement NoVINTip = null;
AutomationElement formSecurityFacility = null;
AutomationElement formEcuInfo = null;
string successTip = null;
txt_Status.AppendText("Initialize" + "\r\n");
AutomationElement root = AutomationElement.RootElement;
txt_Status.AppendText("Get desktop" + "\r\n");
AutomationElementCollection coll = root.FindAll(TreeScope.Children, new
PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window));
txt_Status.AppendText("Start find DPS window" + "\r\n");
for (int numWait = 0; formDPS == null && numWait < 500; numWait++)
{
foreach (AutomationElement i in coll)
{
string name1 = (string)(i.GetCurrentPropertyValue(AutomationElement.NameProperty));
if (name1.Contains("Development Programming System"))
{
formDPS = i;
Console.WriteLine(formDPS.GetCurrentPropertyValue(AutomationElement.NameProperty) + "for GCI");
}
}
}
uj5u.com熱心網友回復:
嘗試下啟動外部程式除錯代碼屬性=》除錯=》啟動外部程式
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/238840.html
標籤:C#
下一篇:如何設計這個單例模式?
