Multithreaded Forms

Open existing form on new thread

Page 1 of 1

14 Replies - 27304 Views - Last Post: 02 July 2009 - 04:30 PM Rate Topic: -----

#1 mishipal   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 04-May 09

Multithreaded Forms

Posted 22 June 2009 - 02:38 AM

I wanna open an existing form on a new thread so that one form does not wait for others, as it has to do a lot of work.

Dim thread As Threading.Thread
			thread = New Threading.Thread(AddressOf Me.showform)
			thread.Start()


Form2.ShowDialog()



i get an activex error

System.InvalidOperationException was unhandled
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="Logitex"
StackTrace:
at Logitex.My.MyProject.MyForms.Create__Instance__[T](T Instance) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 190 at Logitex.My.MyProject.MyForms.get_Home() at Logitex.Login.showform() in D:\Visual Studio 2005 Projects\Logitex\Logitex\Login.vb:line 81 at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.runTryCode(Object userData) at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()
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 Logitex.Home.InitializeComponent() in D:\Visual Studio 2005 Projects\Logitex\Logitex\Home.Designer.vb:line 140 at Logitex.Home..ctor() in D:\Visual Studio 2005 Projects\Logitex\Logitex\Home.vb:line 49
InnerException:

Is This A Good Question/Topic? 0
  • +

Replies To: Multithreaded Forms

#2 woodjom   User is offline

  • D.I.C Addict
  • member icon

Reputation: 34
  • View blog
  • Posts: 566
  • Joined: 08-May 08

Re: Multithreaded Forms

Posted 22 June 2009 - 08:35 AM

View Postmishipal, on 22 Jun, 2009 - 03:38 AM, said:

I wanna open an existing form on a new thread so that one form does not wait for others, as it has to do a lot of work.

Dim thread As Threading.Thread
			thread = New Threading.Thread(AddressOf Me.showform)
			thread.Start()


[b]Form2.ShowDialog()[/b]



i get an activex error


ok, i assume that Show Dialog is a login dialog?

Also, what else are you running on this Form2? ActiveX errors are either Webbased, which doesnt look like to me, or a mis-signed ActiveX control in a client/server application.

View Postmishipal, on 22 Jun, 2009 - 03:38 AM, said:

System.InvalidOperationException was unhandled, 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="Logitex"
StackTrace:
at Logitex.My.MyProject.MyForms.Create__Instance__[T](T Instance) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 190


Lastly, looks like you havent managed the threads that are being created. Remember when you are multithreading that you need to create a callee and caller relationship. With this being said you need to handle the transmission of information through the threads from the server to the client-station and some handshaking, probably with a security key. As well, when this packet is received you need to have it assoicated with the server or it goes no where. Lastly, you need to create a handshake signature from the client-station to the server with basically same criteria.

View Postmishipal, on 22 Jun, 2009 - 03:38 AM, said:

at Logitex.My.MyProject.MyForms.get_Home() at Logitex.Login.showform() in D:\Visual Studio 2005 Projects\Logitex\Logitex\Login.vb:line 81


As far as this error line, you may want to look at line 81 and see what is going on with that line and again, if this is a client/server application, then you may need to step back in the associated objects on that line. VB and C++/C# are notorious for not showing an error until it is way too late and therefore the problem is not on the line it states but instead the setup of the object used on that line.

This post has been edited by woodjom: 22 June 2009 - 08:36 AM

Was This Post Helpful? 0
  • +
  • -

#3 Ändrew   User is offline

  • D.I.C Regular
  • member icon

Reputation: 25
  • View blog
  • Posts: 312
  • Joined: 21-April 08

Re: Multithreaded Forms

Posted 22 June 2009 - 10:35 PM

Try this line of code.
It invokes the thread to do what it say's im told.

Invoke((Action)(() => Form2.ShowDialog()))



Ändrew
Was This Post Helpful? 0
  • +
  • -

#4 woodjom   User is offline

  • D.I.C Addict
  • member icon

Reputation: 34
  • View blog
  • Posts: 566
  • Joined: 08-May 08

Re: Multithreaded Forms

