Static Class/Method Stopping my application
I am having problems calling a static method from another method.
My static method is in a separate project, but I have checked the references and the using statements and everything seems right. Below is a simplified version.
The static method
namespace Backend
{
static public class StartUpChecks
{
public static void RunAtStart()
{
// Calls other static methods and sets application settings
}
}
}
The Windows Form
using Backend;
namespace UI
{
public partial class mainForm:Form
{
public mainForm()
{
InitializeComponent();
}
private void mainForm_Load(object sender, EventArgs e)
{
//MessageBox.Show("It Works");
StartUpChec开发者_如何学Cks.RunAtStart();
}
}
}
When I run the program it simply stops. I have set a breakpoint on the OnLoad event handler but it never gets hit. If I comment-in the MessageBox and comment-out the method call the event fires and the message box shows.
I have no errors showing in VS. I tried creating another method, Test(), and moved the StartUpChecks.RunAtStart() into it. Then I put the Test() call after the MessageBox. The event fires, the message box shows but it will not move on to the Test() method.
Also in VS when debugging I cannot restart the process and I can't step into/over anything.
Any ideas what I have done wrong?
EDIT---
The full Static Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataSourceManager;
using UserValidator;
namespace Backend
{
static public class StartUpChecks
{
public static void RunAtStart()
{
CheckUserAuthorised();
CheckUserAdmin();
SetConnection("myApplication");
}
private static void SetConnection(string appName)
{
AppControl.Connection = ConnectionSetter.SetConnectionString(appName);
}
private static void CheckUserAuthorised()
{
UserValidation checkMe = new UserValidation(AppControl.Connection);
AppControl.UserIsAuthorised = checkMe.UserIsAuthorised();
}
private static void CheckUserAdmin()
{
UserValidation checkMe = new UserValidation(AppControl.Connection);
AppControl.UserIsAdmin = checkMe.UserIsAdmin();
}
}
}
Please try to configure VS to handle thrown exceptions:
Main Menu -> Debug -> Exceptions => Set checkbox beside CLR Exceptions in the "Thrown" cell
Other point enable external code debuging
Main menu -> Tools -> Options -> Debugging -> Uncheck point Just in my code
After this changes try to run your application
There's a nasty bug in the interaction between the operating system and the 64-bit debugger. It causes exceptions that are raised in the Load event handler (or OnLoad method override) to be swallowed without a diagnostic.
Best way to bypass it is to use Project + Properties, Build tab, Platform target = x86. That's the default for VS2010 and also re-enables Edit + Continue. Nice. If running in 64-bit mode is really important then you can trap the exception with Debug + Exceptions, tick the Thrown box for Common Language Runtime exceptions.
This bug otherwise doesn't byte in your final program, it only goes wrong when a debugger is attached.
Try to catch the error on AppDomain.CurrentDomain.UnhandledException event. You add this code in your mainform constructor.
public MainForm()
{
InitializeComponent();
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
}
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
//Diagnose the exception here..
}
Additionally, you can try using a live windows client debugging and tracing utility "Donsole" for live tracing.
精彩评论