我想在每天的某个时刻定时弹出一个消息窗体。
主窗体里,我做了一个定时器Timer1.另外,消息窗体里,我想做渐进式弹出,所以消息窗体里也用了几个Timer控件来控制窗体的弹出和关闭。
但是,发现,主窗体里时间到了后,消息窗体无法正常弹出。消息窗体的load事件可以被调用,但是Timer响应函数无法运行。请问是什么问题呢?
主窗体里,我做了一个定时器Timer1.另外,消息窗体里,我想做渐进式弹出,所以消息窗体里也用了几个Timer控件来控制窗体的弹出和关闭。
但是,发现,主窗体里时间到了后,消息窗体无法正常弹出。消息窗体的load事件可以被调用,但是Timer响应函数无法运行。请问是什么问题呢?
我本意是在一天中两次渐进式地弹出消息框,第一次是在整个程序启动的时候,第二次是在当第一次弹出并关闭20分钟以后再弹出。
调试的时候发现,第一是可以正常弹出并自动关闭,但是20分钟以后,窗体从屏幕下方弹出了一丁点。但是渐进式弹出的Tick事件也没有被调用,然后整个成个程序也卡住了。
来点代码?嵌套调用,把主界面给卡死了吧
渐进不一定要用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();
}
}性能不高,只是个参考思路
//引用部分
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();
}
主窗体好像没什么问题看看消息框的Timer
this.moveUpTimer.Enabled = true;
this.moveUpTimer.Start();
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事件。第二次弹出窗体的时候,这个函数就进不来了。
this.waitingTimer.Enabled = true;
this.waitingTimer.Start();
不知道是不是你的waitingTimer的问题,导致整个界面卡住,先将它的Tick事件注释掉看看
问题难道是在主窗体的timer事件里不能再显示另外的一个窗体?还是说是由于线程,或者消息机制的原因?
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();
}
}
}
{
_timerShowMsg.Stop(); if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(delegate()
{
frmMessageForm frmMsg = new frmMessageForm();
frmMsg.Show(); //第二次弹出消息框
}));
}
else
{
frmMessageForm frmMsg = new frmMessageForm();
frmMsg.Show(); //第二次弹出消息框
}
}
Timer _timerShowMsg = new Timer(1200000); //事件间隔为20分钟_timerShowMsg .Elapsed += new ElapsedEventHandler(ShowMsg);
如果你这样编译成功了,那说明编译器并没有将Window.Forms.Timer和System.Timers.Timer两个类型混淆我试了下,是完全可以的,只不过声明的时候用的是类的完全限定名,只写类名编译都通不过
timersTimer.SynchronizingObject = this;
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忘了启用