Posted 23 June 2009 - 08:06 PM

what .net version are you using?

Here is the threading namespace you can look at Microsoft MSDN Link

The Thread call: MSDN Thread class

Thread Constructors<== Start here cause your New class form is not consistent with .Net 3.5


Direct copy from the Thread Class page above:
Imports System
Imports System.Threading

' Simple threading scenario:  Start a Shared method running
' on a second thread.
Public Class ThreadExample
    ' The ThreadProc method is called when the thread starts.
    ' It loops ten times, writing to the console and yielding 
    ' the rest of its time slice each time, and then ends.
    Public Shared Sub ThreadProc()
        Dim i As Integer
        For i = 0 To 9
            Console.WriteLine("ThreadProc: {0}", i)
            ' Yield the rest of the time slice.
            Thread.Sleep(0)
        Next
    End Sub

    Public Shared Sub Main()
        Console.WriteLine("Main thread: Start a second thread.")
        ' The constructor for the Thread class requires a ThreadStart 
        ' delegate.  The Visual Basic AddressOf operator creates this
        ' delegate for you.
        Dim t As New Thread(AddressOf ThreadProc)

        ' Start ThreadProc.  Note that on a uniprocessor, the new 
        ' thread does not get any processor time until the main thread 
        ' is preempted or yields.  Uncomment the Thread.Sleep that 
        ' follows t.Start() to see the difference.
        t.Start()
        'Thread.Sleep(0)

        Dim i As Integer
        For i = 1 To 4
            Console.WriteLine("Main thread: Do some work.")
            Thread.Sleep(0)
        Next

        Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.")
        t.Join()
        Console.WriteLine("Main thread: ThreadProc.Join has returned.  Press Enter to end program.")
        Console.ReadLine()
    End Sub
End Class



And your problem maybe that the showform event may not be threadable....so put what ever it is you need to get work done into a thread and leave the forms out of the thread.

Found this as a good example: Example Threading

This post has been edited by woodjom: 23 June 2009 - 08:24 PM

Was This Post Helpful? 0
  • +
  • -

#5 woodjom   User is offline

  • D.I.C Addict
  • member icon

Reputation: 34
  • View blog
  • Posts: 566
  • Joined: 08-May 08

Re: Multithreaded Forms

Posted 24 June 2009 - 06:38 AM

View PostÄndrew, on 22 Jun, 2009 - 11:35 PM, said:

Invoke((Action)(() => Form2.ShowDialog()))



problem with this is that this is C++/C# code. VB doesnt do direct casting like that. He would have to do

Invoke(directcast(form2.showdialog,action))



but not sure that that will work for his instance....as he is trying to thread an interface, where as most people thread processes.

Have you tried opening a seperate thread just for the process you are trying to complete? Instead of putting the interface and its code in a thread, leave them out and put the process you are wanting to get done into a thread.

Its not a good idea to put interfaces into threads as they consume alot of resources just by themselves and then to add them to a thread would just compound the resource consumption altogether.
Was This Post Helpful? 0
  • +
  • -

#6 T3hC13h   User is offline

  • D.I.C Regular

Reputation: 66
  • View blog
  • Posts: 337
  • Joined: 05-February 08

Re: Multithreaded Forms

Posted 24 June 2009 - 09:52 AM

I don't understand why your even trying to call me.Show on a new thread. If you can start the thread then you can just call the method directly.
Was This Post Helpful? 0
  • +
  • -

#7 woodjom   User is offline

  • D.I.C Addict
  • member icon

Reputation: 34
  • View blog
  • Posts: 566
  • Joined: 08-May 08

Re: Multithreaded Forms

Posted 24 June 2009 - 04:07 PM

hes trying to seperate a continuous process form the interface thread, altogether. I understand the reason but i think i was looking at the forest instead of the tree.

Since the application runs on its own thread naturally, i believe he needs to move away from trying thread the interface side of the application and consolidate his threading efforts in on the process he wants to run seperately.
Was This Post Helpful? 0
  • +
  • -

#8 woodjom   User is offline

  • D.I.C Addict
  • member icon

