timer 事件失效

miaojuan666 2010-09-14 11:56:46
情形一
主窗体FormA在Socket异步通信接收信息,若有报警信息,调用弹出窗体(FormB),FormB中有个Timer来控制窗体的弹出和慢慢的退去。但是窗体没有满满弹出,Timer的事件不再执行。
情形二
若一开始我用FormA的一个按钮来控制窗体的弹出,正常弹出,前提是没有用过情形一
情形三
如果已经有通过Socket消息触发的方式调用过FormB,情形二再按那个按钮FormB也无法弹出

好像Timer失效了一样,请高手帮忙分析一下,不胜感激!
...全文
234 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
jianshao810 2010-09-14
  • 打赏
  • 举报
回复
贴代码吧。事件没触发?
河北瑾航科技 2010-09-14
  • 打赏
  • 举报
回复
弹出代码执行了吗,设置断点执行一下
malun666 2010-09-14
  • 打赏
  • 举报
回复
首先确认:Timer的属性:enable为true时 事件才会响应。
其次是:可以使用多线程弹窗提示
glest 2010-09-14
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 miaojuan666 的回复:]

高手帮忙分析一下
我跟踪了一下timer,发现事件发生异常
由于以前的函数求值超时,函数求值被禁用。必须继续执行才能重新启用函数求值。

我在formLoad里面都使用了
System.Windows.Forms.Form.CheckForIllegalCrossThreadCalls = false;

结果还是不行
[/Quote]

不要使用System.Windows.Forms.Timer控件,换成System.Timers.Timer对象。
原因前者在主线程堵塞时,停止响应;而后者不占用主线程,因此总能触发。
miaojuan666 2010-09-14
  • 打赏
  • 举报
回复
高手帮忙分析一下
我跟踪了一下timer,发现事件发生异常
由于以前的函数求值超时,函数求值被禁用。必须继续执行才能重新启用函数求值。

我在formLoad里面都使用了
System.Windows.Forms.Form.CheckForIllegalCrossThreadCalls = false;

结果还是不行
miaojuan666 2010-09-14
  • 打赏
  • 举报
回复
贴点原代码如下
弹出窗体:

public TaskbarNotifier()
{
timer.Enabled = true;
timer.Tick += new EventHandler(timer_Tick);
}
public void Show(string strTitle, string strContent, int nTimeToShow, int nTimeToStay, int nTimeToHide)
{
WorkAreaRectangle = Screen.GetWorkingArea(WorkAreaRectangle);
titleText = strTitle;
contentText = strContent;
nVisibleEvents = nTimeToStay;
CalculateMouseRectangles();

// We calculate the pixel increment and the timer value for the showing animation
int nEvents;
if (nTimeToShow > 10)
{
nEvents = Math.Min((nTimeToShow / 10), BackgroundBitmap.Height);
nShowEvents = nTimeToShow / nEvents;
nIncrementShow = BackgroundBitmap.Height / nEvents;
}
else
{
nShowEvents = 10;
nIncrementShow = BackgroundBitmap.Height;
}

// We calculate the pixel increment and the timer value for the hiding animation
if (nTimeToHide > 10)
{
nEvents = Math.Min((nTimeToHide / 10), BackgroundBitmap.Height);
nHideEvents = nTimeToHide / nEvents;
nIncrementHide = BackgroundBitmap.Height / nEvents;
}
else
{
nHideEvents = 10;
nIncrementHide = BackgroundBitmap.Height;
}
taskbarState = TaskbarStates.hidden;
bool aaa = false;
switch (taskbarState)
{
case TaskbarStates.hidden:
taskbarState = TaskbarStates.appearing;
SetBounds(WorkAreaRectangle.Right - BackgroundBitmap.Width - 17, WorkAreaRectangle.Bottom - 1, BackgroundBitmap.Width, 0);
timer.Interval = nShowEvents;
timer.Start();
// We Show the popup without stealing focus
aaa=ShowWindow(this.Handle, 5);
break;

case TaskbarStates.appearing:
Refresh();
break;

case TaskbarStates.visible:
timer.Stop();
timer.Interval = nVisibleEvents;
timer.Start();
Refresh();
break;

case TaskbarStates.disappearing:
timer.Stop();
taskbarState = TaskbarStates.visible;
SetBounds(WorkAreaRectangle.Right - BackgroundBitmap.Width - 17, WorkAreaRectangle.Bottom - BackgroundBitmap.Height - 1, BackgroundBitmap.Width, BackgroundBitmap.Height);
timer.Interval = nVisibleEvents;
timer.Start();
Refresh();
break;
}
}
#region TaskbarNotifier Events Overrides
private void timer_Tick(object sender, EventArgs e)
{
switch (taskbarState)
{
case TaskbarStates.appearing:
if (Height < BackgroundBitmap.Height)
SetBounds(Left, Top - nIncrementShow, Width, Height + nIncrementShow);
else
{
timer.Stop();
Height = BackgroundBitmap.Height;
timer.Interval = nVisibleEvents;
taskbarState = TaskbarStates.visible;
timer.Start();
}
break;

case TaskbarStates.visible:
timer.Stop();
timer.Interval = nHideEvents;
// Added Rev 002
if ((bKeepVisibleOnMouseOver && !bIsMouseOverPopup) || (!bKeepVisibleOnMouseOver))
{
taskbarState = TaskbarStates.disappearing;
}
//taskbarState = TaskbarStates.disappearing; // Rev 002
timer.Start();
break;

case TaskbarStates.disappearing:
// Added Rev 002
if (bReShowOnMouseOver && bIsMouseOverPopup)
{
taskbarState = TaskbarStates.appearing;
}
else
{
if (Top < WorkAreaRectangle.Bottom)
SetBounds(Left, Top + nIncrementHide, Width, Height - nIncrementHide);
else
Hide();
}
break;
}

}

主窗体调用

#region 获取信息
private void sockUDP1_DataArrival(byte[] Data, IPAddress Ip, int Port)
{
................

taskbarNotifier1.Show(strData[0], strAlarm, 500, 10000, 500);
}
#endregion

private void button1_Click(object sender, EventArgs e)
{
taskbarNotifier1.Show("111", "222", 500, 10000, 500);
}

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