WebForm's WebBrowser consecutive navigation - c#

VS 10, WinFormApp
I have a list of url's. I am trying to navigate to the URL's one by one. After completing the load of the document of a page, then the integrated webbrowser will navigate to the next URL.
Here is some code:
private void btnHit_Click(object sender, EventArgs e)
{
webBrowser1.Navigate(lstUrls[counter].url);
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (e.Url != webBrowser1.Url)
{
lblStatus.Text = "Page is loading....";
lblStatus.ForeColor = Color.Red;
return;
}
else
{
funcMethod();
}
}
public void funcMethod()
{
lblStatus.Text = "Page is loaded";
lblStatus.ForeColor = Color.Green;
try
{
webBrowser1.Document.Focus();
webBrowser1.Navigate(lstUrls[++counter].url);
}
catch { }
}
The problem is, when I debug for each element line by line, it works for all(100+) items in the List of URL's. but when I just press the btnHit, then sometimes it loads 2/3/4/5/.. number of urls. and stop loading further. In that time, If I hit the btnHit again, the process starts and same thing happens as before.
What am I missing?

Related

c# webbrowser navigate loop freezing form

I'm trying to display a website in a form and automatically reload the website to see if a certain text is present. I'm doing this in a C# form and try to call a while with an button. This is the code I made:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnStartLoop_Click(object sender, EventArgs e)
{
lblStatusLoop.Text = "Status loop: Started";
lblStatusLoop.ForeColor = System.Drawing.Color.Green;
int i = 0;
while (i < 10)
{
Browser.Navigate("www.google.com");
Browser.DocumentCompleted += Browser_DocumentCompleted;
Thread.Sleep(5000);
i++;
}
}
void Browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
string ZoekText = txtSearchbox.Text.ToString();
if (Browser.DocumentText.Contains(ZoekText))
{
lblSearchResult.Text = "Text found";
}
else
{
lblSearchResult.Text = "Text not found";
}
}
}
As soon as I push the button the appliction freezes and I have to force quit it. I've done some research on google and suggestions were to wait for the document to complete loading but it has no effect.
I don't have much programming experience and do not have someone to fall back on so I hope someone here can point me into right direction.

Windows forms not rendering buttons, images, launching application at runtime - C#

