c# - X分钟后黑屏

标签 c# .net

我认为我没有足够的休息时间,并且因为在计算机上而使眼睛有些紧张。我会尽我所能来做20分钟规则,但是一旦进入该区域,我往往会忘记这样做。

我想启动一个快速应用程序(如果有的话,请给我指点它),该应用程序会在20分钟后(可能在倒计时之前10秒)关闭屏幕或使其空白,或者迫使我休息一下。

不知道C#是否可以像这样访问api,也不确定它应该是控制台应用程序还是wpf应用程序或其他。它需要在启动时启动,并且可能位于任务栏中。

有什么建议么?

编辑

如果范围太广,这就是我的追求


X分钟后C#可以使屏幕空白吗?然后X秒钟后恢复正常。
要执行这样的定时任务,最好将带有
gui还是我可以使用控制台应用程序(尝试将其作为
尽可能快)

最佳答案

您可以使用WPF应用程序和Quartz.NET。

在这种情况下,您将能够向窗口添加自定义内容,例如时钟,并在屏幕被阻塞时显示。

MainWindow.xaml:

<Window x:Class="WPFBlankScreen.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"   
        WindowState="Minimized"
        Background="Orange" 
        KeyDown="Window_KeyDown"
        >
    <Grid>        
    </Grid>
</Window>


后台代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Init();
    }

    private void Init()
    {
        ISchedulerFactory schedFact = new StdSchedulerFactory();
        IScheduler sched = schedFact.GetScheduler();

        IDictionary<string, object> map = new Dictionary<string, object>();
        map.Add("window", this);

        IJobDetail job = JobBuilder.Create<ShowJob>()
         .WithIdentity("showJob", "group")             
         .UsingJobData(new JobDataMap(map))
         .Build();

        ITrigger trigger = TriggerBuilder.Create()
          .WithIdentity("showTrigger", "group")
          .StartNow()
          .WithSimpleSchedule(x => x.WithIntervalInMinutes(1).RepeatForever())
          .Build();

        sched.ScheduleJob(job, trigger);
        sched.Start();
    }

    private void Window_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.F11)
        {               
            this.Hide();
        }
    }
}


ShowJob类:

class ShowJob: IJob
{
    public void Execute(IJobExecutionContext context)
    {
        Window win = context.JobDetail.JobDataMap.Get("window") as Window;
        if (win != null)
        {
            Application.Current.Dispatcher.Invoke((Action)(() =>
            {
                win.Width = System.Windows.SystemParameters.FullPrimaryScreenWidth;
                win.Height = System.Windows.SystemParameters.FullPrimaryScreenHeight;
                win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                win.WindowStyle = WindowStyle.None;
                win.Topmost = true;
                win.WindowState = WindowState.Maximized;
                win.Show();
            }));

            IDictionary<string, object> map = new Dictionary<string, object>();
            map.Add("window", win);

            IJobDetail job = JobBuilder.Create<HideJob>()
             .WithIdentity("hideJob", "group")
             .UsingJobData(new JobDataMap(map))
             .Build();

            ITrigger trigger = TriggerBuilder.Create()
              .WithIdentity("hideTrigger", "group")
              .StartAt(DateBuilder.FutureDate(5, IntervalUnit.Second))
              .Build();

            context.Scheduler.ScheduleJob(job, trigger);
        }
    }
}


HideJob类:

class HideJob: IJob
{
    public void Execute(IJobExecutionContext context)
    {
        Window win = context.JobDetail.JobDataMap.Get("window") as Window;          
        if (win != null && Application.Current != null)
        {               
            Application.Current.Dispatcher.Invoke((Action)(() =>
            {
                win.Hide();
            }));
        }            
    }
}

关于c# - X分钟后黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14943036/

相关文章:

c# - 命令超时不工作

c# - 将内存映射与服务一起使用

c# - 非泛型类是否可能包含 .NET(C# 或 VB.NET)中的泛型列表?

c# - 基于winforms中的鼠标单击查找到对象的最短距离

.NET 4 无法安装,因为 SECUREREPAIR 无法 CreateContentHash 文件 SetupResources.dll : for computing hash Error: 997

java - .NET中Servlet和Applet的对应关系是什么?

c# - 计算在共享文件夹上运行的 .exe 的所有实例

c# - 如何在 C# 中验证 CSV?

c# - 如何更改 DatagridView 中的标题文本 - 在代码 C# 中?

c# - 使用 log4net 的正确方法(记录器命名)