为什么注释掉一行代码会改变是否执行* previous *行代码?

时间:2013-06-03 22:06:52

标签: c# .net-1.1

使用此代码:

    using (pbDialog = new pbDialogs())
    {
        ProgressBar = new frmProgress(this, false);
        ProgressBar.SetProgressLabelText("Inventory Data");
        MessageBox.Show("Set progress label text to Inventory data");
        typeProgress = (int) ProgressStates.ProgressQRY;

        ProgressBar.label1.Text += " (Receiving)";
        if (pbDialog != null)
        {
            MessageBox.Show("pbDialog is not null");
            //pbDialog.ShowDialog(ProgressBar, this);
        }
        else
        {
            MessageBox.Show("pbDialog IS null");
            ProgressBar.ShowDialog();
        }

        ProgressBar = null;

        MessageBox.Show("Made it to compressDB()");
        compressDB();
        . . .
     }

我看到“将进度标签文字设置为广告资源数据” 然后“ pbDialog不为空” 然后“使它成为compressDB()

那里没什么奇怪的;但如果我取消注释上面评论的行,我只看到“ pbDialog不为空

由于某种原因,它因为调用ShowDialog()而挂起;然而,真正奇怪的是,这会阻止显示“将进度标签文本设置为清单数据”。为什么会这样?

注意:我认为代码中的“pb”代表“花生脆”或其他一些;无论如何,我非常确定“脆弱”部分。

更新

是的,将ShowDialog()与pbDialog一起使用是原始程序员通过默默无闻地实施工作保障的一系列例子之一 - 然后他[un]幸运地匆匆忙忙地走了过来,留下了意大利面/蛋壳代码的污水池没有任何评论,误导性的名字和各种奇怪的,错综复杂的,在女巫酿造中可以想象的违反直觉的做法,他据称被认为是优雅设计的杰作和巧妙巧妙的技巧。

pbDialog是类的一个实例(pbDialogs)。只是为了让你尝试一下令人毛骨悚然,令人费解和纠结的一切,这就是那个课程:

public class pbDialogs : IDisposable
{
    private static Form m_top;

    public pbDialogs()
    {
    } // pbDialogs Constructor


    public static void Activate( Form form )
    {
        form.Capture = true;
        IntPtr hwnd  = OpenNETCF.Win32.Win32Window.GetCapture();
        form.Capture = false;
        OpenNETCF.Win32.Win32Window.SetForegroundWindow( hwnd );
    } // Activate

    /// <summary>
    /// This method makes ShowDialog work the way I want, I think.
    /// </summary>
    /// <remarks>
    /// Here is what it does:
    ///   1.  Sets the caption of the new window to the same as the caller's.
    ///   2.  Clears the caption of the parent so it won't show up in the task list.
    ///   3.  When the ShowDialog call returns, brings the previous window
    ///       back to the foreground.
    /// </remarks>
    /// <param name="dialog"></param>
    /// <param name="parent"></param>
    public void ShowDialog( Form dialog, Control parent )
    {
        Control top    = parent.TopLevelControl;
        string caption = top.Text;
        dialog.Text    = caption;
        top.Text       = "--pending--";                 // Don't show parent in task list
        dialog.Activated += new EventHandler( form_Activated );
        dialog.Closed    += new EventHandler( form_Closed );
        m_top = dialog;                         // New top-most form
        dialog.ShowDialog();
        m_top = (Form)top;                      // The top dialog just changed
        dialog.Activated -= new EventHandler( form_Activated );
        dialog.Closed    -= new EventHandler( form_Closed );
        top.Text = caption;                     // Make visible in task list again
        Activate( (Form)top );                  // And make it the active window
    } // ShowDialog

    /// <summary>
    /// If one of our other windows, such as the main window, 
    /// receives an activate event, it will activate the current 
    /// top-most window instead.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private static void form_Activated( object sender, EventArgs e )
    {
        if( (m_top != null) && !(sender == m_top) ) // Is this the top-most window?
            Activate( m_top );                      // No, activate the top dialog
    } // form_Activated

    private static void form_Closed( object sender, EventArgs e )
    {
        m_top = null;               // When you close the top dialog, it's not top anymore
    } // form_Closed

    #region IDisposable Members
    public void Dispose()
    {
        // TODO:  Add pbDialogs.Dispose implementation
    }
    #endregion
} // class pbDialogs

还有一个“相关的”ProgressBar - 一个与pbDialogs共享文件的表单,其实例变量在包含上述代码的文件中定义:

public static frmProgress ProgressBar;

这绝对是“打鼹鼠”的代码 - 如果我做出一个看似无害的小变化,所有达拉斯都会在一个半合理的人认为完全不相关的代码部分中解脱。

这可能表明这个代码/项目是多么松散:我会在注释掉几行后创建一个新版本,文件大小将从400KB变为408,或从412变为408对于.exe来说,通过如此小的改变来改变那么大的(相对意义上的)是不正常的行为,是吗?

更新2

这个,在frmProgress(它有“public class frmProgress:System.Windows.Forms.Form”和“public class pbDialogs:IDisposable”)让我感到害怕:

using System.Windows.Forms;
using OpenNETCF.Windows.Forms;

第二个(OpenNETCF)显示为灰色,表示它并未真正使用,但它可能是以前使用的,并且不知何故“Windows.Forms”代码无意中切换到“系统”代码,这可能是促成其目前的理由。

1 个答案:

答案 0 :(得分:3)

ShowDialog通常是阻止通话。在对话框关闭之前,代码不会继续执行此操作。