Read Win Clipboard inside a thread
-
I have a thread running that needs to read the Windows Clipboard but my Clipboard.GetDataObject() always returns null. I've read that Clipboard.GetDataObject() doesn't work in a thread (even though it's supposed to be thread safe), so I tried setting the apartment state by doing Thread.SetApartmentState(ApartmentState.STA) and still no luck. Anyone have any ideas I can try? The Main() function is declared with [STAThread] and I've even tried using [STAThread] to declare the function that tries to read the clipboard. Here's the background on the thread: The thread imports data from various formats (Excel, CSV, DBF, MDB, and I"m trying to get Word working) into a database. The best way I've seen to read Word Docs is to open the doc by creating a Word Application class, opening the doc in question, having word select severything and copy it to the clipboard. I'm running WinXP pro and Word 2007 (wasn't having much luck in the previous version of Word either) Here's the code that I use to call the thread:
m_ProcessThread = new Thread(new ParameterizedThreadStart(this.RunJob)); m_ProcessThread.SetApartmentState(ApartmentState.STA); m_ProcessThread.Start(NextUp); m_JobList.Sort(); m_JobList.ResetNextUp(); UpdateJobListView();
And here's my clipboard reader (straight from MSDN)IDataObject iData = Clipboard.GetDataObject();
Thanks! -
I have a thread running that needs to read the Windows Clipboard but my Clipboard.GetDataObject() always returns null. I've read that Clipboard.GetDataObject() doesn't work in a thread (even though it's supposed to be thread safe), so I tried setting the apartment state by doing Thread.SetApartmentState(ApartmentState.STA) and still no luck. Anyone have any ideas I can try? The Main() function is declared with [STAThread] and I've even tried using [STAThread] to declare the function that tries to read the clipboard. Here's the background on the thread: The thread imports data from various formats (Excel, CSV, DBF, MDB, and I"m trying to get Word working) into a database. The best way I've seen to read Word Docs is to open the doc by creating a Word Application class, opening the doc in question, having word select severything and copy it to the clipboard. I'm running WinXP pro and Word 2007 (wasn't having much luck in the previous version of Word either) Here's the code that I use to call the thread:
m_ProcessThread = new Thread(new ParameterizedThreadStart(this.RunJob)); m_ProcessThread.SetApartmentState(ApartmentState.STA); m_ProcessThread.Start(NextUp); m_JobList.Sort(); m_JobList.ResetNextUp(); UpdateJobListView();
And here's my clipboard reader (straight from MSDN)IDataObject iData = Clipboard.GetDataObject();
Thanks!I'm not really sure what you've read, but the only thing that could make sense is that it only works in a UI thread ("doesn't work in a thread" - every piece of code that's being executed is run in a thread...). If everything else fails and you're still convinced the threading is the culprit you could try marshalling all calls to
Clipboard.GetDataObject()
to the UI thread... (usingInvoke()
orBeginInvoke()
).Regards, mav -- Black holes are the places where God divided by 0...
-
I'm not really sure what you've read, but the only thing that could make sense is that it only works in a UI thread ("doesn't work in a thread" - every piece of code that's being executed is run in a thread...). If everything else fails and you're still convinced the threading is the culprit you could try marshalling all calls to
Clipboard.GetDataObject()
to the UI thread... (usingInvoke()
orBeginInvoke()
).Regards, mav -- Black holes are the places where God divided by 0...
You know, I really really should've thought of calling from the UI thread myself...:wtf: I'll give it a shot, I'm sure it'll work. Thank you very much!