Solved

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

Posted on 2011-02-14
6
972 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

778 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