Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Read Win Clipboard inside a thread

Read Win Clipboard inside a thread

Scheduled Pinned Locked Moved C#
questiondatabaseannouncement
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MaxRelaxman
    wrote on last edited by
    #1

    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!

    M 1 Reply Last reply
    0
    • M MaxRelaxman

      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!

      M Offline
      M Offline
      mav northwind
      wrote on last edited by
      #2

      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... (using Invoke() or BeginInvoke()).

      Regards, mav -- Black holes are the places where God divided by 0...

      M 1 Reply Last reply
      0
      • M mav northwind

        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... (using Invoke() or BeginInvoke()).

        Regards, mav -- Black holes are the places where God divided by 0...

        M Offline
        M Offline
        MaxRelaxman
        wrote on last edited by
        #3

        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!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups