5

我坚持维护的一些遗留代码陷入了无限循环(因此我自己似乎在其中);不过,我不知道为什么/如何。

这是应用程序的入口点,它在其中实例化主窗体 (frmCentral):

代码附件 A

public static int Main(string [] args)
{
    try
    {
        AppDomain currentDomain = AppDomain.CurrentDomain;

        currentDomain.UnhandledException += new UnhandledExceptionEventHandler(GlobalExceptionHandler);

        string name = Assembly.GetExecutingAssembly().GetName().Name;
        MessageBox.Show(string.Format("Executing assembly is {0}", name)); // TODO: Remove after testing <= this one is seen
        IntPtr mutexHandle = CreateMutex(IntPtr.Zero, true, name);
        long error = GetLastError();
        MessageBox.Show(string.Format("Last error int was {0}", error.ToString())); // TODO: Remove after testing <= this one is also seen

        if (error == ERROR_ALREADY_EXISTS)
        {
            ReleaseMutex(mutexHandle);

            IntPtr hWnd = FindWindow("#NETCF_AGL_BASE_", null);
            if ((int) hWnd > 0)
            {
                SetForegroundWindow(hWnd);  
            }
            return 0;
        }

        MessageBox.Show("made it into Main method #4"); // TODO: Remove after testing <= this one is seen

        ReleaseMutex(mutexHandle);

        MessageBox.Show("made it into Main method #5"); // TODO: Remove after testing <= this one is seen

        DeviceInfo devIn = DeviceInfo.GetInstance();

        MessageBox.Show("made it into Main method #6"); // TODO: Remove after testing <= this one is seen

        Wifi.DisableWifi();

        MessageBox.Show("made it into Main method #7"); // TODO: Remove after testing <= this one is seen

        // Instantiate a new instance of Form1.
        frmCentral f1 = new frmCentral();
        f1.Height = devIn.GetScreenHeight();
        f1.Text = SSCS.GetFormTitle("SSCS HHS", "", "");

        MessageBox.Show("made it before Application.Run() in Main method"); // TODO: Remove after testing <= this one is NOT seen
        Application.Run(f1);

        devIn.Close();

        Application.Exit();
        return 0;
    }
    catch(Exception ex)
    {
        SSCS.ExceptionHandler(ex, "Main");
        return 0;
    }
} // Main() method

然后 frmCentral 的构造函数调用一个名为DBConnection.GetInstance()的(与荣耀相反的是什么?)单例方法:

代码附件 B

public frmCentral()
{
    try
    {
        //
        // Required for Windows Form Designer support
        //
        InitializeComponent();
        MessageBox.Show("made it past InitializeComponent() in frmCentral constructor"); // <= this displays
        devIn = DeviceInfo.GetInstance();
        MessageBox.Show("made it past DeviceInfo.GetInstance() in frmCentral constructor"); // <= this displays
        dbconn = DBConnection.GetInstance();
        MessageBox.Show("made it past DBConnection.GetInstance() in frmCentral constructor");
        WindowState = FormWindowState.Maximized;

        UpdateMenuItemSelectable = false;
        ResetConnectionFetchForm = false;

        AllowNewItems = true;

        listboxWork.Focus();
        MessageBox.Show("made it through frmCentral constructor"); // <= this one does NOT display
    }
    catch (Exception ex)
    {
        SSCS.ExceptionHandler(ex, "frmCentral()");
    }
} // frmCentral Constructor

这是“美化”的单例/单例方法:

代码展览 C

// Singleton pattern, or at least a derivation thereof
public static DBConnection GetInstance()
{
    MessageBox.Show("made it into DBConnection.GetInstance()");
    try
    {
        if (instance == null)
        {
            MessageBox.Show("made it into DBConnection.GetInstance(); instance was null");
            instance = new DBConnection();
        }
    }
    catch(Exception ex)
    {
        SSCS.ExceptionHandler(ex, "DBConnection.GetInstance");
    }
    return instance;
}

这会实例化 DBConnection,因此调用它的构造函数:

代码展览 D

private DBConnection()
{
    try
    {
        // Connection String
        string conStr = "Data Source = " + filename;
        string cmpStr = conStr + ".tmp";
        MessageBox.Show(string.Format("made it into DBConnection constructor. cmpStr == {0}", cmpStr)); // TODO: Comment out or remove
        if (File.Exists(filename+".tmp"))
            File.Delete(filename+".tmp");

        engine = new SqlCeEngine(conStr);
        MessageBox.Show(string.Format("SqlCeEngine created. conStr == {0}", conStr)); // TODO: Comment out or remove

        if (File.Exists(filename))
        {
            MessageBox.Show(string.Format("file {0} exists", filename)); // TODO: Comment out or remove
        }
        else
        {
            // Create the SQL Server CE database
            engine.CreateDatabase();
            MessageBox.Show("Made it past call to engine.CreateDatabase()"); // TODO: Comment out or remove
        }
        engine.Dispose();

        objCon = new SqlCeConnection(conStr);
        MessageBox.Show("Made it past call to new SqlCeConnection(conStr)"); // TODO: Comment out or remove

        objCon.Open();
    }
    catch(Exception ex)
    {
        SSCS.ExceptionHandler(ex, "DBConnection.DBConnection");
    }
}

