An error occurred creating the form.

See Exception.InnerException for details.

Page 1 of 1

7 Replies - 9802 Views - Last Post: 16 March 2010 - 12:56 PM Rate Topic: -----

#1 Ewinz87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 25-May 09

An error occurred creating the form.

Posted 11 March 2010 - 09:52 AM

Hello, I'm posting here about this general problem because I've been struggling on this for well over 24 hours now and I'm completely lost as to what I need to do to fix this. Here's my situation. I have created a class which creates a system timer and execute code when it ticks.

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


Is This A Good Question/Topic? 0
  • +

Replies To: An error occurred creating the form.

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1053
  • View blog
  • Posts: 4,080
  • Joined: 02-July 08

Re: An error occurred creating the form.

Posted 11 March 2010 - 03:03 PM

You should try renaming your timer class - it might be clashing with System.Timers.Timer or Windows.Forms.Timer =)
Was This Post Helpful? 0
  • +
  • -

#3 Ewinz87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 25-May 09

Re: An error occurred creating the form.

Posted 12 March 2010 - 03:40 PM

View Posthawkvalley1, on 11 March 2010 - 02:03 PM, said:

You should try renaming your timer class - it might be clashing with System.Timers.Timer or Windows.Forms.Timer =)

Lol yeah name were confusing, I changed them, although it has nothing to do with my problem.

I ended up fixing it changing my class to create a new thread in the tick event and setting this thread to STA like this:

Private Sub ActionTimer_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles ActionTimer.Elapsed
        Me.ActionThread = New Thread( _
        New ThreadStart(AddressOf Me.Tick))
        Me.ActionThread.SetApartmentState(ApartmentState.STA)
        Me.ActionThread.Start()
    End Sub


And just replaced the stuff to do in a new sub.. named Tick. Anyway, problem solved, I was of course greated by another.. thread-safe.. if anyone know how this work and would be kind enough to explain in details to me. I've been reading on MSDN and everywhere but it's very complicated and the examples doesn't really suit me. Here's the error in details:

System.InvalidOperationException was unhandled
  Message="Cross-thread operation not valid: Control 'lvActions' accessed from a thread other than the thread it was created on."
  Source="System.Windows.Forms"
  StackTrace:
       at System.Windows.Forms.Control.get_Handle()    at System.Windows.Forms.ListView.GetDisplayIndex(ListViewItem item, Int32 lastIndex)    at System.Windows.Forms.ListViewItem.get_Index()    at MyApp_Browser.BotAction.Tick() in C:\Users\Ewinz87\Documents\Visual Studio 2008\Projects\MyApp Browser\MyApp Browser\Timer.vb:line 33    at System.Threading.ExecutionContext.runTryCode(Object userData)    at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)    at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 



Maybe worth noting lvActions is a ListView control on my main form. When the timer ticks, it removes the item by it's index which is provided earlier, the class having created a new timer for this item.
Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2262
  • View blog
  • Posts: 9,462
  • Joined: 29-May 08

Re: An error occurred creating the form.

Posted 12 March 2010 - 04:12 PM

Cross Thread operation is a frequent question on DIC so it is covered in the FAQ pinned at the vb.net forum.
Was This Post Helpful? 0
  • +
  • -

#5 Ewinz87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 25-May 09

Re: An error occurred creating the form.

Posted 12 March 2010 - 06:27 PM

View PostAdamSpeight2008, on 12 March 2010 - 03:12 PM, said:

Cross Thread operation is a frequent question on DIC so it is covered in the FAQ pinned at the vb.net forum.


Oh sorry for not reading then, I'm encountering a problem still however. My stuff is in a class and not in a background worker and the controls are in the main form. So I created the delegate and subs like this:

    ' The delegate
    Delegate Sub RemoveListViewItem_Delegate(ByVal [ListView] As ListView, ByVal [ListViewItem] As ListViewItem)

    ' The delegates subroutine.
    Private Sub RemoveListViewItem_ThreadSafe(ByVal [ListView] As ListView, ByVal [ListViewItem] As ListViewItem)
        ' InvokeRequired required compares the thread ID of the calling thread to the thread ID of the creating thread.
        ' If these threads are different, it returns true.
        If [ListView].InvokeRequired Then
            Dim MyDelegate As New RemoveListViewItem_Delegate(AddressOf RemoveListViewItem_ThreadSafe)
            Me.Invoke(MyDelegate, New Object() {[ListView], [ListViewItem]})
        Else
            Dim MyDelegate As New RemoveListViewItem_Delegate(AddressOf RemoveListViewItem_ThreadSafe)
            Me.Invoke(MyDelegate, New Object() {[ListView], [ListViewItem]})
        End If
    End Sub


Calling this with
RemoveListViewItem_ThreadSafe(frmMain.lvActions, lvItem)
as shown in the faq, it goes to the else (doesn't need to be invoked..) yet it throws the error still. Any idea? I'm completely lost. Lol, I'm thinking of just adding like 50 timer controls and give up on this class to have unlimited timers because I can't get anywhere with this mess.

This post has been edited by Ewinz87: 12 March 2010 - 06:29 PM

Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2262
  • View blog
  • Posts: 9,462
  • Joined: 29-May 08

Re: An error occurred creating the form.

Posted 12 March 2010 - 07:40 PM

From your code your half right but you seem to missed out a important point in the tutorial code.

    ' The delegate
    Delegate Sub RemoveListViewItem_Delegate(ByVal [ListView] As ListView, ByVal [ListViewItem] As ListViewItem)

    ' The delegates subroutine.
    Private Sub RemoveListViewItem_ThreadSafe(ByVal [ListView] As ListView, ByVal [ListViewItem] As ListViewItem)
        ' InvokeRequired required compares the thread ID of the calling thread to the thread ID of the creating thread.
        ' If these threads are different, it returns true.
        If [ListView].InvokeRequired Then
            Dim MyDelegate As New RemoveListViewItem_Delegate(AddressOf RemoveListViewItem_ThreadSafe)
            Me.Invoke(MyDelegate, New Object() {[ListView], [ListViewItem]})


in the else clause of the [ListView].InvokeRequired

        Else
' The code you need to do the removal as would be normally, as if it wasn't threaded '

        End If
    End Sub

Was This Post Helpful? 0
  • +
  • -

#7 Ewinz87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 25-May 09

Re: An error occurred creating the form.

Posted 13 March 2010 - 05:48 AM

Whops, made a mistake while copy pasting, actual code looks like this:

Private Sub RemoveListViewItem_ThreadSafe(ByVal [ListView] As ListView, ByVal [ListViewItem] As ListViewItem)
        ' InvokeRequired required compares the thread ID of the calling thread to the thread ID of the creating thread.
        ' If these threads are different, it returns true.
        If [ListView].InvokeRequired Then
            Dim MyDelegate As New RemoveListViewItem_Delegate(AddressOf RemoveListViewItem_ThreadSafe)
            Me.Invoke(MyDelegate, New Object() {[ListView], [ListViewItem]})
        Else
            frmMain.lvActions.Items.Item(lvItem.Index).Remove()
        End If
    End Sub


lvItem being declared early in the class before the subs.
Was This Post Helpful? 0
  • +
  • -

#8 Ewinz87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 25-May 09

Re: An error occurred creating the form.

Posted 16 March 2010 - 12:56 PM

Bump, still stuck and can't figure out how to achieve my goal for my sake. Any help would be appreciated.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1