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
Solved

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

Posted on 2011-02-14
6
976 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
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.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

829 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