I made a simple webbrowser in c#, which keeps reloading a page and then does something when the page is loaded.
However, after the first time, the following function doesn't fire anymore:
public void gotourl()
{
webBrowser.Navigate("Stackoverflow.com");
}
public void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
MessageBox.Show("Page loaded succesfully."); // only fires once
// waiting for a few seconds by using a timer
gotourl();
}
However, I did reload the page. The documentcompleted state simply doesn't fire again.
Is there a way to let the function fire every time I navigate to an url?
( I also tried webBrowser.Refresh() )
EDIT: I added the unbelievable solution..
The solution is unbelievable. After hours of searching and trying things, I found the answer.
In the properties of webBrowser1, I set the property "AllowNavigation" to false.
If set to false, it only registers the webBrowser1.DocumentCompleted() function only ONCE, but when the AllowNavigation property is set to true(which it is by default), the DocumentCompleted() function repeats.
I have no clue why it works this way and I hope people with the same problem find this answer, as it is the only answer on the net..
Related
I have a problem with my little c# project.
I need to somehow navigate through a site, performing a few simple actions on each page. My solution to it was along the lines of this:
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var button = webBrowser1.Document.GetItemById("next_page_button");
button.InvokeMember("click");
webBrowser1.Refresh();
//here's my ugly solution which works
do {} while (webBrowser1.ReadyState!=WebBrowserReadyState.Complete);
webBrowser1.Navigate("http://www.webtest.com/page3");
webBrowser1.Refresh();
//same method of waiting for loading, causes endless loop this time
do {} while (webBrowser1.ReadyState!=WebBrowserReadyState.Complete);
var images = webBrowser1.Document.GetElementsByTagName("img");
//and then I do stuff with all them images..
So basically my program detects that the webBrowser loaded a page just fine the first time with that ugly while loop, but then, after the navigate() command it enters the second loop and never comes out of it. How come?
I've checked and double checked everything in debug mode, going through every step.
I could use your advice on organizing this program better for sure. xD
After two years i don't know it would help!!
but for others, the main thread is getting busy with your while loop so webbrowser object can not do anything, you need to implement this
webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler((object sender, WebBrowserDocumentCompletedEventArgs arg) =>
{
/// do anything you want with webbrowser document.
})
or using Application.DoEvents(), this will make your main thread loop once and the webbrowser object load it's resources like javascripts files.
This may come across as incredibly stupid, but I cannot figure out if:
I am an idiot
I have misunderstood something
The MS Web Browser control is bugged
I prefer to think that it is the latter.
I have a Web Browser control in a WinForms user control. It has been added to the control at design time, and in theory, in the Load event of the control it should navigate to Google.
Seems straightforward.
However.
public partial class TVHost : UserControl
{
public TVHost()
{
InitializeComponent();
}
private void TVHost_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("http://google.co.uk");
}
}
This doesn't work. No error, just nothing. Inserting a breakpoint/debug lines shows me that the Load event doesn't even get called.
I decided at this point to check that the Load event is being set correctly in the Designer.cs file.
this.Load += new System.EventHandler(this.TVHost_Load);
Seems legit.
If I remove the web browser control from the form, the load event fires.
I don't understand this one bit, how can a control prevent a method which uses it from firing in the first place?
Moving on, I found this:
http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvcs/thread/d6e427b2-9cc9-4318-bb05-11363025e3f7/
TL;DR for the link is as follows: "Load won't work if you have a webbrowser on the form which is set to Visible = true"
So sure as hell, if I change the default visibility of the webbrowser to false, the load event of the control fires. I can work around the problem by setting the visibility of the browser in the load event.
private void TVHost_Load(object sender, EventArgs e)
{
webBrowser1.Visible = true;
webBrowser1.Navigate("http://google.co.uk");
}
Very odd.
Whilst this "fix" works, I find it incredibly hacky and was wondering if anybody has any explaination for this behaviour?
Amazingly I have found this bug in MS Connect, left over from 2005 - http://connect.microsoft.com/VisualStudio/feedback/details/116535/when-adding-a-webbrowser-control-to-a-user-control-the-load-will-not-fire#
From the discussion in the Connect bug you linked to:
For now, if you want to get the Load event to fire, you can set the URL property of the WebBrowser control in the property grid. The URL can be anything you want, even about:blank if you don't want it to start with a page loaded.
So if you go into the designer and set the WebBrowser's Url property to the string about:blank (which tells the WebBrowser to load an empty page), then your user control should start getting its Load event again.
I've been researching this stuff and everyone seems to agree that the solution is to check the ReadyState of the Web Browser until is set to Complete.
But actually the event is sometimes fired with the ReadyState set to Complete several times.
I don't think there is a solution with that crappy .NET WebBrowser, but there might be one if I use the underlying DOM component.
Only problem is, I have no idea how do access the DOM component behind the WebBrowser that fires the DocumentCompleted event.
DocumentCompleted will fire for each frame in the web page. The hard way is to count off the frames, shows you how to access the DOM:
private int mFrameCount;
private void startNavigate(string url) {
mFrameCount = 0;
webBrowser1.Navigate(url);
}
private void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
mFrameCount += 1;
bool done = true;
if (webBrowser1.Document != null) {
HtmlWindow win = webBrowser1.Document.Window;
if (win.Frames.Count > mFrameCount && win.Frames.Count > 0) done = false;
}
if (done) {
Console.WriteLine("Now it is really done");
}
}
The easy way is to check the URL that completed loading:
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (e.Url.Equals(webBrowser1.Url)) {
Console.WriteLine("Now it is really done");
}
}
This would probably happen if the page uses Javascript or <meta refresh> to redirect to another page.
If so, there's no good workaround.
I can't find anything that will give 100% certainty.
Mentioned example (e.Url.Equals(webBrowser1.Url)) may work for a simple WebBrowser.Navigate(url), however, in my case I click nodes in code to open new frames in existing frames. Mostly the number of times "Navigating" and "DocumentCompleted" fire will be the same, but again NOT always. "isBusy = false" and "ReadyState = Complete" will always be the case when it's finished (at least so far) but it will also a few times have this state when it's still loading. Counting frames also seems useless for me, in one case DocumentCompleted is fired 23 times, however, all frames and sub(-sub-sub and so on) frames are 14 in total.
The only thing that seems to work is wait a short period (1 or 2 seconds?) to see if anything happens (any events fired, any state changes).
Hmm, I found another solution for me. Often we're not interested in the whole page being loaded, often we want certain elements to exists. So after each DocumentCompleted and when "isBusy = false" and "ReadyState = Complete" we can search the DOM if this element exists.
In my experience it's impossible to tell when a web page has finished loading until DocumentCompleted hasn't fired for a while. So I refresh a timer for around 1000ms every time the DocumentCompleted event triggers. Then when the timer times out I process the web page.
I'm experimenting with some AJAX now. I have a custom control which appears on my masterpage in which there is an update panel and a timer. The timer fires and the panel updates and everything is dandy. Except that there are some operations that I don't want it to perform on every refresh. It seems like the entire page lifecycle happens with each refresh. There are variables I want to set, and keep their value on the refresh. Is there a way to make it perform ONLY what's in the timer_tick call?
You could take a look at Request["__EVENTTARGET"] in the page load event to see what control caused the postback. If it's the timer control, jump out of the function.
Assuming your timer is called "refreshtimer":
protected void Page_Load(object sender, EventArgs e)
{
if (Request["__EVENTTARGET"] == "refreshtimer")
{
return;
}
// etc
Not sure what what an AJAX.Net post back looks like to, But I usually protect my other controls and content by checking for post back;
protected void Page_Load(object sender, EventArgs e)
{
// if its a post back then my controls should already be setup...
if (!Page.IsPostBack)
{
InitControlData();
}
}
and then it should fall thru to your event handling?
protected void timer_tick(object sender, EventArgs e)
{
// Do my Ajaxy work~
}
UpdatePanels always force the entire page to refresh. If you want only a certain portion of the page to be processed (and it's a fixed size) then you could try using an iframe.
Alternatively, if you want to save the variables you can either put them in ViewState or SessionState so that they are persisted between postbacks
This is not possible with UpdatePanels... the entire page will go through the entire lifecycle. As others mentioned, you can limit the processing that happens by using IsPostBack or ScriptManager's IsInAsyncPostBack, but ultimately this is not going to be a great solution for complex pages.
However, you can use Page Methods to execute just one static method in your page, but you'll have to make the Javascript call yourself and update the UI. Here are some examples:
http://www.singingeels.com/Articles/Using_Page_Methods_in_ASPNET_AJAX.aspx
http://encosia.com/2009/07/21/simplify-calling-asp-net-ajax-services-from-jquery/
http://weblogs.asp.net/craigshoemaker/archive/2008/09/29/using-jquery-to-call-asp-net-ajax-page-methods.aspx
I have a datetimepicker in C#. When I click on it, it expands to show a monthly calendar, when I click the left arrow to go back a month, it changes the value and calls my event. The event includes too much code to include here but it calls several functions needless to say.
The problem I'm having is that when I click that left arrow it gets stuck in some sort of loop and keeps descending through the months and I can't stop it. One of the functions that is being called contains a Application.DoEvents() and if I comment that out it doesn't get stuck in the loop, but I need that command to update another section of the interface. Any idea why this is happening?
I can duplicate it sometimes with this code, sometimes it just does it a couple times, sometimes it gets stuck in the loop.
private void DateTimePickerValueChangedEvent(object sender, EventArgs e)
{
afunction();
}
private void afunction()
{
listView1.Clear();
panel1.Visible = true;
Application.DoEvents();
}
I also have the same problem. In my case, instead of calling DoEvents I'm updating a Crystal Report view. The only workaround I found is to update my view upon the CloseUp event instead of ValueChanged or TextChanged.
Scott, how did you finally corrected your problem ?
The DateTimePicker ValueChanged event is buggy. Per Microsoft Windows Forms Team on this page https://connect.microsoft.com/VisualStudio/feedback/details/1290685/debugging-datetimepicker-event-hangs-vs:
"The DateTimePicker control installs a mouse hook as part of its functionality, but when the debugger has the WinForms application stopped on a breakpoint, it allows the possibility of a deadlock if VS happens to get a mouse message. For now, the deadlock is unfortunately a consequence of the DateTimePicker's design. The mouse hook is installed when the drop down is clicked to display the calendar. This means that breakpoints should not be sent in any event handlers which would be called while the calendar is active. We are currently investigating whether it is possible to address this issue and we will update this thread with further information if we are able to make a fix available."
Without seeing any of the code, try these steps:
Comment out the entire event handler
to see how fast it runs with nothing
attached to it.
Uncomment lines one at a time to see
which ones are causing the most
problems.
Analyze those method calls.
...
Profit!
You could try a couple of things. Get rid of the DoEvents inside of the ChangedEvent.
Call the doevents inside of a seperate function after maybe a period of time (thread.sleep() ?).
I know doevents does cause issues but I rarely use it.
event procedure ValueChanged :
set parameter in sender.tag
enableTimer and execute parameter using sender.tag
example:
private void DateTimePicker_ValueChanged(object sender, EventArgs e)
{
DateTimePicker ThisSender = (DateTimePicker)sender;
Timer.Tag = ThisSender.Name.ToString() + "=" + ThisSender.Value;
Timer.Enabled = true;
}