.Net WebBrowser control wont instanciate on form in new thread?
-
Hi, In my application i need to open 2 forms. After the first form is loaded, iti loads a second form with the following..
System.Threading.Thread MainFormThread = new System.Threading.Thread(new System.Threading.ThreadStart(OpenMainForm));
MainFormThread.Start();
System.Threading.Thread.Sleep(1500);Where OpenMainForm() just has Application.Run(new MainForm()); The MainForm has a webbrowser control, and it will not compile due to the error:
ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.
Does anyone know how i can solve this issue? Thanks for reading. Andy
-
Hi, In my application i need to open 2 forms. After the first form is loaded, iti loads a second form with the following..
System.Threading.Thread MainFormThread = new System.Threading.Thread(new System.Threading.ThreadStart(OpenMainForm));
MainFormThread.Start();
System.Threading.Thread.Sleep(1500);Where OpenMainForm() just has Application.Run(new MainForm()); The MainForm has a webbrowser control, and it will not compile due to the error:
ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.
Does anyone know how i can solve this issue? Thanks for reading. Andy
The Web Browser control is designed to run in a Single threaded fashion. You can not use multi threading when using Web Broswer Control. :( BTW, why do you need a seperate thread for this? You can use a timer instead.
Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
-
Hi, In my application i need to open 2 forms. After the first form is loaded, iti loads a second form with the following..
System.Threading.Thread MainFormThread = new System.Threading.Thread(new System.Threading.ThreadStart(OpenMainForm));
MainFormThread.Start();
System.Threading.Thread.Sleep(1500);Where OpenMainForm() just has Application.Run(new MainForm()); The MainForm has a webbrowser control, and it will not compile due to the error:
ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.
Does anyone know how i can solve this issue? Thanks for reading. Andy
Hi, your situation is not clear to me. if you have two Application.Run() statements, I think you are on the wrong track. If the first form is just a splash screen, I know you are doing it wrong. A splash screen does not need an Application.Run, just create one and Show() it. Then do Application.Run(new Form2()); And make sure your static main() is decorated with a [STAThread] :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
Hi, your situation is not clear to me. if you have two Application.Run() statements, I think you are on the wrong track. If the first form is just a splash screen, I know you are doing it wrong. A splash screen does not need an Application.Run, just create one and Show() it. Then do Application.Run(new Form2()); And make sure your static main() is decorated with a [STAThread] :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
My apologies for not explaining my situation clearly. It is a small client application, the first form is a login form which shows the user a dropdown list which they select an account name from. Then I wish to pass this account name to the main form. It has been awhile since i have done any work with multi-threading but you cant just use form.Show() can you? I just tried and the form shows and then closes straight away. As far as I was aware if you are using multiple forms, you needed to use Application.Run()? Please correct me if I am wrong.
-
My apologies for not explaining my situation clearly. It is a small client application, the first form is a login form which shows the user a dropdown list which they select an account name from. Then I wish to pass this account name to the main form. It has been awhile since i have done any work with multi-threading but you cant just use form.Show() can you? I just tried and the form shows and then closes straight away. As far as I was aware if you are using multiple forms, you needed to use Application.Run()? Please correct me if I am wrong.
Abydosgater wrote:
if you are using multiple forms, you needed to use Application.Run()?
wrong. A Windows app that has modeless forms needs a single Application.Run, so you get one message pump.
Abydosgater wrote:
use form.Show()
right. form.Show() will show the form until the user closes it, the app closes it or the app exits. it is wise to keep the form's reference alive though. Remember: A single message pump can serve multiple modeless forms (i.e. those shown by calling Show).
Abydosgater wrote:
multi-threading
haven't seen your reasons for even considering multi-threading.(there is no need AFAIK). :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
Abydosgater wrote:
if you are using multiple forms, you needed to use Application.Run()?
wrong. A Windows app that has modeless forms needs a single Application.Run, so you get one message pump.
Abydosgater wrote:
use form.Show()
right. form.Show() will show the form until the user closes it, the app closes it or the app exits. it is wise to keep the form's reference alive though. Remember: A single message pump can serve multiple modeless forms (i.e. those shown by calling Show).
Abydosgater wrote:
multi-threading
haven't seen your reasons for even considering multi-threading.(there is no need AFAIK). :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
Ah I see. Well when i use form.Show() it shows the form and closes it straight away. It doesnt give the user a chance to select an account. I have tried form.ShowDialog() and that seems to keep the form active.
LoginForm loginForm = new LoginForm(); loginForm.ShowDialog();
I then added OpenMainForm(string accountname) to the Program class:public static void OpenMainForm(string accountname) { Application.Run(new MainForm(accountname)); }
So when the program launches it shows the login form, and then when the user selects an account it calls the static OpenMainForm in the Program Class which uses Application.Run(), but thats gives errors.Starting a second message loop on a single thread is not a valid operation. Use Form.ShowDialog instead.
Considering the invalid methods I have been using, would it be more logical for me to launch the main form with Application.Run(), have it hidden and show the login form as a dialog from within the main form? Andy -
Ah I see. Well when i use form.Show() it shows the form and closes it straight away. It doesnt give the user a chance to select an account. I have tried form.ShowDialog() and that seems to keep the form active.
LoginForm loginForm = new LoginForm(); loginForm.ShowDialog();
I then added OpenMainForm(string accountname) to the Program class:public static void OpenMainForm(string accountname) { Application.Run(new MainForm(accountname)); }
So when the program launches it shows the login form, and then when the user selects an account it calls the static OpenMainForm in the Program Class which uses Application.Run(), but thats gives errors.Starting a second message loop on a single thread is not a valid operation. Use Form.ShowDialog instead.
Considering the invalid methods I have been using, would it be more logical for me to launch the main form with Application.Run(), have it hidden and show the login form as a dialog from within the main form? AndyHi, I suggest you study this example:
public class Form2 : Form {
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
new Form2("a").Show();
new Form2("b").Show();
Application.Run(new Form2("c"));
}
public Form2(string title) {
Text=title;
}
}Find out the difference between those forms. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
Hi, I suggest you study this example:
public class Form2 : Form {
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
new Form2("a").Show();
new Form2("b").Show();
Application.Run(new Form2("c"));
}
public Form2(string title) {
Text=title;
}
}Find out the difference between those forms. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
Ah thank you, I see where my mistake is. form.Show() only works if you have Application.Run on another form. ie:
new Form2("b").Show(); Application.Run(new Form2("c"));
Thank you very much =] -
Ah thank you, I see where my mistake is. form.Show() only works if you have Application.Run on another form. ie:
new Form2("b").Show(); Application.Run(new Form2("c"));
Thank you very much =]Abydosgater wrote:
form.Show() only works if you have Application.Run on another form.
Not quite.
new Form2("b").Show();
new Form2("c").Show();
Application.Run();also works, but behaves slightly differently. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.