Search code examples
c#wpfwpf-controlsmshtml

WPF C# HTMLDocument variables automatically updates


So I am trying to compare two HTMLDocuments to see if there are any changes in the website using a DispatchTimer().

Here is my code:

    HTMLDocument lastDoc;

    public void startTimer()
    {
        lastDoc = (HTMLDocument)Form.RosterBrowser.Document;

        DispatcherTimer dispatcherTimer = new DispatcherTimer();
        dispatcherTimer.Tick += dispatcherTimer_Tick;
        dispatcherTimer.Interval = new TimeSpan(0, 0, 2);
        dispatcherTimer.Start();
    }
    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
        var thisDoc = (HTMLDocument)Form.RosterBrowser.Document;
        LogTextBlockControl.Text += "DOCUMENT THIS: " + thisDoc.getElementById("groupList").innerText.Length.ToString();
        LogTextBlockControl.Text += "DOCUMENT LAST: " + lastDoc.getElementById("groupList").innerText.Length.ToString();
    }

As you can see: When the times starts for the first time I get the HTMLDocument and store it in lastDoc. Then, every 2 seconds, I get another HTMLDocument variable and I store it in thisDoc. Now, I print the length of a certain element every 2 seconds to see if anything has changed inside this element.

When the program first starts, they both print the same number which is normal since they both got the same HTMLDocument. But lets say I change something in the groupList element. You'd think the length of the thisDoc variable would change. Well it does, but so does the length of lastDoc. This is where the problem occurs.

Whenever an element changes, thisDoc gets updated and prints the length of the changed element but lastDoc gets updated as well and starts printing the same length. This is not what I want since now I can't compare the two to fire a function. I only call startTimer() once in the entire program and I never change lastDoc, it seems to change on itself. I have been sitting on this problem for a day now, I hope someone can help me out.


Solution

  • Form.RosterBrowser.Document returns a reference to the browser Document, so lastDoc and thisDoc are two references pointing to the same HTMLDocument object which sits somewhere on the memory heap.

    Instead you should store the value you are monitoring.
    You'd better monitor the text itself and not only its length as the text can be changed but keeping the same length.

    string lastText;
    
    private string GroupListText => ((HTMLDocument)Form.RosterBrowser.Document).getElementById("groupList").innerText;
    
    public void startTimer()
    {
        lastText = GroupListText;
    
        DispatcherTimer dispatcherTimer = new DispatcherTimer();
        dispatcherTimer.Tick += dispatcherTimer_Tick;
        dispatcherTimer.Interval = new TimeSpan(0, 0, 2);
        dispatcherTimer.Start();
    }
    
    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
        var thisText = GroupListText;
        LogTextBlockControl.Text += "DOCUMENT THIS: " + thisText.Length.ToString();
        LogTextBlockControl.Text += "DOCUMENT LAST: " + lastText.Length.ToString();
    }
    

    I've used a property GroupListText to avoid duplicating the text lookup expression.