Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, November 27, 2008 5:28 PM
Hi,
I've been able to create a simple web app with a form and WebBrowser component but now I want to do the same from a console app or service. Is this possible?
I've created a console app with a form, added a WebBrowser, but all I get is a blank WebBrowser with an egg timer cursor. I'm aware that in a windows app it calls it using Application.Run(new MyBrowserForm) so I'm guessing there's some kind of context I'm missing. Being new to this area I don't know what to do.
And yes, it does have to be a console app. It won't be just visual components it will be launching.
Thanks.
All replies (4)
Thursday, November 27, 2008 6:53 PM âś…Answered
You were right on to mention Application.Run. Windows won't dispatch events to a modeless window without this, thus your entire user interface will be frozen.
Solution #1:
Replace Console.ReadLine with the following:
Application.Run(form); |
Solution #2:
Show the form modally.
form.ShowDialog(); |
Solution #3:
Show the form on a different thread, call Application.Run using that thread.
[STAThread] |
static void Main(string[] args) |
{ |
System.Threading.Thread t = new System.Threading.Thread(ThreadStart); |
t.SetApartmentState(System.Threading.ApartmentState.STA); |
t.Start(); |
Console.ReadLine(); |
} |
private static void ThreadStart() |
{ |
WebBrowser browser = new WebBrowser(); |
browser.Dock = DockStyle.Fill; |
browser.Name = "webBrowser"; |
browser.ScrollBarsEnabled = false; |
browser.TabIndex = 0; |
browser.Url = new Uri("http://www.microsoft.com"); |
Form form = new Form(); |
form.WindowState = FormWindowState.Maximized; |
form.Controls.Add(browser); |
form.Name = "Browser"; |
Application.Run(form); |
} |
The last solution allows the main thread to keep running (so that it runs Console.ReadLine while the WebBrowser is running in the other thread). I have listed the three solutions in what would be my order of preference.
Regardless of what solution you choose, be sure to set the Url of the WebBrowser or you are not going to see anything interesting.
Thursday, November 27, 2008 6:13 PM
Well, a console app can load a form as long as it has [STAThread] on the Main() method. However, you shouldn't rely on this from a service, since a service has no desktop (especially in Vista and beyond).
Would WebClient suffice? This lets you prepare requests and get responses...
Marc [C# MVP]
Thursday, November 27, 2008 6:39 PM
Hi Marc,
In my console app, my main does indeed have a [STAThread]. My problem is that the WebBrowser is not displaying the web page, I'm guessing from the egg timer it is just hanging waiting for something.
Do I need to kick it off in a different thread?
Is there something I need to do to initialise it?
Any help, no matter how trivial, would be helpful to this novice Forms programmer.
Here's what I've tried...
1 | class Program |
2 | { |
3 | [STAThread] |
4 | static void Main(string[] args) |
5 | { |
6 | WebBrowser browser = new WebBrowser(); |
7 | browser.Dock = DockStyle.Fill; |
8 | browser.Name = "webBrowser"; |
9 | browser.ScrollBarsEnabled = false; |
10 | browser.TabIndex = 0; |
11 | |
12 | Form form = new Form(); |
13 | form.WindowState = FormWindowState.Maximized; |
14 | form.Controls.Add(browser); |
15 | form.Name = "Browser"; |
16 | |
17 | form.Show(); |
18 | |
19 | Console.ReadLine(); |
20 | } |
21 | } |
Thursday, November 27, 2008 8:40 PM
Cheers BC.