ThreadStateException when using QueueUserWorkItem in a Timer

Posted by Tim on Stack Overflow See other posts from Stack Overflow or by Tim
Published on 2010-06-07T14:19:19Z Indexed on 2010/06/07 14:22 UTC
Read the original article Hit count: 226

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about c#

Related posts about thread