How to chain several events?

I'd like to chain together several methods that are fired by events.  For example, I have several methods that need to run based on an Internet Explorer webpage load completing:

1. IE.Navigate(url);
OnComplete(){
    FillForm();
    IE.Navigate(url2); //start second page load}

2. OnComplete(){
    ParsePage();
    IE.Navigate(url3); //start third page load}

3. OnComplete(){
    --do something}

I thought about giving each OnComplete a different name.  However, they all have to be tied to IE_Inst.DocumentComplete, which means all three will fire.  No good.  How can I create this chain based on the same kind of event?

Thanks,
Brett
brettrAsked:
Who is Participating?
 
Bob LearnedCommented:
Well, you can control this by using the DocumentComplete event, and using an If construct:

DocumentComplete()

   if (url == url1)
   {
      FillForm();
      IE.Navigate(url2); //start second page load}
   } else
      if (url == url2)
      {
        ParsePage();
        IE.Navigate(url3); //start third page load}
      } else
         {
            // Do something else.
         }
   

Bob
0
 
Bob LearnedCommented:
What's the difference between url2 and url3?

Bob
0
 
brettrAuthor Commented:
Just different webpages.  url2 may have a list of messages, whose URLs I will put into an array.  url3 is the actually URL of one message.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
jj819430Commented:
with events you can use the += operator to add them.
so if you want to chain a bunch of methods to the onload event

this.OnInit += New EventHandler(Function);
this.OnInit += New EventHandler(Function);
this.OnInit += New EventHandler(Function);

etc
0
 
brettrAuthor Commented:
That doesn't get around the problem of those events all firing at the same time when OnInit goes.  For example:

IE_Inst.DocumentComplete += new SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(this.onIEDocComplete);

I could then do the exact same thing using different methods in the param part:
this.onIEDocComplete2
this.onIEDocComplete3

But they will all three fire when DocumentComplete fires.  Not what I want.  Did I misunderstand you?
0
 
brettrAuthor Commented:
Bob, right, I could use some type of switch but I was hoping to avoid switches.
0
 
Neeraj SoniSr. ArchitectCommented:
if you want to make a chain of events #1, #2, #3,
better add event#1 to the IE_Inst.DocumentComplete += new ....
and call #2 at the end of #1 (inside #1) and #3 at the end of #2 (inside #2)

no?

this will be a chain execution of events,  if i understood right.
0
 
brettrAuthor Commented:
#2 will be based on a OnComplete event for the web browser page load.  I understand what you are trying to do but #1 and #2 will both fire at the same time since they are tied to
IE_Inst.DocumentComplete += new ....

How else are saying it will work?
0
 
Neeraj SoniSr. ArchitectCommented:
only #1 is to be attached to the DocumentComplete.

#2 is called inside #1
#3 is called inside#2

if it is the case you want, #1 executes first, then #2 and then #3
0
 
brettrAuthor Commented:
When #1 finishes, #2 will go.  #2 is just a method and not event correct?

#2 will go followed almost immediately by #3.  That flow is just procedural.  #2 will request a webpage, which could take up to 10 seconds to load.  #3 will go before this finishes since it runs immediately after #2.

So the above will not work.  I need #3 to wait for number #2, which means waiting for the page load in #2 to complete.
0
 
Bob LearnedCommented:
For all this to work, you would need synchronous events, which would be nearly impossible with the current AxWebBrowser model, and would be 10 times more complicated than using simple if statements, IMHO.

Bob
0
 
brettrAuthor Commented:
Chaining the events is working very well. At the end of each event, I remove the current event and add the next to keep the chain going.

IE_Inst.DocumentComplete -= new SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(this.onIEDocComplete);

IE_Inst.DocumentComplete += new SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(this.onIEDocComplete2);
0
 
Bob LearnedCommented:
As always, I have to keep a little humility, and never think that I know everything.  I am always interested in learning, and I guess I'll stop learning when I dead.

Bob
0
 
brettrAuthor Commented:
Thanks for all the input.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.