[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1004
  • Last Modified:

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

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
herit02
Asked:
herit02
  • 2
  • 2
  • 2
1 Solution
 
wellholeCommented:
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
 
herit02Author Commented:
this is still not working, :(
same error is repeating.
0
 
wellholeCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
herit02Author Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now