[C # / WPF] Use System.Timers.Timer timer to make floating window advertisement

Original: [C # / WPF] Use System.Timers.Timer timer for floating window advertisement

Demand: After the mouse is stationary for a period of time, a floating window advertisement is displayed.

Idea: The interface XAML writes a Canvas specifically for displaying floating window advertisements, first set it to invisible Visibility = ”Collapsed”, and then use the System.Timers.Timer class as a timer, set the timer to repeat once every second ), Increment the counter and judge whether the accumulated time to display the advertisement is reached, or modify it to Visibility = ”Visible” to display The movement event of the mouse bound to the interface is to reset the counter. The upper right corner of the ad is the close button, click the button to modify back to Visibility = "Collapsed".

The key codes are as follows:

Front desk:

<!-- 浮窗广告:主界面鼠标静止一段时间后,显示该广告 -->
<Canvas x:Name="adCanvas" Width="400" Height="200" Visibility="Collapsed">
    <!-- 关闭按钮 -->
    <Button Command="{Binding CloseAdCanvasCommand}" Canvas.Right="0" Canvas.Top="0">
        <Image Source="/项目名;component/Presentation/Resources/Images/tool.png" />
    </Button>
</Canvas>

Control layer key code

// 静止多少秒显示广告,写在了配置文件里
float adIntervalTime = float.Parse(ConfigurationManager.AppSettings["AD_WINDOW_INTERVAL_TIME"]);

#region 一段时间鼠标静止,浮窗广告
// 主界面鼠标移动事件。一段时间鼠标不移动,则浮窗广告
private void MouseMoveCommand()
{
    adStopTime = 0; // 重置计时器
}

// 开始浮窗广告的计时
private void StartAdWindowTimer()
{
    Timer timer = new Timer();
    timer.Elapsed += new ElapsedEventHandler(TimerTick);
    timer.Interval = 1000; // 每1秒重复检测一次
    timer.Enabled = true;
}

// 计算是否要显示浮窗广告
private void TimerTick(object sender, EventArgs e)
{
    adStopTime++;
    if (adStopTime >= adIntervalTime && allowADShow)
    {
        adCanvasChangeVisibility(true);
        allowADShow = false;
    }
}

// 改变浮窗广告Canvas的显隐
private void adCanvasChangeVisibility(bool visible)
{
    App.Current.Dispatcher.Invoke((Action)delegate // <--- HERE
    {
        if (visible)
            adCanvas.Visibility = Visibility.Visible;
        else
            adCanvas.Visibility = Visibility.Collapsed;
    });
}

// 初始化浮窗广告
private void InitAdCanvas()
{
    adCanvas = shellWindow.adCanvas;
    adCanvasChangeVisibility(false);

    // 背景是广告图片
    ImageBrush brush = new ImageBrush();
    brush.ImageSource = new BitmapImage(new Uri("pack://application:,,,/项目名;component/Presentation/Resources/Images/logo.jpg"));
    adCanvas.Background = brush;
}

// 关闭浮窗广告
private void CloseAdCanvasCommand()
{
    adCanvasChangeVisibility(false);
    allowADShow = true;
}
#endregion

For information, please refer to the use of System.Timers.Timer class in MSDN:

 

 

2018.3.12 update:

Another way of thinking is to use AutoResetEvent to control thread blocking and wakeup.

Copy code
private  void TestTimer () 
{ 
    AutoResetEvent autoResetEvent = new AutoResetEvent ( false );
     int limit = 5 ;
     int index = 0 ;
     while (! autoResetEvent.WaitOne (TimeSpan.FromSeconds ( 3 ))) // execute a loop every 3 seconds Body 
    { 
        index ++ ; 
        Console.WriteLine ( " index = " + index);
         if (index> = limit) 
        { 
            autoResetEvent.Set ();// Jump out of the loop 
        } 
    } 

    Console.WriteLine ( " Thread got signal " ); 
}
Copy code

 reference:

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12741775.html