VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 300,362 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,490 people online right now. Registration is fast and FREE... Join Now!




Multithreaded Forms

 

Multithreaded Forms, Open existing form on new thread

mishipal

22 Jun, 2009 - 01:38 AM
Post #1

D.I.C Head
**

Joined: 4 May, 2009
Posts: 77



Thanked: 1 times
My Contributions
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.

CODE

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

CODE

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:


User is offlineProfile CardPM
+Quote Post


woodjom

RE: Multithreaded Forms

22 Jun, 2009 - 07:35 AM
Post #2

D.I.C Regular
Group Icon

Joined: 8 May, 2008
Posts: 365



Thanked: 15 times
My Contributions
QUOTE(mishipal @ 22 Jun, 2009 - 03: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.

CODE

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

CODE

[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.
QUOTE(mishipal @ 22 Jun, 2009 - 03:38 AM) *


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.

QUOTE(mishipal @ 22 Jun, 2009 - 03:38 AM) *

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 Jun, 2009 - 07:36 AM
User is offlineProfile CardPM
+Quote Post

Ändrew

RE: Multithreaded Forms

22 Jun, 2009 - 09:35 PM
Post #3

D.I.C Head
Group Icon

Joined: 21 Apr, 2008
Posts: 232



Thanked: 10 times
Dream Kudos: 75
My Contributions
Try this line of code.
It invokes the thread to do what it say's im told.

CODE

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


Ändrew
User is offlineProfile CardPM
+Quote Post

woodjom

RE: Multithreaded Forms

23 Jun, 2009 - 07:06 PM
Post #4

D.I.C Regular
Group Icon

Joined: 8 May, 2008
Posts: 365



Thanked: 15 times
My Contributions
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:
vb.net 3.5

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 Jun, 2009 - 07:24 PM
User is offlineProfile CardPM
+Quote Post

woodjom

RE: Multithreaded Forms

24 Jun, 2009 - 05:38 AM
Post #5

D.I.C Regular
Group Icon

Joined: 8 May, 2008
Posts: 365



Thanked: 15 times
My Contributions
QUOTE(Ändrew @ 22 Jun, 2009 - 11:35 PM) *

CODE

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

vb

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.
User is offlineProfile CardPM
+Quote Post

T3hC13h

RE: Multithreaded Forms

24 Jun, 2009 - 08:52 AM
Post #6

D.I.C Head
**

Joined: 5 Feb, 2008
Posts: 153



Thanked: 25 times
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

woodjom

RE: Multithreaded Forms

24 Jun, 2009 - 03:07 PM
Post #7

D.I.C Regular
Group Icon

Joined: 8 May, 2008
Posts: 365



Thanked: 15 times
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

woodjom

RE: Multithreaded Forms

24 Jun, 2009 - 03:13 PM
Post #8

D.I.C Regular
Group Icon

Joined: 8 May, 2008
Posts: 365



Thanked: 15 times
My Contributions
QUOTE(mishipal @ 22 Jun, 2009 - 03: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.

CODE

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

CODE

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.
User is offlineProfile CardPM
+Quote Post

mishipal

RE: Multithreaded Forms

30 Jun, 2009 - 09:37 AM
Post #9

D.I.C Head
**

Joined: 4 May, 2009
Posts: 77



Thanked: 1 times
My Contributions
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
User is offlineProfile CardPM
+Quote Post

T3hC13h

RE: Multithreaded Forms

30 Jun, 2009 - 04:07 PM
Post #10

D.I.C Head
**

Joined: 5 Feb, 2008
Posts: 153



Thanked: 25 times
My Contributions
Call Application.Run(Frm) from the new thread. MageUK posted that tip in another thread on the same topic.
User is offlineProfile CardPM
+Quote Post

mishipal

RE: Multithreaded Forms

30 Jun, 2009 - 07:43 PM
Post #11

D.I.C Head
**

Joined: 4 May, 2009
Posts: 77



Thanked: 1 times
My Contributions
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
User is offlineProfile CardPM
+Quote Post

mishipal

RE: Multithreaded Forms

30 Jun, 2009 - 08:58 PM
Post #12

D.I.C Head
**

Joined: 4 May, 2009
Posts: 77



Thanked: 1 times
My Contributions
Ok Guys.... i figured it out....thanx to DIC and Microsoft...

This is the final code....
CODE

thread = New Threading.Thread(AddressOf Me.showform)
      thread.SetApartmentState(Threading.ApartmentState.STA) '''Single Thread Apartment
      thread.Start()


CODE

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


User is offlineProfile CardPM
+Quote Post

mishipal

RE: Multithreaded Forms

1 Jul, 2009 - 01:00 AM
Post #13

D.I.C Head
**

Joined: 4 May, 2009
Posts: 77



Thanked: 1 times
My Contributions
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
User is offlineProfile CardPM
+Quote Post

woodjom

RE: Multithreaded Forms

2 Jul, 2009 - 03:26 PM
Post #14

D.I.C Regular
Group Icon

Joined: 8 May, 2008
Posts: 365



Thanked: 15 times
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

AdamSpeight2008

RE: Multithreaded Forms

2 Jul, 2009 - 03:30 PM
Post #15

The Bandido Coder
Group Icon

Joined: 29 May, 2008
Posts: 2,685



Thanked: 155 times
Dream Kudos: 3925
Expert In: vb.net, LINQ

My Contributions
read the tutorial: Background Worker for the reason and answer.

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 08:12PM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month