c# – WPF Design Considerations Using WebBrowser Control – Education Career Blog

I have to build a program that will allow me to track from a system different trigger points being hit.

When a trigger has been hit within another system a web service is called, this web service will log to a database the following:

  1. Affiliate Id
  2. Trigger Id
  3. Reference Code

My program is to read this data and invoke a call to affiliates websites for affiliate tracking.

The only trouble is, these sites do not offer any nice web service or form post, it is done via tracking pixels (images created from some kind of process on their server) or javascript.

So I have been playing around with this trying to get it working, and the only way I can think of doing it is via a WPF or WinForms application using the “WebBrowser” control.

So I simply get the results set out of the database and for each trigger captured get the required affiliate tracking image code or javascript code and then do replacements on predefined keys with the reference numbers, affiliate ids, etc.

My concern here, is as the WebBrowser control is asynchronous it may not process the javascript or image request before I ask the web browser to load another tracking code snippet.

List<TRACKING_TRANSACTIONS> trackingTransactions = affiliateContext.TRACKING_TRANSACTIONS.Where(at => at.EFFECTIVE_DATE <= DateTime.Now && at.PROCESSED_DATE == null ).ToList();

foreach (TRACKING_TRANSACTIONS transaction in trackingTransactions)
{

    if (transaction != null)
    {
        AFFILIATE_TRIGGERS affiliateTrigger = affiliateContext.AFFILIATE_TRIGGERS.SingleOrDefault(at => at.AFFILIATE_ID == transaction.AFFILIATE_ID && at.TRIGGER_ID == transaction.TRIGGER_ID);

        if (affiliateTrigger != null)
        {
            //do replacements
            string trackingCode = ReplaceStringValues(affiliateTrigger.TRACKING_CODE, transaction);

            RunWebBrowserTrackingCode(trackingCode);//ConfigurationManager.AppSettings"WebsiteUrl" + "?trackingTransactionId=" + transaction.TRACKING_TRANSACTION_ID.ToString());        
            Thread.Sleep(2000);

            transaction.PROCESSED_DATE = DateTime.Now;

            affiliateContext.SaveChanges();
        }
    }


}

The calls to the web browser are here

void RunWebBrowserTrackingCode(string trackingCode)
{

    if (!webBrowser.Dispatcher.CheckAccess())
    {
        webBrowser.Dispatcher.Invoke(
        System.Windows.Threading.DispatcherPriority.Normal,
            new Action(
            delegate()
            {
                webBrowser.NavigateToString(trackingCode);
            }
        ));
    }
    else
    {
        webBrowser.NavigateToString(trackingCode);
    }
}

My main concern is that using Thread.Sleep(2000); and making the program just wait is the wrong way of doing it.

Can anyone suggest a better way? Have I even tackled this using the right technologies here??

,

Take a look at the DocumentCompleted Event:

http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.ondocumentcompleted.aspx

Leave a Comment