Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
See more:
Here is some code in one of my methods:
webBrowser1.Focus();
System.Windows.Forms.SendKeys.Send("{PGDN}");


I'm using this to force the webbrowser to scroll down so that a picture on the site I'm using is on screen.

Then I use:
findClick();


I have coded this to mean: using(image1) find image on screen and click.
It works fine.

MY PROBLEM IS:
(here is the next line of code):
webBrowser1.Focus();
System.Windows.Forms.SendKeys.Send("{PGDN}");
Point one = this.Location; //at this section my program doesn't wait for the above two commands to run before starting this line
//it runs the code before the screen has been scrolled so my picture finder never gets found.


How do I wait for a certain amount of time before executing the next line of command?
THREAD.SLEEP DOESN'T WORK!!!!!!!!!!
This command freezes the form and doesn't do anything for this problem. I also cannot use timers because I need the code to remain in this method (preferably in one line, like thread.Sleep)

How would I go about doing this?

ANOTHER SIMILAR PROBLEM:
using:
webBrowser1.Navigate("http://google.com");
second command;
third command;


This doesn't wait for the first command to finish so I would like to place a timer that says wait(5000) then continue.
I KNOW ABOUT DOCUMENTCOMPLETED events, I'm using this as an example!
Posted
Updated 2-Mar-11 19:51pm
v2
Comments
Toli Cuturicu 4-Mar-11 9:54am    
Colossal Absurdity. Stop programming or you'll hurt someone, something or yourself.

1 solution

Everything is so wrong, so I was puzzled how to answer.

You need to read about threading. Waiting is pointless. Sending keys is a big abuse. Why would you ever do that? You need to explain your goal to get an advice, but whatever you do, sending keys and waiting will never be a right tool.

Basically, you need to handle event System.Windows.Forms.WebBrowser.DocumentCompleted, something like this:

C#
MyBrowser.DocumentCompleted += (sender, eventArgs) {
    ((WebBrowser)sender).Print();
    //...
};


See http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.documentcompleted.aspx[^], http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.aspx[^].

—SA
 
Share this answer
 
v2
Comments
john123451 2-Mar-11 22:42pm    
Like I was saying, the code I used was an example, and I wouldn't take Thread.Sleep or DocumentCompleted as an answer. I was using the sendKeys as example code and the code I used has nothing to do with the problem I have.
Sergey Alexandrovich Kryukov 2-Mar-11 23:21pm    
If something "has nothing to do with the problem", why posting such code? I thought you try to use SendKeys to simulate manual operation because you did not know how to do it programmatically. This is a relatively common mistake. I'm glad you did not mean to make this mistake, but so was my impression based on your sample.

OK, great. I think I gave you right idea how to resolve your problem.

Please try and report back if you face any problem.

--SA
Sergey Alexandrovich Kryukov 2-Mar-11 23:39pm    
Wait a minute. You say "I wouldn't take... DocumentCompleted as an answer", I wonder why. This is how the problem should be resolved.
--SA
Albin Abel 2-Mar-11 23:23pm    
hi on document completed event the whole page is loaded. You can find any control...No need for scrolling down.
Albin Abel 2-Mar-11 23:24pm    
my 5 SAKryukov

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900