c# - WebBrowser control won't navigate to URL when the call is via a timer -
i'm trying make app in c# integrated system.windows.controls.webbrowser. need implement functionality make periodic checks user activity , if user not active timespan, redirect browser control home page.
below code snippet app. using system.timers.timer class periodically call checkuseractivity
method, computes timespan , if timespan larger threshold, should redirect browser calling gohome
method.
unfortunately, won't work , behaviour seems rather strange me. added logging see happens (see below). "should go away soon" message shows ok, after browser doesn't naviagte home page , second message after call navigate
doesn't show up.
i absolutely no errors. works expected except call webbrowser's navigate
method , call after vanished somehow.
i suspect problem might have threads, have little experience c# have absolutely no idea problem might here.
i tried rewrite code using system.threading.tasks.task
library in same way mentioned @ http://stackoverflow.com/questions/8267298/how-to-start-a-long-running-process-in-a-separate-thread
(sry link inside code block - not enough reputation :d ). put checking code inside infinite loop runs long running task , thread sleeps 2 seconds, ran same problem.
thanks advice. here code snippet (it should work standalone project):
xaml:
<window x:class="timertest.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" height="800" width="800"> <grid> <webbrowser x:name="webbrowser" loadcompleted="webbrowser_loadcompleted" ></webbrowser> </grid> </window>
c#:
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using system.timers; using system.windows; using system.windows.controls; using system.windows.data; using system.windows.documents; using system.windows.input; using system.windows.media; using system.windows.media.imaging; using system.windows.navigation; using system.windows.shapes; namespace timertest { /// <summary> /// interaction logic mainwindow.xaml /// </summary> public partial class mainwindow : window { private uri uri_home; private timer timer; private uri uri_last; private datetime lastused; public mainwindow() { uri_home = new uri("http://example.com/", urikind.relativeorabsolute); uri_last = uri_home; initializecomponent(); webbrowser.navigate(uri_home); // create timer 2 second interval. timer = new timer(2000); // hook elapsed event timer. timer.elapsed += checkuseractivity; timer.autoreset = true; timer.enabled = false; lastused = datetime.now; } private void gohome() { //system.windows.forms.methodinvoker update = delegate() //{ webbrowser.navigate(uri_home); //}; //webbrowser.invoke(update); } private void logmessage(string msg) { datetime = datetime.now; console.writeline(now.tostring() + ": " + msg); } private void checkuseractivity(object source, elapsedeventargs e) { logmessage("entered checkuseractivity handler"); datetime = datetime.now; timespan timespan = - lastused; logmessage("timespan " + timespan.seconds.tostring()); if (timespan.seconds > 5 && uri_home != uri_last) { timer.enabled = false; logmessage("timer disabled."); logmessage("will go away soon."); gohome(); logmessage("should have gone home"); } } private void webbrowser_loadcompleted(object sender, navigationeventargs e) { webbrowser browser = (webbrowser)sender; uri uri_navigated = e.uri; uri_last = uri_navigated; logmessage("webbrowser loadcompleted event triggered. loadcompleted uri (saved uri_last): " + uri_navigated.absoluteuri); logmessage("timer enabled status: " + timer.enabled.tostring()); lastused = datetime.now; timer.enabled = true; logmessage("timer enabled (end of loadcompleted handler)."); } } }
here output of 1 session:
11.9.2014 8:10:01: webbrowser loadcompleted event triggered. loadcompleted uri (saved uri_last): http://example.com/ 11.9.2014 8:10:01: timer enabled status: false 11.9.2014 8:10:01: timer enabled (end of loadcompleted handler). 11.9.2014 8:10:03: entered checkuseractivity handler 11.9.2014 8:10:03: timespan 2 11.9.2014 8:10:05: entered checkuseractivity handler 11.9.2014 8:10:05: timespan 4 11.9.2014 8:10:07: entered checkuseractivity handler 11.9.2014 8:10:07: timespan 6 11.9.2014 8:10:09: entered checkuseractivity handler 11.9.2014 8:10:09: timespan 8 11.9.2014 8:10:11: entered checkuseractivity handler 11.9.2014 8:10:11: timespan 10 11.9.2014 8:10:13: entered checkuseractivity handler 11.9.2014 8:10:13: timespan 12 11.9.2014 8:10:15: entered checkuseractivity handler 11.9.2014 8:10:15: timespan 14 11.9.2014 8:10:17: entered checkuseractivity handler 11.9.2014 8:10:17: timespan 16 11.9.2014 8:10:19: entered checkuseractivity handler 11.9.2014 8:10:19: timespan 18 11.9.2014 8:10:21: entered checkuseractivity handler 11.9.2014 8:10:21: timespan 20 11.9.2014 8:10:23: entered checkuseractivity handler 11.9.2014 8:10:23: timespan 22 11.9.2014 8:10:25: entered checkuseractivity handler 11.9.2014 8:10:25: timespan 24 11.9.2014 8:10:27: entered checkuseractivity handler 11.9.2014 8:10:27: timespan 26 11.9.2014 8:10:29: entered checkuseractivity handler 11.9.2014 8:10:29: timespan 28 11.9.2014 8:10:30: webbrowser loadcompleted event triggered. loadcompleted uri (saved uri_last): http://www.iana.org/domains/reserved 11.9.2014 8:10:30: timer enabled status: true 11.9.2014 8:10:30: timer enabled (end of loadcompleted handler). 11.9.2014 8:10:31: entered checkuseractivity handler 11.9.2014 8:10:31: timespan 1 11.9.2014 8:10:33: entered checkuseractivity handler 11.9.2014 8:10:33: timespan 3 11.9.2014 8:10:35: entered checkuseractivity handler 11.9.2014 8:10:35: timespan 5 11.9.2014 8:10:37: entered checkuseractivity handler 11.9.2014 8:10:37: timespan 7 11.9.2014 8:10:37: timer disabled. 11.9.2014 8:10:37: go away soon.
the browser navigates homepage (example.com) after started. timer disabled, gets enabled @ end of loadcompleted
handler. user lingers bit , clicks link on page. loadcompleted
triggered (timer remains enabled, because didn't have change url yet). user lingers again, time not on homepage, condition inside checkuseractivity
true after while, timer disabled , browser should navigate home page, nothing happens... sad story :(
it threading problem - if replace gohome method following one, starts working.
enjoy :)
private void gohome() { dispatcher.invokeasync(() => { webbrowser.navigate(uri_home); }); }
Comments
Post a Comment