我想在每天的某个时刻定时弹出一个消息窗体。
主窗体里,我做了一个定时器Timer1.另外,消息窗体里,我想做渐进式弹出,所以消息窗体里也用了几个Timer控件来控制窗体的弹出和关闭。
但是,发现,主窗体里时间到了后,消息窗体无法正常弹出。消息窗体的load事件可以被调用,但是Timer响应函数无法运行。请问是什么问题呢?

解决方案 »

  1.   

    Start了。调试的时候,Start被调用了。但是Tick事件进不去。
    我本意是在一天中两次渐进式地弹出消息框,第一次是在整个程序启动的时候,第二次是在当第一次弹出并关闭20分钟以后再弹出。
    调试的时候发现,第一是可以正常弹出并自动关闭,但是20分钟以后,窗体从屏幕下方弹出了一丁点。但是渐进式弹出的Tick事件也没有被调用,然后整个成个程序也卡住了。
      

  2.   


    来点代码?嵌套调用,把主界面给卡死了吧
    渐进不一定要用Timer吧//Form的Load事件
    private   void   Login_Load(object   sender,   System.EventArgs   e)

     for(double   d=0d;   d <   1.0d;   d+=0.1d)   
     {   
       System.Threading.Thread.Sleep(50);   
       Application.DoEvents();   
       this.Opacity=d;   
       this.Refresh();   
     }   
    }性能不高,只是个参考思路
      

  3.   


    //引用部分
    using System;
    using System.Windows.Forms;
    using System.Collections.Generic;
    using System.Data;using System.Timers;
    using System.Drawing;//主窗体弹出消息框
    Timer _timerShowMsg = new Timer(1200000); //事件间隔为20分钟private void ShowMessageForm()
    {
      frmMessageForm frmMsg= new frmMessageForm ();
      frmMsg.Show();  _timerShowMsg .Elapsed += new ElapsedEventHandler(ShowMsg);
      _timerShowMsg .AutoReset = false; //时间到后仅调用一次,不重复调用
      _timerShowMsg .Enabled = true; //计时开始
    }private ShowMsg(object source,System.Timers.ElapsedEventArgs e)
    {
      _timerShowMsg.Enable= false; //计时器关闭
      frmMessageForm frmMsg= new frmMessageForm ();
      frmMsg.Show(); //第二次弹出消息框}//////消息框代码
    private void frmMessageForm_Load(object sender, EventArgs e)
    {
    //获取当前屏幕
               Screen currentScreen = Screen.AllScreens[0];        
              //设置窗口弹出初始位置
               this.StartPoint = new Point();
              this.StartPoint.X = currentScreen.WorkingArea.Width - this.Width;
              this.StartPoint.Y = Screen.AllScreens[0].WorkingArea.Height-25;            //设置窗口弹出终止位置
                this.EndPoint = new Point();
                this.EndPoint.X = currentScreen.WorkingArea.Width - this.Width;
                this.EndPoint.Y = Screen.AllScreens[0].WorkingArea.Height-this.Height-25;            //设置窗口初始位置
                this.Location = this.StartPoint;
               this.TopMost = true;
               
                this.moveUpTimer.Enabled = true;
                this.moveUpTimer.Start();
    }
      

  4.   

    谢谢你的思路。我这个消息窗体除了做渐进式弹出,还要求在显示一段时间后自动关闭,并且消息窗体里需要做一些图片切换闪烁的效果,所以就要用到Timer。不知道有没有其他好的办法。
      

  5.   

    本来还以为你忘写 _timerShowMsg.Enable= false; //计时器关闭这句
    主窗体好像没什么问题看看消息框的Timer
    this.moveUpTimer.Enabled = true;
    this.moveUpTimer.Start();
      

  6.   


    private void moveUpTimer_Tick(object sender, EventArgs e)
            {
                if (this.Location.Y > this.EndPoint.Y)
                {
                    Point tempPoint = new Point();
                    tempPoint.X = this.Location.X;
                    tempPoint.Y = this.Location.Y-5;
                    this.Location = tempPoint;
                }
                else
                {
                    this.moveUpTimer.Enabled = false;
                    this.moveUpTimer.Stop();                this.waitingTimer.Enabled = true;
                    this.waitingTimer.Start();
                }
            }这个是moveUpTimer的Tick事件。第二次弹出窗体的时候,这个函数就进不来了。
      

  7.   

    还是没发现明显的问题
    this.waitingTimer.Enabled = true;
    this.waitingTimer.Start();
    不知道是不是你的waitingTimer的问题,导致整个界面卡住,先将它的Tick事件注释掉看看
      

  8.   

    我把消息框里的所有timer都禁掉了,也就是说在到达时间点后只需要弹出消息框即可,不开启任何timer计时器,窗体控件中也不加载任何数据。发现还是不行,窗体弹出了一部分,就卡死在那里了。
    问题难道是在主窗体的timer事件里不能再显示另外的一个窗体?还是说是由于线程,或者消息机制的原因?
      

  9.   

    后来,我建立一个简单的程序进行测试:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;using System.Timers;namespace TimerTest
    {
        public partial class Form1 : Form
        {
            private System.Timers.Timer tShowForm2 = new System.Timers.Timer(5000); //时间间隔5秒
            public Form1()
            {
                InitializeComponent();        }        private void simpleButton1_Click(object sender, EventArgs e)
            {
                tShowForm2.Elapsed += new ElapsedEventHandler(ShowForm2);
                tShowForm2.AutoReset = false;
                tShowForm2.Enabled = true;
            }        private void ShowForm2(object source, System.Timers.ElapsedEventArgs e)
            {
                tShowForm2.Enabled = false;
                tShowForm2.Stop();
                Form2 frm = new Form2();
                frm.Show();
            }
        }
    }
      

  10.   

    你必须使用委托,timer实际上是在另外一个线程里void _timerShowMsg_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
                _timerShowMsg.Stop();            if (this.InvokeRequired)
                {
                    this.Invoke(new MethodInvoker(delegate()
                    {
                        frmMessageForm frmMsg = new frmMessageForm();
                        frmMsg.Show(); //第二次弹出消息框              
                    }));
                }
                else
                {
                    frmMessageForm frmMsg = new frmMessageForm();
                    frmMsg.Show(); //第二次弹出消息框
                }
            }
      

  11.   

    //主窗体弹出消息框
    Timer _timerShowMsg = new Timer(1200000); //事件间隔为20分钟_timerShowMsg .Elapsed += new ElapsedEventHandler(ShowMsg);
    如果你这样编译成功了,那说明编译器并没有将Window.Forms.Timer和System.Timers.Timer两个类型混淆我试了下,是完全可以的,只不过声明的时候用的是类的完全限定名,只写类名编译都通不过
      

  12.   

    17楼说的也有道理,如果你的主窗体Timer是在另外一个线程里面就不一样了
      

  13.   

    System.Timers.Timer是在.NET的Thread Pool上面运行的,而不是直接在UI Thread上面运行,所以在这种Timer的EventHandler里面进行耗时较长的计算不会导致UI失去响应。将System.Timers.Timer的SynchronizingObject属性赋于当前窗体,将自动同步
    timersTimer.SynchronizingObject = this;
      

  14.   


     private void ShowMessageForm()
          {
               //...
               _timerShowMsg.SynchronizingObject = this;
          }        private void ShowMsg(object source, System.Timers.ElapsedEventArgs e)
            {            
               _timerShowMsg.Enabled = false; //计时器关闭
                frmMessageForm frmMsg = new frmMessageForm();
               frmMsg.Show(); //第二次弹出消息框
                 _timerShowMsg.Enabled = true;
            }_timerShowMsg忘了启用