How can I catch an application crash/ungraceful exit? - vb.net
I have a VB.NET WinForms application running from an executable stored on a network share. In that application, I have defined the UnhandledException handler in the ApplicationEvents (Private Sub MyApplication_UnhandledException(sender As Object, e As UnhandledExceptionEventArgs) Handles Me.UnhandledException). In my handler, I have a method that logs the exception details to a text file before prompting the user to confirm exiting the application.
However, this application "randomly" crashes and exits completely without any log created or message box displayed. This behavior happens at different executable points in the application and I'm trying desperately to track down the cause. I'm guessing that the problem may be due to either a temporary loss of network connectivity or some other issue communicating with the PostgreSQL database, but I can't confirm the source since there's no stack trace or message detail provided before the application disappears from the user's screen.
This should be "simple", but I'm at a loss as I've tried several things, including wrapping massive blocks of code in Try...Catch blocks and adding additional logging features to my error handler. I've tried rearranging the code in my UnhandledException handler to avoid any issues with new object instantiation (for my ErrorHandler object). I added a check in the error handling for logging the error locally if the network is unavailable. I've even added a simple message box to the FormClosing event of my main form if the closing wasn't directly initiated by the user to try to at least have the application do something before shutting down completely.
No matter what I've tried so far, the application still forcibly exits during seemingly random times. The user will be pressing a button to execute any of a number of methods that usually work normally. If the user relaunches the application after being kicked out and performs the exact same action again, it works without a problem. What I need to accomplish is some form of "idiot-proofing" the error handling so that whatever is causing the application's exit is caught and logged. I'm sure there are things I'm not thinking of at this point, so let me know if any further clarification is needed.
CODE
The application's Startup event handler:
Private Sub MyApplication_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
Try
Common.ApplicationStartup(ApplicationSettings.CurrentUser)
Catch ex As Exception
Dim StartupException As New ErrorHandler(ex)
StartupException.LogException()
MessageBox.Show("You do not have permission to access this resource." & vbCrLf & vbCrLf &
"The application will now exit.")
System.Environment.Exit(1)
End Try
' *********************************************************************
' ** Notify the user if the application is running in test mode. **
' *********************************************************************
If ApplicationSettings.TestMode Then
MessageBox.Show("This application is currently running in Test Mode, and will use " &
"local paths for data and configuration information." & vbCrLf & vbCrLf &
"If you are trying to use this application with live data and see " &
"this message, please contact the IT HelpDesk for assistance.", "TEST MODE",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
If ApplicationSettings.CurrentUser.Department = Users.Employee.Department.IS Then
If MessageBox.Show("Do you want to continue in Test Mode?", "TEST MODE", MessageBoxButtons.YesNo,
MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = DialogResult.No Then
ApplicationSettings.TestMode = False
End If
End If
End If
' *********************************************************************
' ** Initialize any application-specific settings here. **
' *********************************************************************
Try
'If ApplicationSettings.TestMode AndAlso ApplicationSettings.CurrentUser.Department = Users.Employee.Department.IS Then
' MessageBox.Show("If you have any additional parameters/settings to configure for this application, " &
' "please do so before commenting out this message.",
' "DEVELOPMENT WARNING", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
'End If
Catch ex As Exception
Dim ExHandling As New Common.ErrorHandler(ex)
ExHandling.LogException()
MessageBox.Show("There was a problem with initializing the application's configuration." & vbCrLf & vbCrLf &
"The application will now exit.")
System.Environment.Exit(2)
End Try
End Sub
The ApplicationStartup method:
Public Sub ApplicationStartup(ByRef CurrentUser As Users.Employee)
' *********************************************************************
' ** Default the TestMode variable to False. If the check for **
' ** whether or not the application is running from the IDE fails, **
' ** the application should assume that it's running live. **
' *********************************************************************
ApplicationSettings.TestMode = False
' *********************************************************************
' ** Perform a check of whether or not the application is running **
' ** from the IDE or the Debug folder. **
' *********************************************************************
SetTestMode()
' *********************************************************************
' ** Retrieve any parameters sent to the executable from the command **
' ** line and determine if the application is running from the task **
' ** scheduler. **
' *********************************************************************
ApplicationSettings.ScheduledTask = False
ApplicationSettings.RuntimeParameters = System.Environment.GetCommandLineArgs().ToList
If Not ApplicationSettings.RuntimeParameters Is Nothing AndAlso ApplicationSettings.RuntimeParameters.Count > 0 Then
For Each Parameter As String In ApplicationSettings.RuntimeParameters
If Parameter.ToUpper.Contains("SCHEDTASK") Then
ApplicationSettings.ScheduledTask = True
Exit For
End If
Next
End If
' *********************************************************************
' ** Set up the CurrentUser object by querying Active Directory and **
' ** the PostgreSQL database for details. **
' *********************************************************************
Try
If CurrentUser.ADUserName Is Nothing OrElse String.IsNullOrEmpty(CurrentUser.ADUserName) Then
CurrentUser = New Users.Employee(Environment.UserName)
End If
Catch UserEx As Exception
Dim ExHandler As New ErrorHandler(UserEx)
ExHandler.LogException()
Throw UserEx
End Try
If CurrentUser Is Nothing Then
Throw New Exception("Username " & Environment.UserName & " was not found in Active Directory.")
ElseIf CurrentUser.Enabled = False Then
Throw New Exception("Username " & Environment.UserName & " is not a currently active employee.")
End If
' *********************************************************************
' ** Default the DBCommandTimeout variable to 30. **
' *********************************************************************
ApplicationSettings.DBCommandTimeout = 30
End Sub
Private Sub SetTestMode()
' *********************************************************************
' ** Use the Debug.Assert to call the InTestMode function, which **
' ** will set the TestMode variable to True. Debug.Assert will only **
' ** execute if the program is running from a debugging version of **
' ** the code (in Design-Time, or from the Debug folder). When the **
' ** code is running from a compiled executable, the Debug.Assert **
' ** statement will be ignored. **
' *********************************************************************
Debug.Assert(InTestMode)
End Sub
Private Function InTestMode() As Boolean
' *********************************************************************
' ** Set the global TestMode variable to True. This function is **
' ** only called in debug mode using the Debug.Assert method in the **
' ** SetTestMode Sub. It will not be called if the application is **
' ** running from a compiled executable. **
' *********************************************************************
Common.ApplicationSettings.TestMode = True
Return True
End Function
The UnhandledException event handler:
Private Sub MyApplication_UnhandledException(sender As Object, e As UnhandledExceptionEventArgs) Handles Me.UnhandledException
Dim Response As DialogResult = DialogResult.Yes
Response = MessageBox.Show("An unknown error occurred in the application." & vbCrLf & vbCrLf &
"Do you want to exit the application?", "UNHANDLED EXCEPTION",
MessageBoxButtons.YesNo, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
Dim UnhandledError As New ErrorHandler(e.Exception)
UnhandledError.LogException()
If Response = DialogResult.Yes Then
e.ExitApplication = True
Else
e.ExitApplication = False
End If
End Sub
The main form's FormClosing event:
Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If Not e.CloseReason = CloseReason.UserClosing Then
MessageBox.Show("The application has encountered some sort of problem and is closing.")
End If
End Sub
Let me know if you want/need to see more code. As I said, the error occurs at seemingly random points in the application's execution and are inconsistent between one try or another.
UPDATE 7/1/2020
I haven't come back to this topic for a while because I moved a copy of the executable (and the supporting libraries) to the user's local drive and had her run the application from there. While she was using that copy, she wasn't "booted" from the program as described above (she had a few errors here and there, but those were all handled by my exception handling routine as expected).
Here we are a few months later and I've had cause to switch the user back to using the copy of the executable from the network share. I just received a report from the user that she's once again experiencing the issue with being randomly kicked out of the application without any warning or error and I'm not getting any of my exception "reports". Luckily for me, she's done a decent job of documenting the occurrences.
The strange thing is that sometimes these crashes occur when she's not doing anything "special". A couple of times they've happened when she simply clicks on one of the toolstrip menus to display a drop-down list of submenus. I've checked and there isn't any event handling code for these parent toolstrip menus, so it isn't like there are any queries or other instructions being executed. It should simply be displaying the submenu.
FWIW, a couple of weeks ago, we had a serious connectivity issue between our office and the server on which these executables are being stored (hosted VM's accessed via site-to-site VPN). I was getting around 10% packet loss across the VPN, even though I didn't see any packet loss anywhere else. I never found out what was causing the packet loss, but it appears to have been resolved and I can only assume that one of the ISP's between here and there had a faulty piece of equipment that they repaired/replaced. When I run a PING test to the server across the VPN, I'm not seeing any significant packet loss (maybe 1 packet out of several thousand) and response times of 15-35ms.
At this point, I'm only guessing (obviously), but I'm thinking that perhaps there's some sort of "time-out" occurring on the VPN connection that's causing a loss of connection to the code base. It's a total pS.W.A.G. ((pseudo) Scientific Wild-#$$ Guess), but I'm trying to come up with a viable solution to address the issue.
MY IDEAS
One thought is this: All of my in-house applications are run from this server and all of the supporting libraries for each are stored in the executable folder. Yes, this means that I've got multiple copies of many libraries stored on the server in various folders. I've been wanting to reduce this duplication, but I haven't really had/taken the time to figure out the best way to do so. At this point, I'm considering some sort of "installer" package for each of the workstations to drop the necessary libraries into each user's GAC (Global Assembly Cache) instead of accessing them through the VPN.
The only problem with this (that I can think of) is that there are several legacy systems that use different versions of the same libraries. For example, my current development is using Npgsql v4.1.3.1, but there are some applications that are still using v2.x and I don't really have time to go through every application to find which ones are/aren't using the current version and implement a version upgrade. That's just one of the many libraries where such an issue would arise, so I suppose I'd need to try to install all of the in-use versions to each GAC.
Another thought: Bring all of the executables back to a local server (not over the VPN) and change all of the shortcuts to point to that version instead of the one that requires the VPN. This, obviously, would have the benefit of less dependency on things like Internet connectivity and 3rd-party systems, as well as reduced latency.
The issue with this option, however, is that it's completely "unsupported" by my bosses. Their response when I've suggested something similar in the past is along the lines of "we're paying for a hosted server and they should support it..." Well, we all know that something like this may well be beyond the scope of any reasonable support request for a 3rd-party server host.
I'm really leaning towards the GAC option - at least as a first step - but I'll need to do a bit of research before I start traipsing down that road. Does anyone else have other suggestions for ways I might be able to deal with this? I'm really running out of ideas and I've got to find a real, workable and sustainable solution.
MORE INFO
I've implemented the suggestion below from #djv of wrapping the application's launch in a "startup" form that starts a new thread, but that still hasn't been able to catch whatever is causing the crash. The application still just periodically dies with absolutely no logging that I've been able to find so far.
I've also included, in the ApplicationEvents, a very simple handler for the NetworkAvailabilityChanged event to try to catch something happening there.
Private Sub MyApplication_NetworkAvailabilityChanged(sender As Object, e As NetworkAvailableEventArgs) Handles Me.NetworkAvailabilityChanged
If Not My.Computer.Network.IsAvailable Then
MessageBox.Show("Network connection has been lost.", "NETWORK CONNECTION TESTING", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
End Sub
Unfortunately, even that hasn't given me any additional insights as the user hasn't ever seen that MessageBox.
I have found an error in the user's Windows Event Logs that seems to correspond to the most recent event, but I'm not sure what it means, exactly:
EVENT ID 1000
---
Faulting application name: <EXECUTABLE_NAME>.exe, version: 1.0.0.0, time stamp: 0x9d491d36
Faulting module name: clr.dll, version: 4.8.4180.0, time stamp: 0x5e7d1ed7
Exception code: 0xc0000006
Fault offset: 0x000cc756
Faulting process id: 0xe570
Faulting application start time: 0x01d64fc245d7d922
Faulting application path: \\<SERVER_NAME>\<SHARE_PATH>\<EXECUTABLE_NAME>.exe
Faulting module path: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Report Id: 7016e3cc-7406-4854-95be-dbe3231447e7
Faulting package full name:
Faulting package-relative application ID:
This seems to indicate something in the CLR crapping out, but that really doesn't seem to give me any more information than I had before.
FURTHER DIAGNOSTICS/RESEARCH FOR SPECIFIC ISSUE
After digging around in the Event Logs some more, I found a couple more errors from around the time of the aforementioned event:
EVENT ID 1005
---
Windows cannot access the file for one of the following reasons: there is a problem with the network connection, the disk that the file is stored on, or the storage drivers installed on this computer; or the disk is missing. Windows closed the program <EXECUTABLE_NAME> because of this error.
Program: <EXECUTABLE_NAME>
File:
The error value is listed in the Additional Data section.
User Action
1. Open the file again. This situation might be a temporary problem that corrects itself when the program runs again.
2. If the file still cannot be accessed and
- It is on the network, your network administrator should verify that there is not a problem with the network and that the server can be contacted.
- It is on a removable disk, for example, a floppy disk or CD-ROM, verify that the disk is fully inserted into the computer.
3. Check and repair the file system by running CHKDSK. To run CHKDSK, click Start, click Run, type CMD, and then click OK. At the command prompt, type CHKDSK /F, and then press ENTER.
4. If the problem persists, restore the file from a backup copy.
5. Determine whether other files on the same disk can be opened. If not, the disk might be damaged. If it is a hard disk, contact your administrator or computer hardware vendor for further assistance.
Additional Data
Error value: C00000C4
Disk type: 0
And this:
EVENT ID 1026
---
Application: <EXECUTABLE_NAME>.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Runtime.InteropServices.SEHException
at <ROOT_NAMESPACE>.frmPayments.get_instance()
at <ROOT_NAMESPACE>.frmMain.tsmiProcessPayments_Click(System.Object, System.EventArgs)
at System.Windows.Forms.ToolStripItem.RaiseEvent(System.Object, System.EventArgs)
at System.Windows.Forms.ToolStripMenuItem.OnClick(System.EventArgs)
at System.Windows.Forms.ToolStripItem.HandleClick(System.EventArgs)
at System.Windows.Forms.ToolStripItem.HandleMouseUp(System.Windows.Forms.MouseEventArgs)
at System.Windows.Forms.ToolStripItem.FireEventInteractive(System.EventArgs, System.Windows.Forms.ToolStripItemEventType)
at System.Windows.Forms.ToolStripItem.FireEvent(System.EventArgs, System.Windows.Forms.ToolStripItemEventType)
at System.Windows.Forms.ToolStrip.OnMouseUp(System.Windows.Forms.MouseEventArgs)
at System.Windows.Forms.ToolStripDropDown.OnMouseUp(System.Windows.Forms.MouseEventArgs)
at System.Windows.Forms.Control.WmMouseUp(System.Windows.Forms.Message ByRef, System.Windows.Forms.MouseButtons, Int32)
at System.Windows.Forms.Control.WndProc(System.Windows.Forms.Message ByRef)
at System.Windows.Forms.ScrollableControl.WndProc(System.Windows.Forms.Message ByRef)
at System.Windows.Forms.ToolStrip.WndProc(System.Windows.Forms.Message ByRef)
at System.Windows.Forms.ToolStripDropDown.WndProc(System.Windows.Forms.Message ByRef)
at System.Windows.Forms.Control+ControlNativeWindow.OnMessage(System.Windows.Forms.Message ByRef)
at System.Windows.Forms.Control+ControlNativeWindow.WndProc(System.Windows.Forms.Message ByRef)
at System.Windows.Forms.NativeWindow.Callback(IntPtr, Int32, IntPtr, IntPtr)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG ByRef)
at System.Windows.Forms.Application+ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr, Int32, Int32)
at System.Windows.Forms.Application+ThreadContext.RunMessageLoopInner(Int32, System.Windows.Forms.ApplicationContext)
at System.Windows.Forms.Application+ThreadContext.RunMessageLoop(Int32, System.Windows.Forms.ApplicationContext)
at System.Windows.Forms.Application.Run(System.Windows.Forms.Form)
at <ROOT_NAMESPACE>.StartupForm.Main()
at System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
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.Threading.ThreadHelper.ThreadStart()
Doing some further research on the error code and exception information, it would seem that the problems are, in fact, due to the executable being loaded from the network share across the VPN.
Some of the information I found:
Unable to start applications from network “0xc0000006”
Performance penalties for .NET app running from shared network folder
How does IMAGE_FILE_NET_RUN_FROM_SWAP in an EXE file affect runtime libraries
This last one had me doing some research on PE file options and how to set them for a VB.NET application, but I wasn't finding enough to feel like pursuing that line of thought would provide enough benefit.
I guess this means that I need to do something to bring everything back across the VPN for execution. Perhaps I'll do some sort of actual local installation for the application (now I need to figure out how to actually do that, but that's well beyond the scope of this question). I'm not particularly happy about it, but at least I have an idea of what direction to take this from here.
BACK TO THE ORIGINAL QUESTION
However, this still doesn't answer my original question about how to catch and handle these app-killer exceptions. I'd be "fine" with the application crashing if it had at least given me some indication as to what happened at the time. I thought that the UnhandledException handler in the ApplicationEvents would catch even these, but further research into the SEHException at least helps me to get an idea of why it doesn't - see SEHException not caught by Try/Catch. I know it'd be a nightmare to try to define rules to handle or ignore every single type of exception that could possibly crop up in the UnhandledException event handler, but it would've been a whole lot nicer and more helpful for troubleshooting to at least see something.
Here's an idea. It looks like it might require some rewrite of how your application is started. But consider adding another "startup form" with an Application.Run à la C# void Main() { Application.Run(Form); }. You can wrap that in a Try-Catch and handle otherwise unhandled exceptions.
Add a form called StartupForm, and make that your application's entry point
And the StartupForm code
Imports System.Threading
Public Class StartupForm
Protected Sub StartupForm_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim applicationThread = New Thread(AddressOf Main)
applicationThread.SetApartmentState(ApartmentState.STA)
applicationThread.Start()
Dispose()
End Sub
Protected Shared Sub Main()
Using myForm As New MainForm()
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf UnhandledExceptionHandler
AddHandler Application.ThreadException, AddressOf UIThreadExceptionHandler
Application.EnableVisualStyles()
Try
Application.Run(myForm)
Catch ex As Exception
UnhandledExceptionHandler(myForm, New UnhandledExceptionEventArgs(ex, True))
End Try
End Using
End Sub
End Class
Module ExceptionHandlers
Public Sub UIThreadExceptionHandler(ByVal sender As Object, ByVal args As ThreadExceptionEventArgs)
MessageBox.Show(args.Exception.Message, NameOf(UIThreadExceptionHandler))
End Sub
Public Sub UnhandledExceptionHandler(ByVal sender As Object, ByVal args As UnhandledExceptionEventArgs)
MessageBox.Show(args.ExceptionObject.Message, NameOf(UnhandledExceptionHandler))
End Sub
End Module
StartupForm will set off a new thread before it disposes itself. The new thread starts a UI thread with your MainForm (whatever it's called - this is your current startup form).
To test it, you can throw an exception from a button press
Public Class MainForm
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Throw New Exception("Unhandled, from button on Form")
End Sub
End Class
and see that it's handled in initial Try-Catch
We add additional handlers in case the Try-Catch doesn't catch everything
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf UnhandledExceptionHandler
AddHandler Application.ThreadException, AddressOf UIThreadExceptionHandler
but I find that managed* exceptions are all caught.
*Of course, we won't be catching exceptions from unmanaged sources
I would make log file which is a txt file in your application host (debug folder) of the app so you could as developer check on it from time or send these log files to you remotely which is data collection from time to time to view code errors on client side and have general overview of what is crashing using the stack strace you can view code line that had error and have detailed describtion of the error, also
tutorial to using stream writer
https://www.c-sharpcorner.com/article/csharp-streamwriter-example/
so you would make it like to so for every code block that is prone to having error
Try
'Here your code is written for example dim a as integer=12
Catch ex as exception
' Here stream writer takes your ex.Message & ex.Stack Trace and adds it to the log file
'After that show message box saying
MessageBox.Show("Oops something crashed please try again ")
'That way you are handling both the user and developer
End Try
'For user you don't want the app to crash on him for you as developer you need the log 'file to trace errors and prevent them for future iterations of your program
'also at your assembly with each version make sure you have also make sure your log file has utc date time for when the error occured and include user that did it as well if available
Related
Weird Issue With Windows Service - Service Timing Out
Hi there I have piece of legacy (VS2010) Windows Service code that I have imported into VS2017 and is causing me severe frustration. This code has worked well for about the last 6 years, however when I carry out the install and attempt to start the service the SCM comes back with a timeout error. The OnStart code is as follows: Protected Overrides Sub OnStart(ByVal args() As String) 'Instaniate the timer for the service _serviceTimer = New Threading.Timer(New Threading.TimerCallback(AddressOf Tick), Nothing, 60000, 60000) End Sub The call back is: Private Sub Tick(ByVal state As Object) 'Switch off the timer event whilst the code executes _serviceTimer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite) If Not _started Then Startup() _started = True End If Call ServerProcess() 'Re-enable the timer now that application code has completed _serviceTimer.Change(_longInterval, _longInterval) End Sub I originally had the Startup process in the OnStart method, however removed it as an attempt at resolving this issue, however it has not made any difference. Method Startup is as follows: Public Sub Startup() Try 'Source these settings from the local config file _appDataFolder = Utilities.GetSetting("AppDataRoot") _configPathMapped = _appDataFolder & Utilities.GetSetting("ConfigPathMapped") _logPath = _appDataFolder & "\" & utl.GetSetting("LogPath") 'Instaniate the timer for the service - Commented out after moving startup code from OnStart method ' _serviceTimer = New Threading.Timer(New Threading.TimerCallback(AddressOf Tick), Nothing, Timeout.Infinite, Timeout.Infinite) 'Initialise logging architecture _logger = New aslLog.Logger(_configPathMapped & "nlog.config", _logPath, My.Application.Info.ProductName, My.Application.Info.Version.ToString) _logger.SendLog("Started PSALERTS Schedule Server Service", NLog.LogLevel.Info, _serviceTimer, _checkInterval, Nothing) 'Determine if the cloned config files exists in the mapped config file folder 'We clone these files to a writable destination to allow us to overcome write restrictions ot the C: drive on the SPEN PTI Desktop model If Not System.IO.File.Exists(_configPathMapped & "psaservermachine.config") Then 'Clone the app.config file in the config folder as psaservermachine.config Utilities.CloneFile(_programFileLocation & "PSALERTSScheduleServer.exe.config", _configPathMapped & "psaservermachine.config") End If If Not System.IO.File.Exists(_configPathMapped & "nlog.config") Then 'Clone the nlog.config file Utilities.CloneFile(_programFileLocation & "PSALERTSScheduleServer.exe.config", _configPathMapped & "nlog.config") End If 'Determine the Oracle TNS Environment 'Check for the existence of the environment variable 'TNS_ADMIN' If Environment.GetEnvironmentVariable("TNS_ADMIN") IsNot Nothing Then 'If TNS_ADMIN exists then we can continue with the application session Else Dim oraTnsPath As String = "" 'If it doesn't exist then we need to determine the Oracle information from the PATH environment variable oraTnsPath = GetOraTnsPath() If oraTnsPath <> "" Then 'Then create the TNS_ADMIN environment variable Environment.SetEnvironmentVariable("TNS_ADMIN", oraTnsPath) Else 'If no oracle client information exists then raise an error to this effect and exit the app 'informing the user that they need to install the Oracle client in order to use PSALERTS Beep() Throw New PSALERTSOracleConfigException( "PSALERTS Oracle Configuration Error. PSALERTS Did not find a valid Oracle Client." & vbCrLf & vbCrLf & "Please install a valid Oracle Client and try again." & vbCrLf & vbCrLf & "If a valid Oracle Client is installed then ensure that the PATH environment variable contains an entry for the Oracle Client." & vbCrLf & vbCrLf & "For example - TNS_ADMIN=C:\oracle\12.1.0\Client_lite\NETWORK\ADMIN" ) End If End If 'Register the application If Not Registered() Then 'Register the application Register() End If If Registered() Then 'Clean/close any stray Excel processes from previous debug session If _debugModeOn Then CleanUpRedundantProcesses("EXCEL", "PSALERTS") End If 'instantiate fresh excel session _myXLApp = New Excel.Application 'Get the timer interval settings _longInterval = CType(utl.GetSettingServerMachine(_configPath, "appSettings", "LongIntervalMillis"), Integer) _initInterval = CType(utl.GetSettingServerMachine(_configPath, "appSettings", "InitialIntervalMillis"), Integer) _refreshInterval = CType(utl.GetSettingServerMachine(_configPath, "appSettings", "InitialIntervalMillis"), Integer) 'Re-start the timer with periodic signalling as per the specified check interval _serviceTimer.Change(_initInterval, _initInterval) Else _started = False End If Catch ex As Exception _logger.SendLog("PSALERTS Schedule Server startup failure.", NLog.LogLevel.Error, ex) Finally End Try End Sub I use a similar technique for a number of similar services and they are running fine. Would appreciate some insight from any Windows Service gurus out there. Oh, I use WiX to carry out the install, again this is a well worn template for a number of similar such applications. Kind Regards Paul J.
Core: The very most typical errors: Config problems: connection strings, faulty paths, etc... Boot startup problem (good list - from FAQ) Wrong password / login account when running as a real user with password. Files missing or runtimes missing. Permission problems (ACL / NT Privilege missing). Maybe check this answer before the below. UPDATE: Maybe have a look at this previous answer. Service startup timing issue. Also check my ad-hoc answer there in the same page. Debugger: Other than that - nothing like stepping through the code with a debugger. I haven't done that in a long time. Deploy debug binaries and try? Windows 10 now hides messages from services - not sure how that affects debuggers: No more switching to Session 0. I am not a service guru, but a deployment specialist. I'll just provide some links and see if that helps. Maybe I have not fully understood the whole problem. I tend to focus on the deployment side and not so much development side. Ideas List / Debugging Check List: These are "ideas lists" for what could be wrong for applications in general - not just services (two first lists are similar - created some time apart): Crash on launch Desktop application won't launch General purpose WiX / MSI links Yes, these lists are very generic - too large to digest. Just skim the first two I think. Debugging Tools: Also a reminder of the most useful service debugging tools: Event Viewer, Task Manager, Services.msc, Process Explorer (system internals), The NET command and SC.exe. Good Service FAQ: https://www.coretechnologies.com/WindowsServices/FAQ.html
Your startup method should fire up a background worker and quickly return to the SCM that it has started. There is a system wide default setting of 30 seconds but honestly a proper service should respond in a few seconds. Looking though your code, your connection to the database is probably the long pole causing the problem.
Addhandler [process].Exited makes windows service to stop
Recently I've created a windows service which can help to trace the process start time and exit time, but I found that whenever the addhandler [process].exited executed, the windows service will stop Here are my codes for the handler For Each chrome_p As Process In NewChromeProccess chrome_p.EnableRaisingEvents = True AddHandler chrome_p.Exited, Sub() Using sw As StreamWriter = New StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\MonitoringApplication.txt", True) ChromeEndTime.Add(chrome_p.ExitTime) sw.WriteLine($"Process: {chrome_p.ProcessName}, Exit Time: {chrome_p.ExitTime}") End Using End Sub Next The description for Event ID 0 from source Service1 cannot be found. Either the component that raises this event is not installed on your local computer or the installation is corrupted. You can install or repair the component on the local computer. If the event originated on another computer, the display information had to be saved with the event. The following information was included with the event: Service cannot be started. An instance of the service is already running
After some investigation I think the problem you are encountering is due to the line sw.WriteLine($"Process: {chrome_p.ProcessName}, Exit Time: {chrome_p.ExitTime}") And, specifically, the chrome_p.ProcessName part. When I had the line as you wrote it above, I was getting the error message: Process has exited, so the requested information is not available. Switching that out with a string constant, I get the line Process: chrome_p.ProcessName, Exit Time: 5/2/2019 8:05:46 PM in the text log file.
Marshal.GetActiveObject fails with OPERATION UNAVAILABLE, HRESULT: 0x800401E3 exception if elevation levels mismatch. What can I do?
I am writing a VB.NET add-on for Solidworks, in which I first need to connect to an already running instance of Solidworks (similarly to how such connections are made to Excel). I am using Marshal.GetActiveObject method: Sub ConnectToSolidworks() Const PROG_ID As String = "SldWorks.Application" Dim progType = System.Type.GetTypeFromProgID(PROG_ID) If progType Is Nothing Then MSG("Cannot detect a valid Solidworks installation.") : End : Application.Exit() 'connect to Solidworks Try swApp = TryCast(System.Runtime.InteropServices.Marshal.GetActiveObject(PROG_ID), SolidWorks.Interop.sldworks.ISldWorks) Catch ex As Exception printException(System.Reflection.MethodBase.GetCurrentMethod().Name, ex) End Try 'check if connected If swApp Is Nothing Then MSG("Unable to connect to Solidworks.") End Application.Exit() End If End Sub Unfortunately, there is a problem. If Solidworks is launched as an Administrator, and my application is not, or vice versa, Marshal.GetActiveObject fails with HRESULT: 0x800401E3 exception. However, I need my application to work without administrative access, and regardless of whenever Solidworks was launched as administrator. The reason why I'm not using Interaction.CreateObject, Interaction.GetObject or System.Activator.CreateInstance, is because these methods will launch Solidworks on their own if it is not running already, which I want to avoid at all costs. With that in mind, how can I get Marshal.GetActiveObject to work when the elevation levels mismatch, or is there yet some another way to get this done? EDIT: I tried Interaction.GetObject again, and it also throws an exception if elevation levels mismatch ("Cannot create ActiveX component").
Monitor Executable Use
My goal is to set up a service to watch a network folder containing about 200 .exe files. What I'd like is to have the service update a log each time one of the .exes is launched. Basically I'd like to log usage of each application by recording every time one one of them is used. I've tried using the FileSystemWatcher class to accomplish this, code below, figuring that the LastAccess filter would do the trick, but it seems it won't. When I run this code no event is raised when the applications are opened. Is there some way of using the FileSysteWatcher class to do this kind of monitoring? Is there any way to do what I'm attempting? Private Sub StartWatch() Dim exeWatcher As New FileSystemWatcher exeWatcher.Path = "<path>" exeWatcher.Filter = "*.exe" exeWatcher.IncludeSubdirectories = True exeWatcher.NotifyFilter = (NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName Or NotifyFilters.Attributes) AddHandler exeWatcher.Changed, AddressOf ExeChanged exeWatcher.EnableRaisingEvents = True End Sub Private Sub ExeChanged(source As Object, e As FileSystemEventArgs) Console.WriteLine("File: " & e.FullPath & " " & DateTime.Now.ToString()) End Sub
Take a look at this Stack Overflow answer, which involves monitoring WMI Win32_Process instance creation events (basically, when WMI registers that a new process has been created). This is probably the most effective way outside of a C++ kernel hook to find out when a process has started. At that point, you just need to use a regular expression to test the file path against to see if it's originating from that folder, and respond appropriately if it is.
The file system watcher cannot be used to accomplish this because it doesn't know why the file is being accessed. It could be accessed to show the properties of the executable or someone copied it to their local hard drive. If your goal is to see what machines are running your executable, you can use Windows Management Instrumentation (WMI) to remotely query a machine for Win32_Process and determine if your process is running there.
Capturing Popups, Alerts, and Exceptions from Excel with VB 2010
This is my first time posting in stackoverflow. I started learning VB 2010 yesterday (though I do know some Ruby) and have googled this problem a lot but haven't found much. I've written a program that takes a CSV list of filepaths (all .XLS files) and attempts to open them. These excel workbooks were flagged as unreadable by another program; my program tries to capture the warning, popup, or exception that caused the error. It works for most exceptions (password protected, Unreadable Content, Dangerous File, etc.), but some popups that require you to click the 'OK' button to continue aren't caught. Maybe they aren't categorized as Exceptions or something, I'd love to know. It would be great to at least get the text form of them. Here is my script: Private Sub runReportButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles runReportButton.Click Dim Exl As New Excel.Application() Exl.Visible = False Exl.DisplayAlerts = False For Each row As DataGridViewRow In dgvReport.Rows If Not IsNothing(row.Cells(0).Value) Then If row.Cells(0).Value.ToString.Contains(".xls") Then Try 'Open the workbook and attempt to catch the error!' 'Use bogus passwords to cause Password Protected Error, Readonly disabled so the Password popup shows. Update links disabled because it wasn't being caught anyways' Dim wb As Excel.Workbook = Exl.Workbooks.Open(row.Cells(0).Value.ToString, [ReadOnly]:=False, [WriteResPassword]:="WWWWWWWWWWW", [Password]:="WWWXWXWWWXW", [UpdateLinks]:=False) row.Cells(4).Value = "Could not catch an error :(" wb.Close(0) dgvReport.Refresh() Catch ex As Exception 'Writes the Exception Message to the data grid view' row.Cells(4).Value = ex.Message End Try ProgressBar1.Value += 1 End If End If Exl.Quit() Next row dgvReport.Refresh() End Sub The reason I'm having a problem is that certain popups only occur when I open the file manually. Even with Excel set to visible and display alerts on, programmatically the file opens and closes without any popups. One particular warning is the following: "One or more worksheets in this workbook have names that contain square brackets:[]. To avoid unexpected results when referencing these worksheets, remove any square brackets from their names." It doesn't seem like a fatal error, but it causes the other program to flag it as unreadable because we cannot be certain of the integrity of the contents (i.e. the unexpected results). So my need isn't to ignore these popups or improve the other program, it's to capture this pesky popup message. If anyone can shed any light on the duality of this popup's behavior or provide a different approach to capturing alerts it would be much appreciated!