I'm having this issue for a while now on some machines, when the user runs the application all the buttons, images, menu items, almost everything goes blank.
My first thought was: I have a separate thread using BackgroundWorker updating a StatusBar and I've read that this can cause issues to form rendering.
I found an article (it's in portuguese, since I'm a Brazilian) that explains this is a issue caused by a security plugin installed by our banks called Warsaw. One of the solutions was to contact the developers to have them analyze your software so the plugin will work correctly. I've done this and I'm waiting for their response.
The other solution is to sign our assembiles so the plugin may recognize as a trusted software, this is good but not free of charge unfortunately.
Now, here's an example of my application looks like when it's launched with this issue:
A form with a GridView
The code for the separate thread as mentioned above:
int countAlerta = 0;
int countAlertaRFRP = 0;
int countAlertaAditivo = 0;
private void timer1_Tick(object sender, EventArgs e)
{
if (!backgroundWorker1.IsBusy)
backgroundWorker1.RunWorkerAsync();
else
{
alertaToolStripStatusLabel.Text = "Continuando a verificação...";
alertaToolStripStatusLabel.IsLink = false;
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
backgroundWorker1.ReportProgress(0);
timer1.Enabled = false;
lock (new object())
{
countAlerta = new BLL.AlertasBLL().CountRVSRAS();
countAlertaRFRP = new BLL.AlertasBLL().CountRFRP();
countAlertaAditivo = new BLL.AlertasBLL().CountAditivos();
}
backgroundWorker1.ReportProgress(100);
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
alertaToolStripStatusLabel.Text = "Verificando...";
alertaToolStripStatusLabel.IsLink = false;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (countAlerta + countAlertaAditivo + countAlertaRFRP > 0)
{
alertaToolStripStatusLabel.Text = "ALERTA: Verifique!!!";
alertaToolStripStatusLabel.IsLink = true;
alertaToolStripStatusLabel.Font = new Font(alertaToolStripStatusLabel.Font, FontStyle.Bold);
}
else
{
alertaToolStripStatusLabel.Text = "Não existem alertas";
alertaToolStripStatusLabel.IsLink = false;
}
timer1.Enabled = true;
}

web browser cannot work with 1 click

I have a situation which occurs in WebBrowser with C#.
I am trying to do downloader through a site However , when i click first time, it doesn't work but if I click second times it works.
How can i solve this problem .
Sincerely.
Codes :
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Document.GetElementById("youtube-url").SetAttribute("value", textBox1.Text);
webBrowser1.Document.GetElementById("submit").InvokeMember("click");
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
label3.Text = "Video Alındı , indirme işleminin hazır olması bekleniyor";
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlElementCollection col = webBrowser1.Document.GetElementsByTagName("a");
String link = "";
foreach (HtmlElement el in col)
{
if (el.InnerText == "Download")
{
link = el.GetAttribute("href");
Download(link);
label3.Text = "Video indiriliyor";
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.Navigate("http://www.youtube-mp3.org/tr");
}
void Download(String link)
{
WebClient downloader = new WebClient();
downloader.DownloadFileAsync(new Uri(link),#"D:\a.mp3");
downloader.DownloadProgressChanged += new DownloadProgressChangedEventHandler(downloader_DownloadProgressChanged);
}
void downloader_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
label3.Text = "Video İndiriliyor : " + progressBar1.Value + "%";
if (progressBar1.Value == 100)
label3.Text = "Video İndirildi";
}
You're blocking yourself from investigating what the problem is. It's never a good idea to disable script errors for WebBrowser control (as you do with ScriptErrorsSuppressed = true), unless you handle them internally in your host app. Do the following:
enable script errors (ScriptErrorsSuppressed = false);
enable script debugging for all applications;
implement WebBrowser Feature Control (FEATURE_BROWSER_EMULATION), so the web page is getting the same (or close) experience and HTML features as with full-featured IE browser.
Then, hopefully, you can find out what's going wrong when you're simulating a button click, and debug it.

Duplex printing with WebBrowser control only printing 1 side

I'm having an issue with my .NET application only printing the second page of my HTML file, and completely ignoring the first page (no other page is printed, and the back of it is blank).
When I pull up my printer's queue window, it does show it go from "Spooling" to "Printing" and lists both pages, so I'm at a loss as to why it's not printing the first page.
(My printer IS set to duplex printing, and if I literally just print the HTML document from my browser, it works as expected)
Here's what I'm doing:
private void Form1_Load(object sender, EventArgs e)
{
// Create a FileSystemWatcher to monitor all files on drive C.
FileSystemWatcher fsw = new FileSystemWatcher("C:\\COAForms");
// Watch for changes in LastAccess and LastWrite times, and
// the renaming of files or directories.
fsw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Register a handler that gets called when a
// file is created, changed, or deleted.
//fsw.Changed += new FileSystemEventHandler(OnChanged);
fsw.Created += new FileSystemEventHandler(OnChanged);
fsw.Error += new ErrorEventHandler(fsw_Error);
//fsw.Deleted += new FileSystemEventHandler(OnChanged);
fsw.EnableRaisingEvents = true;
fsw.SynchronizingObject = this;
PrinterSettings settings = new PrinterSettings();
label2.Text = settings.PrinterName;
Thread.CurrentThread.SetApartmentState(ApartmentState.STA);
}
void fsw_Error(object sender, ErrorEventArgs e)
{
MessageBox.Show(e.ToString());
}
private void OnChanged(object source, FileSystemEventArgs e)
{
notifyIcon1.BalloonTipText = "Printing document " + e.Name + "...";
notifyIcon1.BalloonTipTitle = "Printing Application";
notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
notifyIcon1.ShowBalloonTip(500);
PrintCOAPage(e.Name);
}
private void PrintCOAPage(string name)
{
try
{
// Create a WebBrowser instance.
WebBrowser webBrowserForPrinting = new WebBrowser();
// Add an event handler that prints the document after it loads.
webBrowserForPrinting.DocumentCompleted +=
new WebBrowserDocumentCompletedEventHandler(PrintDocument);
// Set the Url property to load the document.
webBrowserForPrinting.Url = new Uri(#"C:\COAForms\" + name);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void PrintDocument(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
try
{
PrinterSettings ps = new PrinterSettings();
ps.Duplex = Duplex.Vertical;
// Print the document now that it is fully loaded.
((WebBrowser)sender).Print();
// Dispose the WebBrowser now that the task is complete.
((WebBrowser)sender).Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Show();
this.Activate();
if (this.WindowState == FormWindowState.Minimized)
{
this.WindowState = FormWindowState.Normal;
}
}
private void Form1_Resize(object sender, EventArgs e)
{
if (FormWindowState.Minimized == WindowState)
{
Hide();
}
}
I only just recently added the PrinterSettings to the code and it changed nothing.
I would greatly appreciate any help you guys can provide on this! Thank you!
Wow, didn't think the CSS would impact it, but for whatever reason the CSS I was using (involving z-index) made the first page NOT display when I print previewed under the WebBrowser control, but worked just fine in actual IE8.. after changing the CSS around a bit it now works as intended.

C# app with WebBrowser (WinForms) control causes vshost to crash after a while

Below is my code, and what happens when I run this code, first of all it runs for about an hour freely (eating a lot memory - starts from around 400MB RAM and goes up to 1GB), but after an hour or so VSHOST crashes and Visual Studio doesn't catch any exception..
Any ideas why this is happening?
tnx :)
private void UpdateLastPosted()
{
WebClient wc = new WebClient();
string html = wc.DownloadString("http://blogs.com/lastblogs.aspx");
MatchCollection collection = Regex.Matches(html, #"blogs\.com/blogread\.asp\?blog=(\d+)");
foreach (Match pend in collection)
{
pending.Enqueue(pend.Groups[1].Value);
}
}
private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (((WebBrowser)sender).ReadyState == WebBrowserReadyState.Complete)
{
if (pending.Count > 0)
{
((WebBrowser)sender).Stop();
NavigateTo("http://blogs.com/blogread.asp?blog=" + pending.Dequeue());
}
else
{
UpdateLastPosted();
UpdateMostActive();
if (pending.Count > 0)
{
((WebBrowser)sender).Stop();
NavigateTo("http://blogs.com/blogread.asp?blog=" + pending.Dequeue());
}
}
}
}
public void NavigateTo(string url)
{
browser.Navigate(url);
}
For every element in the html page the method browser_DocumentCompleted is called. Try specifiying the exact or similar url :
private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if(e.Url.OriginalString.ToLower() == "http://www.myisite.com/contact.aspx")
{
......

Categories