Public Class Timer
Friend WithEvents Action As New System.Timers.Timer
Public lviAction As ListViewItem
Private Sub Action_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Action.Elapsed
frmMain.ncMain.BalloonTipTitle = lviAction.Group.Header
frmMain.ncMain.BalloonTipText = lviAction.Text & " > " & lviAction.SubItems.Item(0).Text
frmMain.lvActions.Items.Item(lviAction.Index).Remove()
End Sub
Public Sub StartTimer(ByVal lInterval As Long)
Action.Interval = lInterval
Action.AutoReset = False
Action.Start()
End Sub
End Class
In my main form, I have the following code to that creates the timer:
Private Sub Level1ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Level1ToolStripMenuItem.Click
If DialogInterval.ShowDialog = Windows.Forms.DialogResult.OK Then
'Add a new item in the listview for the new action
Dim NewAction As New ListViewItem
NewAction.Text = "Bank Management"
NewAction.Group = lvActions.Groups(0)
NewAction.SubItems.Add("Level 1")
NewAction.SubItems.Add(ActionDateTime)
lvActions.Items.Add(NewAction)
'Create a new timer for the new action
Dim Action As New Timer '(My class timer)
Action.lviAction = NewAction
Action.StartTimer(TimerInterval)
End If
End Sub
I'm getting the following error when the timer tick:
System.InvalidOperationException was unhandled by user code
Message="An error occurred creating the form. See Exception.InnerException for details. The error is: ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment."
Source="MyApp Browser"
StackTrace:
at MyApp_Browser.My.MyProject.MyForms.Create__Instance__[T](T Instance) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 190 at MyApp_Browser.My.MyProject.MyForms.get_frmMain() at MyApp_Browser.Timer.Action_Tick(Object sender, EventArgs e) in C:\Users\Ewinz87\Documents\Visual Studio 2008\Projects\MyApp Browser\My App Browser\Timer.vb:line 7 at System.Timers.Timer.MyTimerCallback(Object state)
InnerException: System.Threading.ThreadStateException
Message="ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment."
Source="System.Windows.Forms"
StackTrace:
at System.Windows.Forms.WebBrowserBase..ctor(String clsidString) at System.Windows.Forms.WebBrowser..ctor() at MyApp_Browser.frmMain.InitializeComponent() in C:\Users\Ewinz87\Documents\Visual Studio 2008\Projects\MyApp Browser\My App Browser\Form1.Designer.vb:line 51 at MyApp_Browser.frmMain..ctor()
InnerException:
Why am I getting this error and what should I do to fix it?
Edit: Been reading, still trying to fix this mess. I understand I my webbrowser control can only run in STA and from what it seems, when calling my class it's MTA. How do I change it so background work, the class thread is STA? Correct me if I'm wrong, I'm not familiar with all this threading stuff.
This post has been edited by Ewinz87: 11 March 2010 - 12:49 PM

New Topic/Question
Reply




MultiQuote





|