WPF指定时间内不操作自动注销退出功能

当WPF程序10分钟不操作时,自动注销退出。

新建WPF应用程序(.net 4.5),命名为AutoLogoutNoOperate.

默认窗体MainWindow.xaml设计如图:

为窗体添加事件 Loaded="Window_Loaded" 和 Closing="Window_Closing"。

一、新建全局类配置GlobalUtil,用于记录最后操作时间 和 超时多少分钟配置。

GlobalUtil.cs源程序如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AutoLogoutNoOperate
{
    /// <summary>
    /// 全局变量类
    /// </summary>
    public class GlobalUtil
    {
        /// <summary>
        /// 静态构造函数,只执行一次
        /// </summary>
        static GlobalUtil()
        {
            LastOperateTime = DateTime.Now;
            AdminTimeoutMinute = 10;
        }
        /// <summary>
        /// 最后一次操作时间
        /// </summary>
        public static DateTime LastOperateTime { get; set; }

        /// <summary>
        /// 超过10分钟不操作就注销退出【10分钟不操作就无法打开参数配置界面】
        /// </summary>
        public static int AdminTimeoutMinute { get; set; }
    }
}

二、窗体MainWindow.xaml的设计相关代码:

<Window x:Class="AutoLogoutNoOperate.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:AutoLogoutNoOperate"
        mc:Ignorable="d"
        Title="十分钟不操作界面,自动注销【自动切换到主界面,其他界面不激活】" Height="450" Width="800" Loaded="Window_Loaded" Closing="Window_Closing">
    <Grid>
        <Menu HorizontalAlignment="Left" Height="35" Margin="29,27,0,0" VerticalAlignment="Top" Width="448" Grid.ColumnSpan="6">
            <Button Content="主界面" x:Name="BtnMain" FontSize="18" />
            <Button Content="参数配置" x:Name="BtnParaCfg" FontSize="18"/>
            <Button Content="工艺配置"  x:Name="BtnTechnologyCfg" FontSize="18" />
            <Button Content="其他配置" x:Name="BtnOtherCfg" FontSize="18"/>
        </Menu>
        <DockPanel x:Name="PnlMain" HorizontalAlignment="Left" Height="306" LastChildFill="False" Margin="27,67,0,0" VerticalAlignment="Top" Width="727" Grid.ColumnSpan="7">
            <Label Content="这是【主界面】内容" FontSize="18" HorizontalAlignment="Left" Margin="80,12,0,0" VerticalAlignment="Top" Width="200"/>
        </DockPanel>
        <DockPanel x:Name="PnlParaCfg" HorizontalAlignment="Left" Height="306" LastChildFill="False" Margin="27,67,0,0" VerticalAlignment="Top" Width="727" Grid.ColumnSpan="7">
            <Label Content="这是【参数配置】内容" FontSize="18" HorizontalAlignment="Left" Margin="80,42,0,0" VerticalAlignment="Top" Width="200"/>
        </DockPanel>
        <DockPanel x:Name="PnlTechnologyCfg" HorizontalAlignment="Left" Height="306" LastChildFill="False" Margin="27,67,0,0" VerticalAlignment="Top" Width="727" Grid.ColumnSpan="7">
            <Label Content="这是【工艺配置】内容" FontSize="18" HorizontalAlignment="Left" Margin="80,72,0,0" VerticalAlignment="Top" Width="200"/>
        </DockPanel>
        <DockPanel x:Name="PnlOtherCfg" HorizontalAlignment="Left" Height="306" LastChildFill="False" Margin="27,67,0,0" VerticalAlignment="Top" Width="727" Grid.ColumnSpan="7">
            <Label Content="这是【其他配置】内容" FontSize="18" HorizontalAlignment="Left" Margin="80,102,0,0" VerticalAlignment="Top" Width="200"/>
        </DockPanel>
    </Grid>
</Window>


