WaitOne while I introduce multithreading
For some time I've been meaning to do some multi threading in the Dynamics SOP Fulfilment software I wrote. I have always thought that I could make things run a little more slickly. At fulfilment the following processes occur;
-
Credit/debit card debited if one is attached to the order
-
Order lines are fulfilled by quantities on the pick list being fulfilled
-
receipt printed (optional depending on carriage option- customer collect etc)
-
despatch note printed (optional)
-
invoice printed (optional)
-
thermal parcel label(s) printed one for each parcel entered
-
consignment details are exported via XML to UPS / Business Post / TNT depending on carriage routing algorithm
-
UI is constantly updated with progress bar and messages about progress
Spinning the fulfilment thread off on to its own thread using a background worker control was straight forward and is something I did when VS2005 was released. However some of the above processes take quite a few seconds to run, the fulfilment computers are quite recent hardware and thus should be able to perform better.
Thanks to good object orientated design, it turned out to be fairly trivial to get each of these processes to run on their own thread and synchronise them using wait handles. I had to use wait handles as some tasks depend on the successful completion of others. [more]
The most challenging part ending up being the coordination of any exceptions raised. as most of these activities interact a lot with unreliable external resource (print RPC servers, carrier computers, etc) error handling had to be spot on. This turned out to be critical when it came to the card debiting, where unfortunately an error getting lost on another thread and never getting raised on the fulfilment thread would result in an order going out the door with no payment. Something very undesirable. Unfortunately this happened when the company internet connection went down to reboot the firewall and we found orders had not been debited. The net.webexception from the http call to the card processor got lost on the wrong thread... whoops. Took a couple of hours to work out what had happened, as I didn't have the information about the outage. So lesson learnt, be very diligent about exception handling on complicated mission critical multi threaded applications!
After the work everything works much more smoothly and I found some oversights in the code that may have bitten me in the future during the clean up and refactoring of the blocks of code.