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. Visual Basic
  4. STA Error

STA Error

Scheduled Pinned Locked Moved Visual Basic
comhelpannouncement
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
    maksim310
    wrote on last edited by
    #1

    Hi, I have a main startup form called MainForm and I declared an instance of a class that launches a timers.timer that needs to update the MainForm controls (textboxes, progressBars,...) so I created the following delegate in MainForm: Public Delegate Sub updateMainForm(ByRef neededChannel As Integer) and the following call from the Timers.Timer.Elapsed event: mainForm.Invoke(New mainForm.updateMainForm(AddressOf mainForm.determineIfKeepInAllOrIndividualChannel), New Object() {currentSTATE.CHANNEL}) and I get this error: An error occurred creating the form. See Exception.InnerException for details. The error is: 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. I looked into the error and they recommended putting a <STATThread>_ in the Main function but I don't have a main function. If there is a better way to communicate with the MainForm from the Elapsed event then that is fine as well. I'm looking into starting the Timer from a BackgroundThread and keep it alive with Monitor so that I can utilize both ProgressChanged and WorkCompleted, but there must be a simpler way.

    L M 2 Replies Last reply
    0
    • M maksim310

      Hi, I have a main startup form called MainForm and I declared an instance of a class that launches a timers.timer that needs to update the MainForm controls (textboxes, progressBars,...) so I created the following delegate in MainForm: Public Delegate Sub updateMainForm(ByRef neededChannel As Integer) and the following call from the Timers.Timer.Elapsed event: mainForm.Invoke(New mainForm.updateMainForm(AddressOf mainForm.determineIfKeepInAllOrIndividualChannel), New Object() {currentSTATE.CHANNEL}) and I get this error: An error occurred creating the form. See Exception.InnerException for details. The error is: 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. I looked into the error and they recommended putting a <STATThread>_ in the Main function but I don't have a main function. If there is a better way to communicate with the MainForm from the Elapsed event then that is fine as well. I'm looking into starting the Timer from a BackgroundThread and keep it alive with Monitor so that I can utilize both ProgressChanged and WorkCompleted, but there must be a simpler way.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, seems like you are using some component (a separate DLL?) that needs the thread to be STA. (what are those channels?) A Timers.Timer will fire on a separate Thread of which you don't have control. The easiest fix might be to switch to a Windows.Forms.Timer, which ticks on the GUI thread, which by default is STA IIRC. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      Voting for dummies? No thanks. X|


      1 Reply Last reply
      0
      • M maksim310

        Hi, I have a main startup form called MainForm and I declared an instance of a class that launches a timers.timer that needs to update the MainForm controls (textboxes, progressBars,...) so I created the following delegate in MainForm: Public Delegate Sub updateMainForm(ByRef neededChannel As Integer) and the following call from the Timers.Timer.Elapsed event: mainForm.Invoke(New mainForm.updateMainForm(AddressOf mainForm.determineIfKeepInAllOrIndividualChannel), New Object() {currentSTATE.CHANNEL}) and I get this error: An error occurred creating the form. See Exception.InnerException for details. The error is: 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. I looked into the error and they recommended putting a <STATThread>_ in the Main function but I don't have a main function. If there is a better way to communicate with the MainForm from the Elapsed event then that is fine as well. I'm looking into starting the Timer from a BackgroundThread and keep it alive with Monitor so that I can utilize both ProgressChanged and WorkCompleted, but there must be a simpler way.

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

        I looked into the error details and it came out with this: System.InvalidOperationException was unhandled by user code Message="An error occurred creating the form. See Exception.InnerException for details. The error is: 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." Source="ScienceProgram_v2" StackTrace: at ScienceProgram_v2.My.MyProject.MyForms.Create__Instance__[T](T Instance) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 190 at ScienceProgram_v2.My.MyProject.MyForms.get_mainForm() at ScienceProgram_v2.startExperimentClass.STATUSupdate_MainForm() in G:\ProgrammingFolder\ScienceProgram\ScienceProgram_v2\neededClasses\startExperimentClass.vb:line 1229 at ScienceProgram_v2.startExperimentClass.endExperiment(Boolean& experimentCanceled) in G:\ProgrammingFolder\ScienceProgram\ScienceProgram_v2\neededClasses\startExperimentClass.vb:line 1190 at ScienceProgram_v2.startExperimentClass.sendCommands() in G:\ProgrammingFolder\ScienceProgram\ScienceProgram_v2\neededClasses\startExperimentClass.vb:line 1115 at ScienceProgram_v2.startExperimentClass.repeatingTimer_elapsed(Object source, ElapsedEventArgs e) in G:\ProgrammingFolder\ScienceProgram\ScienceProgram_v2\neededClasses\startExperimentClass.vb:line 1094 at System.Timers.Timer.MyTimerCallback(Object state) InnerException: System.Threading.ThreadStateException Message="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." Source="System.Windows.Forms" StackTrace: at System.Windows.Forms.ComboBox.set_AutoCompleteSource(AutoCompleteSource value) at ScienceProgram_v2.mainForm.InitializeComponent() in G:\ProgrammingFolder\ScienceProgram\ScienceProgram_v2\mainForm.Designer.vb:line 843 at ScienceProgram_v2.mainForm..ctor() InnerException: Maybe something here? The only DLLs that I use are the Krypton Toolkit and the DevExpress Xtra editors. Everything else is standard Microsfot stuff. -Max

        modified on Monday, July 14, 2008 4:07 AM

        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