C#WinForms应用程序中的计时器未按预期运行

C#WinForms应用程序中的计时器未按预期运行,c#,winforms,visual-studio,timer,C#,Winforms,Visual Studio,Timer,我正在使用Visual Studio 2013创建Visual C#Windows窗体应用程序,而不是使用设计器来设置窗体 我有一个简单的计时器,可以让我的应用程序暂停几秒钟,以便在用户看到并访问菜单之前显示启动屏幕。但是,当我运行debug时,应用程序不会出现在屏幕上,直到计时器完成,然后它跳过启动屏幕。我已经研究了使用计时器的不同方法,并尝试在这方面寻找帮助,但现在找不到使其工作的方法。我猜我错过了一些非常明显的东西,但作为一个新手,我无法发现它 任何帮助都将不胜感激 using Syste

我正在使用Visual Studio 2013创建Visual C#Windows窗体应用程序,而不是使用设计器来设置窗体

我有一个简单的计时器,可以让我的应用程序暂停几秒钟,以便在用户看到并访问菜单之前显示启动屏幕。但是,当我运行debug时,应用程序不会出现在屏幕上,直到计时器完成,然后它跳过启动屏幕。我已经研究了使用计时器的不同方法,并尝试在这方面寻找帮助,但现在找不到使其工作的方法。我猜我错过了一些非常明显的东西,但作为一个新手,我无法发现它

任何帮助都将不胜感激

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Timers;

namespace SimpleForm
{

    public class TheForm : Form
    {

        private MenuStrip menuStrip;
        private MainMenu menuMain;
        private MenuItem menuBlockFile;
        private MenuItem menuBlockOthers;
        private MenuItem menuItemExit;
        private MenuItem menuItemHints;

        static Bitmap imgIntroBg = null;
        static Bitmap imgMenuBg = null;

        private Panel pnlIntro;
        private Panel pnlMenu;

        static int counter = 1;
        static System.Timers.Timer timer;

        public TheForm()
        {

            FormInitialize();

            MenuInitialize();

            introDisplay();

            MenuDisplay(true);

        }

        private void FormInitialize()
        {

            System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Game));

            this.SuspendLayout();

            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(714, 462);
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.Name = "SimpleForm";
            this.Text = "Simple Form";
            ControlBox = false;
            BackColor = System.Drawing.Color.Black;
            StartPosition = FormStartPosition.CenterScreen;

            this.ResumeLayout(false);

        }


        private void MenuInitialize()
        {

            this.SuspendLayout();

            this.menuStrip = new MenuStrip();
            this.menuMain = new MainMenu();
            this.menuBlockFile = new MenuItem();
            this.menuBlockOthers = new MenuItem();

            this.menuItemExit = new MenuItem();
            this.menuItemHints = new MenuItem();

            this.menuMain.MenuItems.AddRange(new MenuItem[] {
                this.menuBlockFile,
                this.menuBlockOthers});

            this.menuBlockFile.Index = 0;
            this.menuBlockFile.MenuItems.AddRange(new MenuItem[] {
                this.menuItemExit
            } );
            this.menuBlockFile.Text = "File";

            this.menuBlockOthers.Index = 1;
            this.menuBlockOthers.MenuItems.AddRange(new MenuItem[] {
                this.menuItemHints
            });
            this.menuBlockOthers.Text = "Others";

            this.menuItemExit.Index = 0;
            this.menuItemExit.Text = "Exit";
            this.menuItemExit.Click += new System.EventHandler(this.menuItemExit_Click);

            this.menuItemHints.Checked = true;
            this.menuItemHints.Index = 0;
            this.menuItemHints.Text = "Temp";
            this.menuItemHints.Click += new System.EventHandler(this.menuItemExit_Click);

            this.Menu = this.menuMain;

            imgMenuBg = new Bitmap("graphics/layout/menubg.png");
            pnlMenu = new Panel();
            pnlMenu.Name = "pnlMenu";
            pnlMenu.Location = new Point(0, 0);
            pnlMenu.Width = 714;
            pnlMenu.Height = 462;
            pnlMenu.BackgroundImage = imgMenuBg;

            MenuDisplay(false);

            this.ResumeLayout(false);

        }

        private void MenuDisplay(bool display)
        {

            this.menuBlockFile.Visible = display;
            this.menuBlockOthers.Visible = display;
            if (display == true) {
                Controls.Add(pnlMenu);
            } else {
                Controls.Remove(pnlMenu);
            }

        }

        private void introDisplay()
        {

            timer = new System.Timers.Timer();
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            timer.Interval = 5*1000;
            timer.Enabled = true;

            imgIntroBg = new Bitmap("graphics/layout/Introbg.png");
            pnlIntro = new Panel();
            pnlIntro.Name = "pnlIntro";
            pnlIntro.Location = new Point(0, 0);
            pnlIntro.Width = 714;
            pnlIntro.Height = 482;
            pnlIntro.BackgroundImage = imgIntroBg;

            timer.Start();

            Controls.Add(pnlIntro);

            while (counter != 0)
            {

            }

            Controls.Remove(pnlIntro);

        }

        private static void timer_Elapsed(object source, ElapsedEventArgs e)
        {
            counter = 0;
            timer.Stop();
        }

        private void menuItemExit_Click(object sender, System.EventArgs e)
        {
            Application.Exit();
        }

    }

}
while(counter!=0)
部分不是正确的操作。当计时器完成时,
timer\u expressed
回调将被触发,因此您可以添加映像(
Controls.add(plnIntro);
),然后等待计时器的回调被触发。就在那时,一切都结束了。此时,只需卸下
plnIntro
,它就可以按预期操作了

