ThreadStateException when using QueueUserWorkItem in a Timer
- by Tim
Hi all,
I have a ThreadStateException in my winforms application.
Step to reproduce :
Create simple winforms app
Add a button
In click event, do :
timer1.Interval = 1000;
timer1.Tick += timer1_Tick;
timer1.Start();
void timer1_Tick(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(delegate
{
StringCollection paths = new StringCollection { @"c:\my.txt", @"c:\my.png" };
Clipboard.SetFileDropList(paths);
});
}
The exception tells me :
Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it.
But the main has already the [STAThread] attribute.
How to solve it ?
Thanks in advance for any help