我看到代码展览 A 中的 *MessageBox.Show()*s(除非另有说明),然后是代码展览 B,然后是代码展览 C,然后是代码展览 D,然后它在 C 和 D 之间来回切换“直到奶牛回家。”

我不明白为什么 DBConnection 构造函数和 DBConnection GetInstance() 递归地相互调用,虽然......我错过了大海捞针,还是隐藏在视线中的大象,或者......?? ?

更新

public static void ExceptionHandler(Exception ex, string location)
{
    try
    {
        MessageBox.Show("Exception: " + ex.Message + "\n\nLocation: " + location, GetFormTitle("SSCS: " + ex.GetType().FullName,"",""));
    }
    catch(Exception exc)
    {
        MessageBox.Show("Exception Handler generated an exception!\n" + exc.Message + "\n\nCalling Location: " + location, GetFormTitle("SSCS: " + exc.GetType().FullName,"",""));
    }
}

更新 2

这里有更多有启发性的晦涩之处:

public static string GetFormTitle(string formName, string serialNo, string siteNo)
{

    string titleBar = formName == "" ? "SSCS HHS" : formName;

    if((serialNo == ""))
    {
        User person = new User();
        person.getUserFromTable();
        serialNo = person.getSerialNo();
    }

    if (frmCentral.HashSiteMapping.ContainsKey(siteNo))
    {
        siteNo = (string) frmCentral.HashSiteMapping[siteNo];
    }


    if (serialNo != "")
        titleBar += " - " + serialNo + (siteNo == "" ? "" : " Site #" + siteNo);

    return titleBar;
}

更新 3

我添加的未捕获异常代码:

currentDomain.UnhandledException += new UnhandledExceptionEventHandler(GlobalExceptionHandler);

static void GlobalExceptionHandler(object sender, UnhandledExceptionEventArgs args)
{
    Exception e = (Exception)args.ExceptionObject;
    MessageBox.Show(string.Format("GlobalExceptionHandler caught {0}; Compact Framework Version == {1}", e.Message, Environment.Version.ToString()));
}

不过,我还没有看到任何证据表明曾经到达过这个处理程序(到目前为止,无论如何)。

更新 4

非常有趣 - 在 GetFormTitle 添加一个 MessageBox.Show(或两个)之后:

public static string GetFormTitle(string formName, string serialNo, string siteNo)
{
    MessageBox.Show(string.Format("GetFormTitle() reached. formName == {0}; serialNo == {1}; siteNo == {2}", formName, serialNo, siteNo)); // TODO: Remove after testing
    string titleBar = formName == "" ? "SSCS HHS" : formName;

    if((serialNo == ""))
    {
        User person = new User();
        person.getUserFromTable();
        serialNo = person.getSerialNo();
    }

    if (frmCentral.HashSiteMapping.ContainsKey(siteNo))
    {
        siteNo = (string) frmCentral.HashSiteMapping[siteNo];
    }


    if (serialNo != "")
        titleBar += " - " + serialNo + (siteNo == "" ? "" : " Site #" + siteNo);
    MessageBox.Show(string.Format("titleBar val about to be returned. Val is {0}", titleBar)); // TODO: Remove after testing
    return titleBar;
}

...这些是我现在看到的 MessageBox.Show() 的序列:

0) Made it into DBConnection.GetInstance() 
1) Made it into DBConnection.GetInstance() instance was null
2) Made it to DBConnection constructor cmpStr == ....
3) Sqlceengine created. conStr == ...
4) File \My Documents\HHSDB.SDF exists
5) Made it past call to new SqlCeConnection(conStr)
6) GetFormTitle() reached. fromName == SSCS:
System.Data.SqlserverCe.SqlCeException; serial No ==; siteNo ==

...随后是一轮更追尾的递归消息,直到我热启动(我讨厌与 Fab 4 相矛盾,但与流行的观点相反,幸福绝对不是热启动!*)

...所以就在 MessageBox 消息显示的中间,插入了一个异常!完全宾夕法尼亚嘻哈(关键说唱)!

* Let's have no attempts at humor revolving around warm booty, now!

那是远程桌面调试的最高级展示,LB2!

4

1 回答 1

6

这是可能发生的事情(此时是一个理论,直到 OP 可以确认)

开始问题的东西......

启动无限循环链的问题可能objCon.Open();在图表 D 中。连接到数据库可能存在一些问题,并且Open()调用应该抛出异常。这当然会被catch紧随该行的当地人抓住。

更新 1 中显示的本地catch调用SSCS.ExceptionHandler。它看起来是良性的,但其中隐藏了罪魁祸首的同谋,其名称GetFormTitle显示在更新 2 中。

事情变得更糟的地方......

GetFormTitle有一段非常有趣的代码:

User person = new User();
person.getUserFromTable();

...这很可能是从数据库(其他地方)检索用户信息的模型。

于是就创建了无限循环……

好吧,User要从数据库中获取,需要一个连接,这很可能会导致DBConnection.GetInstance()被调用,objCon.Open();它开始新的循环,因此创建了一个无限循环(巧妙地,不使用任何语言的内置循环机制,应适当注明)。

确认:

OP:请在 中放入非常简单的(意味着不要调用GetFormTitle MessageBoxes GetFormTitle,如果上述理论是正确的,您将在执行路径中看到它。

于 2014-05-02T22:21:58.680 回答