您不必在循环时用
锁定线程,线程才能工作

尝试删除:

while (counter != 0)
{

}

Controls.Remove(pnlIntro);
然后放入
控件。移除(pnlIntro)

private void timer_Elapsed(object source, ElapsedEventArgs e)
{
    Controls.Remove(pnlIntro);
    timer.Stop();
}
while(counter!=0)
部分不是正确的操作。当计时器完成时,
timer\u expressed
回调将被触发,因此您可以添加映像(
Controls.add(plnIntro);
),然后等待计时器的回调被触发。就在那时,一切都结束了。此时,只需卸下
plnIntro
,它就可以按预期操作了

您不必在
循环时用
锁定线程,线程才能工作

尝试删除:

while (counter != 0)
{

}

Controls.Remove(pnlIntro);
然后放入
控件。移除(pnlIntro)

private void timer_Elapsed(object source, ElapsedEventArgs e)
{
    Controls.Remove(pnlIntro);
    timer.Stop();
}

对我来说,这件事在“Controls.Remove(pnlIntro);”一行中出现了几个错误。一个是
非静态字段、方法或属性'System.Windows.Forms.Control.Controls.get'
需要对象引用,第二个是
非静态字段、方法或属性'SimpleForm.TheForm.pnlIntro'
需要对象引用。很抱歉,没有看到计时器经过的回调是静态的,将修正答案!:)此外,计时器对象不必是静态的,我甚至建议它不应该是静态的。它运行时会在“Controls.Remove(pnlIntro);”上抛出一个错误。“InvalidOperationException未由用户代码处理”-“System.Windows.Forms.dll中发生类型为“System.InvalidOperationException”的异常,但未在用户代码中处理其他信息:跨线程操作无效:控件“pnlIntro”是从创建它的线程以外的线程访问的。”可能System.Timers.Timer创建了一个新线程,我不确定具体的类处理。试着改用System.Windows.Forms.Timer,看看这是否有帮助。对我来说,这会在“Controls.Remove(pnlIntro);”一行出现一些错误。一个是
非静态字段、方法或属性'System.Windows.Forms.Control.Controls.get'
需要对象引用,第二个是
非静态字段、方法或属性'SimpleForm.TheForm.pnlIntro'
需要对象引用。很抱歉,没有看到计时器经过的回调是静态的,将修正答案!:)此外,计时器对象不必是静态的,我甚至建议它不应该是静态的。它运行时会在“Controls.Remove(pnlIntro);”上抛出一个错误。“InvalidOperationException未由用户代码处理”-“System.Windows.Forms.dll中发生类型为“System.InvalidOperationException”的异常,但未在用户代码中处理其他信息:跨线程操作无效:控件“pnlIntro”是从创建它的线程以外的线程访问的。”可能System.Timers.Timer创建了一个新线程,我不确定具体的类处理。尝试改用System.Windows.Forms.Timer,看看是否有帮助。