views:

18

answers:

1

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??

+1  A: 

Take a look at the DocumentCompleted Event:

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

kyndigs
I considered that, but then could not work out how I would itterate through my list using this to wait for it to complete. Unless I have some sort of variable that says it is ok to process the next object in the list, kind of like a leaky bucket?? But this would have me running a tight loop, while(cannotProcessNextItem){//sit and do nothing}
jimplode
Had to use a different event in WCF, I am now waiting for the webpage to load completely, then I process the next one.
jimplode