C# WebBrowser doesn't show content - c#

I'm using WebBrowser and when I'm trying to call .Navigate(some_local_html) then nothing is displayed on my browser. If I then use MessageBox.Show(), then while message is shown I can see my html in browser. But when I close MessageBox, html is missing again.
I've tried Try-catch, but there was no errors.
I was trying to set default url on webBrowser control, and there is no result also. I can see nothing.
RESOLVED:
That wasn't a thread itself, but some kind of thread. I added next code:
Stream stream = null;
webBrowser1.DocumentStream = stream;
and forgot to remove it... That's a reason.
Thanks everyone!

Not sure if this will help at all, but it sounds like something is redrawing in the background, as when you put a messagebox up I am sure it sleeps the thread so nothing else can happen until it has been actioned, so whatever is overwriting it would be stopped temporarily.
If you do have something on that thread refreshing or redrawing frequently that could be causing your problems, try adding a button to your form which does a thread.sleep(1000) to see if that correctly displays your browser for a second.

It'd be helpful to know where you are calling your navigate and MessageBox functions. I quickly created a test to see if I could produce a similar result but the code below worked exactly as expected.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.webBrowser1.Navigate("http://www.google.com");
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Some Text");
}
}

Related

Revit API External Command freezing after WinForms Close() event

I am trying to create an external command for Revit that pops up a windows forms that has a single text box for the user to enter a description. After the user clicks Ok, the form closes but Revit gets stuck in a loading motion. It doesn't freeze, it just is constantly loading something. When I pause debugging, the code only made it to the line form.ShowDialog(). If I take out the showdialog and just run it without the form, it runs fine.
I'll post my relevant code snippets below. The form is in its own .cs, separate from the external command .cs but still under the same namespace.
Form click events code:
private void CancelForm(object sender, EventArgs e)
{
isReport = false;
this.DialogResult = DialogResult.Cancel;
Close();
}
private void SubmitForm(object sender, EventArgs e)
{
isReport = true;
reportDesc = descriptionBox.Text;
this.DialogResult = DialogResult.OK;
Close();
}
Execute command:
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
//Get application and document objects
UIApplication uiapp = commandData.Application;
Document doc = uiapp.ActiveUIDocument.Document;
ReportForm ticket = new ReportForm();
ticket.ShowDialog();
The code stops processing after showdialog. From what I could read on it, I thought it might have to do with focus on Revit after the form closing or the form not closing properly but couldn't figure out how to fix it.
Any help would be greatly appreciated!
Here is an old sample that creates a minimal Windows form on the fly, displays it as a modal form and closes successfully afterwards: The Schedule API and Access to Schedule Data. Maybe that will help. Maybe the debugger is interfering with the form closing somehow?
I ended up remaking the entire addin, made sure I was using Jeremy Tammik's Revit Addin Wizard to initiate it, and it started working. Not exactly sure what fixed it but guessing it was some error in the set up or such.

TaskCanceledException after closing window

So first of all, I have multiple windows which I hide when opening another window, so I have to use
Application.Current.Shutdown();
to completely close the Application when pressing the "x" on the top right.
I wanted to handle the WindowClosing Event in my Home.xaml.cs file. But if I do this:
public Home()
{
InitializeComponent();
Closing += WindowClosing.OnWindowClosing;
}
then I am getting an System.Threading.Tasks.TaskCanceledException when I am closing the window.
Here is the WindowClosing event handler:
public static void OnWindowClosing(object sender, CancelEventArgs e)
{
Application.Current.Shutdown();
}
The weird part is, that I´ve done exactly the same with the login window, and it works there without any trouble.
I´ve stepped through it, and the closing event gets set as it should (in the Login.xaml.cs, as also in the Home.xaml.cs File).
I know this is not much Information for this error (I think thats all, but maybe the error is coming from somewhere else?!), but maybe someone else encountered this issue and can help me.
If you need more information just tell me, I will edit the question then.
Thanks for your help!
EDIT:
The Solution was taking Environment.Exit(0); instead of Application.Current.Shutdown();
In your OnWindowClosing() method, you can try substituting the following line for Application.Current.Shutdown():
Environment.Exit(0);

c# webBrowser.ReadyState will not achieve Complete state inside DocumentCompleted event?

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.

How do I go to a webpage, load, then do some code?

I've tried using Sleep but that just froze the program. Is it messing up because it's in the timer? I can get it to go to the site and not do code, but once I put other code it doesn't load properly.
I also tried putting code in:
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) but then I had even more problems.
private void timer1_Tick(object sender, EventArgs e)
{
//code here
if (click == null)
{
webBrowser1.Url = new System.Uri("http://url.com", System.UriKind.Absolute);
timer1.Stop();
//load or wait x seconds
timer1.Start();
}
EDIT:
I'm trying to go from one page to the next and doing code once the page is loaded. If I put code in webBrowser1_DocumentCompleted it messes up the code before it. How do I add DocumentCompleted to only this one instance?
First, it seems like ur approach is wrong,
Open the url in the web browser control.
Wait for DocumentCompletedEvent. It will come once page is loaded fully.
Write ur code in document completed event based on your criteria/condition.

WPF form should resize to show its content

When WPF window appear first time, its content seem frozen. To refresh content I need to resize form, then it will be fixed. Or I hit the TAB then find a listbox -it's not visible- and click it and viola! Form updates its content again.
What do you think? Weird huh? Thanks in advance!
Edit:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.Show();
while (!AppMain.needClose)
{
System.Windows.Forms.Application.DoEvents();
DoThings();
}
}
Resizing the window would force the internals to invalidate and re-paint. You could try invalidating the form when it's loaded to force it to do the same:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.Show();
this.Invalidate();
while (!AppMain.needClose)
{
System.Windows.Forms.Application.DoEvents();
DoThings();
}
}
Unless you're doing some sort of custom message pumping though, the standard forms message pump should do that while loop for you. You might well find that because you're intercepting the window loaded event you're stopping initialisation from completing.
Calling DoEvents is a bad code smell in my experience. If you need to do something periodically, it's better to trigger it from a timer of some sort.
When your window is loaded it's already shown, why are you calling this.show() again??

Categories