how can i set timeout for webBrowser navigate (url) event
c# netframework 4.0
By using a Timer of course. For example:
public void NavigateTo(Uri url) {
webBrowser1.Navigate(url);
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e) {
timer1.Enabled = false;
MessageBox.Show("Timeout on navigation");
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
if (e.Url == webBrowser1.Url && timer1.Enabled) {
timer1.Enabled = false;
// etc..
}
}
I am using following approach based on Navigating and Navigated events. The time between these two events are observed for redirecting to home pgae.
//Navigation Timer
timer2.Enabled = true;
timer2.Interval = 30000;
br.DocumentCompleted += browser_DocumentCompleted;
br.DocumentCompleted += writeToTextBoxEvent;
br.Navigating += OnNavigating;
br.Navigated += OnNavigated;
br.ScriptErrorsSuppressed = true;
br.Navigate(ConfigValues.websiteUrl);
private void OnNavigating(object sender, WebBrowserNavigatingEventArgs e)
{
//Reset Timer
timer2.Stop();
timer2.Start();
WriteLogFunction("OnNavigating||||||"+e.Url.ToString());
}
private void OnNavigated(object sender, WebBrowserNavigatedEventArgs e)
{
//Stop Timer
timer2.Stop();
WriteLogFunction("NAVIGATED <><><><><><><> " + e.Url.ToString());
}
private void timer2_Tick(object sender, EventArgs e)
{
WriteLogFunction(" Navigation Timeout TICK");
br.Stop();
br.Navigate(ConfigValues.websiteUrl);
}
Reference
Create a time-out for webbrowser loading method
webbrowser timeout if page wont load
Related
Long Story Short,
The app I am making will Launch a Game.
The Start button will then Check to make sure the games EXE is running..
IF it is running, it will run a script to press buttons 1, 2, and 3..
After that it will loop that script, but first checking if the game has not crashed (if it crashed it wont run the loop)
My Issue:
While the loop is running, which is using System.Threading.Thread.Sleep,
does not let other functions of the app preform (in this case showing and hiding button, and changing label colors.) The main reason this is important is the button it wont is the Stop button which is suppose to end the looping script.
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
Timer timer;
Stopwatch sw;
public Form1()
{
InitializeComponent();
button4.Visible = false;
button2.Enabled = false;
}
private void timer_Tick(object sender, EventArgs e)
{
label2.Text = sw.Elapsed.Seconds.ToString() + "seconds";
Application.DoEvents();
}
// ===============================================
// BUTTON FUNCTIONS
// ===============================================
// Launch GAME
private void button1_Click(object sender, EventArgs e)
{
// Launch GAME
Process.Start(#"C:\Windows\System32\Notepad.exe");
button2.Enabled = true;
}
// START BOT
private void button2_Click(object sender, EventArgs e)
{
status.Text = #"Starting Bot..";
timer = new Timer();
timer.Interval = (1000);
timer.Tick += new EventHandler(timer_Tick);
sw = new Stopwatch();
timer.Start();
sw.Start();
BotReady(sender, e);
}
// PLAYER DIED
private void button3_Click(object sender, EventArgs e)
{
KillBot(sender, e);
status.Text = #"lol u ded";
}
// STOP THE BOT
private void button4_Click(object sender, EventArgs e)
{
KillBot(sender, e);
status.Text = #"Bot Stopped";
button2.Visible = true;
button4.Visible = false;
button2.Enabled = true;
}
// KILL GAME AND BOT (IF IT CRASHED OR SOMETHING)
private void button5_Click(object sender, EventArgs e)
{
}
// ===============================================
// OTHER FUNCTIONS
// ===============================================
// Target GAME application
private void TargetAQ(object sender, EventArgs e)
{
// this part doesnt work yet.
// Selection.Application and Tab to it
}
// CHECK IF GAME IS STILL RUNNING, KILL BOT IF GAME IS NOT DETECTED
public void CheckStatus(object sender, EventArgs e)
{
Process[] GAME = Process.GetProcessesByName("notepad");
if (GAME.Length == 0)
// GAME NOT running
{
KillBot(sender, e);
button2.Enabled = false;
status.Text = #"GAME is not Running, Bot Stopped.";
uGAME.ForeColor = Color.Red;
button2.Visible = true;
button4.Visible = false;
button2.Enabled = false;
}
else
// GAME IS running
{
status.Text = #"GAME is Running!";
uGAME.ForeColor = Color.Green;
button2.Visible = false;
button4.Visible = true;
}
}
// Verify bot and GAME are running before starting
public void BotReady(object sender, EventArgs e)
{
CheckStatus(sender, e);
if (uGAME.ForeColor == Color.Green)
{
status.Text = #"Bot Started!";
Botting(sender, e);
}
else { status.Text = #"GAME is not running"; }
}
//THE BOT ACTIONS
public void Botting(object sender, EventArgs e)
{
// Check to make sure everything is still running
CheckStatus(sender, e);
TargetAQ(sender, e);
if (uGAME.ForeColor == Color.Green)
{
// all is running, then you good.
Script(sender, e);
}
//GAME died, kill scripts
else {
KillBot(sender, e);
status.Text = #"GAME Crashed:(";
}
}
//Things it does in-game
public void Script(object sender, EventArgs e)
{
status.Text = #"Bot in progress..";
// Use skills in game rotation
System.Threading.Thread.Sleep(3000);
SendKeys.Send("1");
System.Threading.Thread.Sleep(3000);
SendKeys.Send("2");
System.Threading.Thread.Sleep(3000);
SendKeys.Send("3");
// Go back and check the game has not crashed before re-running the script
Botting(sender, e);
}
// STOP THE BOT AND TIME COUNTER
public void KillBot(object sender, EventArgs e)
{
// kill the clock and bot
status.Text = #"Stopping bot...";
timer.Stop();
sw.Stop();
label2.Text = sw.Elapsed.Seconds.ToString() + " seconds";
status.Text = #"Bot Stopped";
}
}
}
I want to stop browser navigating,when i click button.I tried in the below method but it doesn't worked.
private void webBrowser1_Navigating(object sender, NavigatingEventArgs e)
{
button2.Click += new RoutedEventHandler((object caller,System.Windows.RoutedEventArgs f) =>
{
e.Cancel = true;
});
}
Try this one-
private void webBrowser_Navigating(object sender, NavigatingEventArgs e)
{
stop.IsEnabled=true;
stop.Click += new RoutedEventHandler((object caller, System.Windows.RoutedEventArgs f) =>
{
stopPressed = true;
});
if (stopPressed == true)
e.Cancel = true;
}
If you want to stop the browser navigating you can try like this:
private void stop_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
MiniBrowser.InvokeScript("eval", "document.execCommand('Stop');");
}
I am trying to detect the internet connection, if the internet connection is available and it is connected, it will continue, otherwise it will throw message box says that the connection is not available.
What I am encounter is whether the internet connection is connected or not connected, the code will continue.
Here is the code:
** The program will continue to worker_ProgressChanged, even though there is no internet connection available **
public CheckUpdates()
{
InitializeComponent();
bool checkConnection = CheckConnection.IsConnectedToInternet();
progressBar1.Style = ProgressBarStyle.Marquee;
if (checkConnection == true)
{
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.DoWork += new DoWorkEventHandler(worker_DoWork);
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
}
else
{
System.Media.SoundPlayer _sound = new System.Media.SoundPlayer(#"C:\Windows\Media\Windows Notify.wav");
_sound.Play();
DialogResult _dialogResult = MessageBox.Show("No connection available, please check your internet connection!", "No connection");
if (_dialogResult == DialogResult.OK)
{
this.Hide();
this.Close();
}
}
}
private void CheckUpdates_Load(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
if (e.ProgressPercentage.Equals(100))
{
System.Media.SoundPlayer _sound = new System.Media.SoundPlayer(#"C:\Windows\Media\Windows Notify.wav");
_sound.Play();
DialogResult _dialogResult = MessageBox.Show("No updates were available!", "No updates");
if (_dialogResult == DialogResult.OK)
{
this.Hide();
this.Close();
}
}
}
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
_timer.Enabled = true;
_timer.Tick += new EventHandler(Timer_Tick);
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i <= 100; i++)
{
backgroundWorker1.ReportProgress(i);
System.Threading.Thread.Sleep(100);
}
}
void Timer_Tick(object sender, EventArgs e)
{
_timer.Enabled = false;
}
class CheckConnection
{
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);
public static bool IsConnectedToInternet()
{
int Desc;
return InternetGetConnectedState(out Desc, 0);
}
}
Your answer much appreciated!
Thank you very much!
Because you call backgroundWorker1.RunWorkerAsync(); in the event WelcomeScreen_Load or in the event CheckUpdates_Load without checking if the Internet is connected or not. (Probably the worker_ProgressChanged is defined at design time withing the backgroundWorkder properties)
These events doesn't seems to be related to a Form.Load event handler because your class doesn't derive from Form. However it seems clear that you need to put a check there otherwise whoever triggers these events will start your background worker.
private void CheckUpdates_Load(object sender, EventArgs e)
{
if(CheckConnection.IsConnectedToInternet())
backgroundWorker1.RunWorkerAsync();
}
private void WelcomeScreen_Load(object sender, EventArgs e)
{
if(CheckConnection.IsConnectedToInternet())
backgroundWorker1.RunWorkerAsync();
}
I'm trying to get source code of a webpage and save it to richtextbox and that web browser control navigates to new URL. But I'm getting a blank rich text box. Here is my code:
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
webBrowser2.Navigate("http://www.gmail.com");
}
private void timer1_Tick(object sender, EventArgs e)
{
if(webBrowser2.ReadyState == WebBrowserReadyState.Complete)
{
timer1.Enabled = false;
richTextBox1.Text = webBrowser2.DocumentText;
webBrowser2.Navigate("new URL");
}
}
I see two alternatives to what you're doing.
1) Using DocumentCompleted:
private void Form_Load(object sender, EventArgs e) {
webBrowser.Navigate("www.google.com");
}
private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
richTextBox.Text = webBrowser.DocumentText;
}
2) Using the WebClient:
private void Form_Load(object sender, EventArgs e) {
using (System.Net.WebClient wc = new System.Net.WebClient()) {
richTextBox.Text = wc.DownloadString("http://www.google.com");
}
}
I cannot figure out how to catch the Navigating event on the WebBrowser control. Basically I'm trying to figure out how trigger the progress bar to show when a user clicks a link on a page.
Here is the code I use to show the progress bar and then hide it on page loaded. Can someone help me out with the event handler for Navigating?
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
progressBar.IsIndeterminate = true;
progressBar.Visibility = Visibility.Visible;
webBrowser.Navigate(new Uri(MY_URL, UriKind.Absolute));
webBrowser.LoadCompleted += new System.Windows.Navigation.LoadCompletedEventHandler(PageLoadCompleted);
webBrowser.Navigating = ?
}
private void PageLoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
progressBar.IsIndeterminate = false;
progressBar.Visibility = Visibility.Collapsed;
}
The documentation you seek is here. You can write
webBrowser.Navigating += webBrowser_Navigating;
// ...
void webBrowser_Navigating( object sender, NavigatingEventArgs e )
{
// ...
}
The answer of by VisualStuart helped me solve my problem.
My now working code is as below :
private void MyButton1_Click(object sender, RoutedEventArgs e)
{
MyprogressBar.IsIndeterminate = true;
MyprogressBar.Visibility = Visibility.Visible;
string site = MyTextBox1.Text;
webBrowser1.Navigate(new Uri(site, UriKind.Absolute));
webBrowser1.Navigating += webBrowser1_Navigating;
webBrowser1.LoadCompleted += new System.Windows.Navigation.LoadCompletedEventHandler(webBrowser1_LoadCompleted);
}
private void webBrowser1_Navigating(object sender, NavigatingEventArgs e)
{
MyTextBox1.Text = e.Uri.ToString();
MyprogressBar.IsIndeterminate = true;
MyprogressBar.Visibility = Visibility.Visible;
}