【发布时间】:2014-04-23 03:43:57
【问题描述】:
我有一个 C# 程序,它应该逐行遍历日志文件,然后根据读取的内容更新标签,但是,我得到了一个未处理的异常。
它正在读取的文件来自另一个正在写入日志的外部 c++ 程序(我希望这个 C# 包装程序能够将输出从文本重定向到这个窗口(这是我能想到的唯一方法of 是在 c++ 程序写入日志时从 c# 中读取日志。
代码如下:
p = new Process();
p.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory + "\\scan.cmd";
p.Start();
//p.WaitForExit();
// Read the file and display it line by line.
string logFile = AppDomain.CurrentDomain.BaseDirectory + "\\log.txt";
string line;
System.IO.StreamReader file = new System.IO.StreamReader(logFile);
while((line = file.ReadLine()) != null && p.HasExited == false)
{
itemBeiingScanned_Label.Content = line;
}
file.Close();
这里是例外(发生两次)-
在 PresentationFramework.dll 中发生了“System.Windows.Markup.XamlParseException”类型的第一次机会异常
附加信息:对匹配指定绑定约束的“SpywareKing.MainWindow”类型的构造函数的调用引发了异常。
如果有这个异常的处理程序,程序可以安全地继续。
这里是调试代码:
64e35\PresentationFramework.dll'. Symbols loaded.
The thread 0x20c0 has exited with code 259 (0x103).
The thread 0x13b0 has exited with code 0 (0x0).
The thread 0x383c has exited with code 0 (0x0).
The thread 0x2838 has exited with code 259 (0x103).
'SpywareKing.vshost.exe' (CLR v4.0.30319: SpywareKing.vshost.exe): Loaded 'C:\Users\Jaycen\Documents\Dev\SpywareKing\SpywareKing\bin\x64\Release\SpywareKing.exe'. Symbols loaded.
'SpywareKing.vshost.exe' (CLR v4.0.30319: SpywareKing.vshost.exe): Loaded 'C:\windows\Microsoft.Net\assembly\GAC_MSIL\Accessibility\v4.0_4.0.0.0__b03f5f7f11d50a3a\Acc essibility.dll'. Symbols loaded.
SpywareKing.vshost.exe' (CLR v4.0.30319: SpywareKing.vshost.exe): Loaded C:\windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'. Symbols loaded.
'SpywareKing.vshost.exe' (CLR v4.0.30319: SpywareKing.vshost.exe): Loaded 'C:\windows\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework.Aero2\v4.0_4.0.0.0__31bf3856ad364e35\PresentationFramework.Aero2.dll'. Symbols loaded.
A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
A first chance exception of type 'System.Xaml.XamlObjectWriterException' occurred in System.Xaml.dll
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Additional information: The invocation of the constructor on type 'SpywareKing.MainWindow' that matches the specified binding constraints threw an exception.
An unhandled exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Additional information: The invocation of the constructor on type 'SpywareKing.MainWindow' that matches the specified binding constraints threw an exception.
The program '[14348] SpywareKing.vshost.exe' has exited with code 0 (0x0).
【问题讨论】:
-
您确定异常发生在您从日志中读取时,而不仅仅是在创建窗口时?把阅读器代码注释掉看看?
-
该异常似乎与 XAML / WPF 有关,它是您应用的演示(显示)框架。此代码是否在主窗口的构造函数中?也许尝试注释掉这段代码并确保程序运行,或者将代码移动到控制台应用程序以隔离。
-
另外 - 而不是读取文件(可能存在争用问题) - 您可以让您的 CMD 简单地将其结果回显到标准输出并使用您的 Process 对象的 .StandardOutput 属性(@987654324 @) 这是一个 StreamReader:msdn.microsoft.com/en-us/library/…
-
问题似乎是同一实例运行的后台进程太多,我不小心忘记关闭。但是,我现在的问题是它不会实时更新标签,只会更新一次。反正有实时更新吗?
-
但是您只读取文件一次。一种选择可能是使用 FileSystemWatcher。
标签: c# .net wpf io streamreader