STA Error
-
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. -
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.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|
-
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.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