0

我在 C# 中有以下控制台应用程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.Windows.Forms;

namespace PreventScreenshot
{
    class Program
    {
        [STAThread]
        public static void Main(string[] args)
        {
            System.Timers.Timer timer1 = new System.Timers.Timer();
            timer1.Elapsed += new ElapsedEventHandler(timer_Tick);
            timer1.Interval = 1000;
            timer1.Enabled = true;
            timer1.Start();

            Console.WriteLine("---Prevent Screenshot from being taken---");
            Console.WriteLine();
            Console.WriteLine("DLL operation started.  Try to take a screenshot");
            Console.WriteLine();
            Console.WriteLine("Press enter to exit");
            Console.ReadLine();
        }

        public static void timer_Tick(object sender, EventArgs e)
        {
            Clipboard.Clear();
        }
    }
}

假设应用程序每秒清除剪贴板。但是,它不起作用。问题是什么?

编辑:

我刚刚编辑了代码。当我尝试运行程序时,剪贴板仍未清除。怎么了?

4

2 回答 2

6

移动Console.ReadLine()timer1.Start()

目前,您的应用程序正在等待输入,然后启动计时器并立即存在。

关于您编辑的帖子:

您正在使用Timerfrom System.Windows.Forms,它不适合控制台应用程序。尝试使用来自System.Timers.

于 2013-03-27T17:16:57.710 回答
2
  1. 您需要在 ReadLine() 调用之前设置并启动计时器,否则您的代码将无法访问它。

  2. 您的应用程序线程需要处于单线程单元 (STA) 模式。将 [STAThread] 属性应用于方法。请参阅:http: //msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.aspx

于 2013-03-27T17:19:00.490 回答