Reputation: 4607
I have the following console application in 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();
}
}
}
Supposedly, the application clears the clipboard every second. However, it is not working. What is the problem?
Edit:
I just edited the code. When I try to run the program, the clipboard is still not cleared. What is happening?
Upvotes: 0
Views: 2952
Reputation: 10591
You need to setup and start your timer BEFORE the ReadLine() call, otherwise your code will not reach it.
Your application threads need to be in Single Threaded Apartment (STA) mode . Apply the [STAThread] attribute to the method. See: http://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.aspx
Upvotes: 2
Reputation: 12654
Move Console.ReadLine()
after timer1.Start()
Currently, your application is waiting for an enter press, then starts the timer and exists immediately.
Regarding your edited post:
You are using Timer
from System.Windows.Forms
, it is not suitable for console applications. Try using timer from System.Timers
.
Upvotes: 6