3

I have created an WPF application that starts on windows start. Application checks for list of files on the system and if any file is missing it popups the file path.

The issue I am facing is when I restart the system while application is running, on restart my application crashes and throws System.Threading.ThreadAbortException. Below is the Stack Trace that I get in event logs

Application: FileValidator.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Threading.ThreadAbortException
Stack:
   at System.Windows.Markup.WpfXamlLoader.Load(System.Xaml.XamlReader, System.Xaml.IXamlObjectWriterFactory, Boolean, System.Object, System.Xaml.XamlObjectWriterSettings, System.Uri)
   at System.Windows.Markup.WpfXamlLoader.LoadBaml(System.Xaml.XamlReader, Boolean, System.Object, System.Xaml.Permissions.XamlAccessLevel, System.Uri)
   at System.Windows.Markup.XamlReader.LoadBaml(System.IO.Stream, System.Windows.Markup.ParserContext, System.Object, Boolean)
   at System.Windows.Application.LoadBamlStreamWithSyncInfo(System.IO.Stream, System.Windows.Markup.ParserContext)
   at System.Windows.Application.LoadComponent(System.Uri, Boolean)
   at System.Windows.Application.DoStartup()
   at System.Windows.Application.<.ctor>b__1(System.Object)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate)
   at System.Windows.Threading.DispatcherOperation.InvokeImpl()
   at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(System.Object)
   at System.Threading.ExecutionContext.runTryCode(System.Object)
   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode, CleanupCode, System.Object)
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.ProcessQueue()
   at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
   at MS.Win32.HwndWrapper.WndProc(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate)
   at System.Windows.Threading.Dispatcher.InvokeImpl(System.Windows.Threading.DispatcherPriority, System.TimeSpan, System.Delegate, System.Object, Int32)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(System.Windows.Interop.MSG ByRef)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame)
   at System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame)
   at System.Windows.Threading.Dispatcher.Run()
   at System.Windows.Application.RunDispatcher(System.Object)
   at System.Windows.Application.RunInternal(System.Windows.Window)
   at System.Windows.Application.Run(System.Windows.Window)
   at System.Windows.Application.Run()
   at FileValidator.App.Main()

What is the issue, has anyone faced this issue before ???

Leandro Bardelli
  • 10,561
  • 15
  • 79
  • 116
Sumit
  • 2,932
  • 6
  • 32
  • 54
  • How is the app being started when windows restarts? Can you run it up under the debugger and step through it? – Russell Troywest Dec 10 '11 at 21:22
  • @Russell: I am making registry entry to start app on windows start. I can run it under debugger but since issue is occurring only after machine restart, so there is no point in running under debugger. My app runs perfectly fine initially, the issue only occurs after restart of machine. – Sumit Dec 11 '11 at 08:05
  • Are you using the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run key? – Russell Troywest Dec 11 '11 at 09:17
  • Yes, I am using this key – Sumit Dec 11 '11 at 12:44
  • Are you using file system watching? Please post the code to verify the files on real time. – Leandro Bardelli Dec 12 '11 at 14:35
  • @Leandro : what do you mean by file system watching? I dont know what it is, sorry for my lack of knowledge. – Sumit Dec 13 '11 at 05:01
  • ok np. How do you check for the files? Are you checking for this files everytime, each 5 secs, when something happens with this files? If you are using file system watching and you force restart, it will crash – Leandro Bardelli Dec 13 '11 at 13:45
  • ok, for file checking I am simply using File.Exists(filepath), I check existing of the file after an interval defined by the user, which can vary from a second to hours. – Sumit Dec 13 '11 at 17:34

2 Answers2

3

It's impossible to answer definitively without more debugging info or code to look at, but it sounds like an issue with the CLR. For example, if your application is executing before the CLR runtime is fully initialized, you could run into an exception like the one you're getting here. Since WPF is primarily managed code, I'd say this is a distinct possibility.

I would recommend you move the startup entry from HKLM to HKCU. This way, it should be loaded when the user logs-on instead of on Windows startup, which should mean that the CLR will be initialized by then.

If that doesn't work, MSDN recommends that you do a try/catch on ThreadAbortExceptions. I can't remember off the top of my head if WPF supports handling or not, but if so then I'd say that would be your best bet if moving execution to the user login portion of the boot stack doesn't fix it.

Useful Sources:

http://msdn.microsoft.com/en-us/library/system.threading.threadabortexception.aspx

Is CLR loaded and initialized everytime,when a new managed application is loaded?

Community
  • 1
  • 1
Kris Craig
  • 558
  • 1
  • 7
  • 19
  • Yeah, no doubt. WPF handles exceptions just fine. Write the exceptions to a log file or the event log. – Xcalibur37 Dec 13 '11 at 01:37
  • @kris and xcalibur37: I have done exception handling at each level, but its just that exception is not catched at any level. So, I am assuming exception does not arise from my application. I will try to move my start up entry from HKLM to HKCU and let you the results. – Sumit Dec 13 '11 at 05:04
  • @kris: there was no change in behavior after moving statup entry from HKLM to HKCU. I am still receiving errors after machine is restarted. – Sumit Dec 16 '11 at 06:41
0

Can you check some more details

  • Are there any inner exception?
  • Are there any entries in the system event log.
  • Please set up the performance counters and check for any failures(perfmon.exe) of clr
  • Hope you know about the events in Application class such as DispatcherUnhandledException .Please subscribe to those. That will help to catch exception which are missed in try{}catch{}
Joy George Kunjikkuru
  • 1,495
  • 13
  • 27
  • the stacktrace that i have provided in the question is from Event Logs. I will take a look at DispatcherUnhandledException, I didnt know about that. – Sumit Dec 13 '11 at 17:36