Solved

Error Occuring while opening other form from a threaded form in vb.net

Posted on 2011-02-14
6
962 Views
Last Modified: 2012-05-11
I basically have two forms, ("form1" & "loginform")
Form1 has axwindowsmediaplayer1 (windows media player component)
Loginform consists of two textbox (username, password) and one button1 to process login.

Situation
When user press button1, a new thread is created, which logins to mysql database stored online and if login gets successful, i hide current form (me.hide) and show my main form (form1.show).

To my surprise, instead of showing form1 it is throwing exception. here is the error:
"An error occurred creating the form. See Exception.InnerException for details.  The error is: ActiveX control '6bf52a52-394a-11d3-b153-00c04f79faa6' cannot be instantiated because the current thread is not in a single-threaded apartment."

So how can i get rid of these error and successfully show my main form??? I don't know whether this error is with visual basic 2008, 2010 or some mistake from my side in improperly coding in vb.net. This project is really important for me and any help will be really very appreciated.
0
Comment
Question by:herit02
  • 2
  • 2
  • 2
6 Comments
 
LVL 9

Expert Comment

by:wellhole
ID: 34888773
Typically this error happens because you're trying to access a UI component outside of the thread it was created. What you need to do is create a delegate for form1.show. Just call ShowForm1() instead of form1.show.

Private Delegate Sub ShowForm1_Delegate()

Public Sub ShowForm1()
    If form1.InvokeRequired Then
      form1.BeginInvoke(New ShowForm1_Delegate(AddressOf ShowForm1)
    Else
      form1.Show()
    End If
End Sub
0
 
LVL 3

Author Comment

by:herit02
ID: 34888828
this is still not working, :(
same error is repeating.
0
 
LVL 9

Expert Comment

by:wellhole
ID: 34888895
I suggest you repeat what I have there across the board. I recommend running this through VS and finding out exactly which lines are causing this and applying this solution to it.

FYI, BeginInvoke is asynchronous and Invoke is synchronous. It may be safer to use Invoke instead if you don't understand very well.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 34889090
Is the LoginForm the "Startup Object" in Project --> Properties?  If not, how is it being displayed?

You stated:

    "When user press button1, a new thread is created, which logins to mysql database stored online and if login gets successful, i hide current form (me.hide) and show my main form (form1.show)."

Instead of a manual thread, use the BackgroundWorker() control to access the mysql database.  In the DoWork() handler, set "e.Result" to indicate whether login was successful.  When the RunWorkerCompleted() event fires, check the result and show Form1 from there if appropriate.

See:
http://msdn.microsoft.com/en-us/library/cc221403(VS.95).aspx

*You can leave Form1 as the "Startup Object" and then use the Application.Startup() Event to show the Login Form instead.  You'd still use the BackgroundWorker() control within the Login Form to access the database though.  Set the DialogResult() Property of the Login Form to indicate success/failure.  Back in the Application.Startup() event, you set "e.Cancel" to True to cancel the Startup Object (Form1) and cause the app to exit.  If login succeeds you simply exit the Startup() event as normal and Form1 would continue loading.
See:
http://msdn.microsoft.com/en-us/library/w3xx6ewx(VS.80).aspx

Very simple example:
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_24214878.html#23858425
0
 
LVL 3

Author Closing Comment

by:herit02
ID: 34894652
Well that worked like a charm!
One thing i didn't understand was what is brief difference between Background Worker & New Threads.

One more thing to notice is that when form1 actually runs wmplayer activex object, i also have some simultaneous threads on that form1 but that is not giving any problem but it sure does when form1 is called to open from other threaded form, Sounds Weird.

Anyway Idle_Mind, Thanks a Ton! You are seriously not idle :)
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 34896528
"what is brief difference between Background Worker & New Threads."

When using a Manual Thread you have to manually use Invoke/Delegates to update the GUI.

The BackgroundWorker() control simply wraps a Thread and does the Invoke/Delegate for you via the Events.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now