Reputation: 34
  • View blog
  • Posts: 566
  • Joined: 08-May 08

Re: Multithreaded Forms

Posted 24 June 2009 - 04:13 PM

View Postmishipal, on 22 Jun, 2009 - 03:38 AM, said:

I wanna open an existing form on a new thread so that one form does not wait for others, as it has to do a lot of work.

Dim thread As Threading.Thread
			thread = New Threading.Thread(AddressOf Me.showform)
			thread.Start()


Form2.ShowDialog()



Getting back to the original question, now that i have thought about it. just open up the first form, and right before you start the process of what ever your doing with it, open the second form and then shove the process into its own seperate thread. Pending on the complexity of the thread you may want to reduce its priority, otherwise you may find it crunching your computer speed and defeating what you are trying to do. Also, if you are trying to do 1M iterations on one computer you may want to look at get another computer to do the process. Windows can confortably handle 100k iteration processes at about 5 of them. Once you get to 1M iterations per process, you start noticing memory leaks, processor speed slowing, and your desktop starts taking a hit.

I would suggest anything over about 200k iterations per process (max it at around 5 process) you need to cut them up between computers. If each of the computers is just a crunch machine then you can increase the process count to around 20 to 30 process per computer. Just let it be known of the warning above that you may notice performance issues when you start multi-threading mass iterations processes on a actively used computer.
Was This Post Helpful? 0
  • +
  • -

#9 mishipal   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 04-May 09

Re: Multithreaded Forms

Posted 30 June 2009 - 10:37 AM

I am really very sorry... but went on vacations...

Yes woodjom is right i want to open a new form so that when first form is calculating things user can still work on second form. The code can open a new form but not an existing form. The problem is with opening new interface on other thread.

Thanking You in anticipation
Was This Post Helpful? 0
  • +
  • -

#10 T3hC13h   User is offline

  • D.I.C Regular

Reputation: 66
  • View blog
  • Posts: 337
  • Joined: 05-February 08

Re: Multithreaded Forms

Posted 30 June 2009 - 05:07 PM

Call Application.Run(Frm) from the new thread. MageUK posted that tip in another thread on the same topic.
Was This Post Helpful? 0
  • +
  • -

#11 mishipal   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 04-May 09

Re: Multithreaded Forms

Posted 30 June 2009 - 08:43 PM

i changed that to

Quote

application.run(logitex.home)
but it still gives following error

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.

Please guide me
Was This Post Helpful? 0
  • +
  • -

#12 mishipal   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 04-May 09

Re: Multithreaded Forms

Posted 30 June 2009 - 09:58 PM

Ok Guys.... i figured it out....thanx to DIC and Microsoft...

This is the final code....
thread = New Threading.Thread(AddressOf Me.showform)
	  thread.SetApartmentState(Threading.ApartmentState.STA) '''Single Thread Apartment
	  thread.Start()



<STAThread()> _   
   Sub showform()
	  Application.Run(New Form2())
   End Sub


Was This Post Helpful? 0
  • +
  • -

#13 mishipal   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 04-May 09

Re: Multithreaded Forms

Posted 01 July 2009 - 02:00 AM

OMG!!!
I am back with a new problem...

I managed to start existing form on new thread and its working....
now i want to change

Form2.label1.text = form1.label1.text

Its giving cross thread exception
Was This Post Helpful? 0
  • +
  • -

#14 woodjom   User is offline

  • D.I.C Addict
  • member icon

Reputation: 34
  • View blog
  • Posts: 566
  • Joined: 08-May 08

Re: Multithreaded Forms

Posted 02 July 2009 - 04:26 PM

You may want to store the information in a global variable as a handling variable.

Pass the form1 information to the variable and then pull that information from the variable and into form2. Im not exactly positive on this but in theory it should work. This aspect of threading is where my knowledge is limited and im hitting it slowly but surely.

If i had to guess you need to probably use hashing of the two forms to reference their memory/thread location and then pass accordingly.
Was This Post Helpful? 0
  • +
  • -

#15 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Multithreaded Forms

Posted 02 July 2009 - 04:30 PM

read the tutorial: Background Worker for the reason and answer.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1