?
Solved

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

Posted on 2011-02-14
6
Medium Priority
?
996 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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 86

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

765 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