三、窗体对应的程序MainWindow.xaml.cs 源程序如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AutoLogoutNoOperate
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// 是否开启监控
        /// </summary>
        bool isMonitor = false;
        public MainWindow()
        {
            InitializeComponent();
            BtnMain.Click += Button_Click;
            BtnParaCfg.Click += Button_Click;
            BtnTechnologyCfg.Click += Button_Click;
            BtnOtherCfg.Click += Button_Click;
        }

        /// <summary>
        /// 每次点击按钮就认为维持心跳,更新 最后一次操作时间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            if (button == null)
            {
                return;
            }
            GlobalUtil.LastOperateTime = DateTime.Now;
            switch (button.Name)
            {
                case "BtnMain":
                    DisplayCurrentPanelOnly(PnlMain, BtnMain);
                    break;
                case "BtnParaCfg":
                    DisplayCurrentPanelOnly(PnlParaCfg, BtnParaCfg);
                    break;
                case "BtnTechnologyCfg":
                    DisplayCurrentPanelOnly(PnlTechnologyCfg, BtnTechnologyCfg);
                    break;
                case "BtnOtherCfg":
                    DisplayCurrentPanelOnly(PnlOtherCfg, BtnOtherCfg);
                    break;
            }
        }

        /// <summary>
        /// 只显示当前面板,其他面板隐藏
        /// </summary>
        /// <param name="panel"></param>
        private void DisplayCurrentPanelOnly(DockPanel panel, Button button)
        {
            //显示与隐藏面板
            DockPanel[] dockPanels = new DockPanel[] { PnlMain, PnlParaCfg, PnlTechnologyCfg, PnlOtherCfg };
            for (int i = 0; i < dockPanels.Length; i++)
            {
                if (dockPanels[i].Name == panel.Name)
                {
                    dockPanels[i].Visibility = Visibility.Visible;
                }
                else
                {
                    dockPanels[i].Visibility = Visibility.Hidden;
                }
            }
            //显示按钮颜色
            Button[] buttons = new Button[] { BtnMain, BtnParaCfg, BtnTechnologyCfg, BtnOtherCfg };
            for (int i = 0; i < buttons.Length; i++)
            {
                if (buttons[i].Name == button.Name)
                {
                    buttons[i].Foreground = new SolidColorBrush(Colors.Blue);
                }
                else
                {
                    buttons[i].Foreground = new SolidColorBrush(Colors.Black);
                }
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Button_Click(BtnMain, e);
            isMonitor = true;
            //启动任务线程:默认10分钟不操作即注销退出
            Task.Factory.StartNew(() =>
            {
                while (isMonitor)
                {
                    if (DateTime.Now.Subtract(GlobalUtil.LastOperateTime).TotalMinutes >= GlobalUtil.AdminTimeoutMinute)
                    {
                        LogoutWork();
                    }
                    System.Threading.Thread.Sleep(5000);
                }
            });
        }

        /// <summary>
        /// 注销操作:切换到主界面,禁用 其他配置界面
        /// </summary>
        void LogoutWork()
        {
            try
            {
                Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background, new Action(() => 
                {
                    Button_Click(BtnMain, null);
                    if (BtnParaCfg.IsEnabled)
                    {
                        BtnParaCfg.IsEnabled = false;
                    }
                    if (BtnTechnologyCfg.IsEnabled)
                    {
                        BtnTechnologyCfg.IsEnabled = false;
                    }
                    if (BtnOtherCfg.IsEnabled)
                    {
                        BtnOtherCfg.IsEnabled = false;
                        MessageBox.Show($"超过【{GlobalUtil.AdminTimeoutMinute}】分钟未操作,注销退出!", "注销退出");
                    }
                }));
            }
            catch { }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (MessageBoxResult.Yes != MessageBox.Show("确认要退出系统吗?", "", MessageBoxButton.YesNo, MessageBoxImage.Question))
            {
                e.Cancel = true;
                return;
            }
            isMonitor = false;
            System.Environment.Exit(0);
        }
    }
}


四、程序运行如图:

如果十分钟内不操作,配置按钮将被禁用,如图:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

斯内科

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值