Sunday, 29 June 2014
Overview Of Asynchronous Tools in C# and C++
The tools to do it have evolved a lot over the past few years, both in C# and C++.
Saturday, 24 May 2014
Adapter And Dependency Injection Without The Pretty Diagrams
Monday, 12 May 2014
Java vs C++ vs C#: What Is The Best Programming Language?
Tuesday, 11 September 2012
Using Reactive Extensions with Bloomberg
Lately I had a chance to use the Bloomberg API to build a price capture service. In the context of finance the Bloomberg API -when run on a PC where the Bloomberg terminal is installed- allows you to read prices and rates for financial instruments. The “Bloomberg terminal” is an expensive piece of software that looks like nothing you’ve seen before unless you are old enough to have used the French Minitel. It comes with a funny-looking keyboard and a Mission Impossible-style fingerprint authentication system.
The API is straightforward and fully asynchronous. When opening a session you specify a single delegate that processes all notifications received from Bloomberg.
Bloomberg provides several services, some for real-time market data, others for reference static information, another for historical data… When you start a session, Bloomberg sends you a SESSION_STATUS message confirming the session is up. If you open a service you get SERVICE_STATUS messages . If you start subscribing to an instrument’s updates Bloomberg sends SUBSCRIPTION_DATA messages with the price updates as well as SUBSCRIPTION_STATUS messages containing subscription errors if something went wrong.
When writing such a client you find yourself waiting for a message before taking action. For instance you wait for a service to open before subscribing with instruments, you wait for subscription updates to arrive in order to save them to memory…
If you take action following the reception of a message, you run this action in the same call stack as the delegate you passed to the API. Is that a problem? Yes it can be: if your action takes too long, you slow down the Bloomberg thread consuming the messages and the system starts sending you ‘Slow Consumer’ warning admin messages.
So in the end you have to do things such as:
- redirect execution from a worker thread to the main thread (to avoid blocking the consumer thread)
- fire off a timer at regular intervals to display stats,
- schedule tasks at a set time (to schedule snapshots for instance)
- wait for several tasks to complete before starting a new one
Of course you could do all this using threads/handles/message loops, or the BackgroundWorker pattern which I used a lot when building windows UIs but it’s just not practical. So I started looking up MSDN to get familiar with the latest tools that make async programming easier (in .NET 4.0) with BeginInvoke / EndInvoke, thread pooling, the TPL…
@smwhit told me “Stop being such a C++ tard and take a look at Reactive Extensions”
Indeed RX is spot-on for this. One of the very very nice things with Reactive Extensions is that you can parameterize the scheduler. For each delegate you register you simply set a parameter to decide whether you want the code to run on a new thread, a thread pool, a dispatcher, an event pool or on the current thread the next time it’s available. You can even replace the scheduler with a TestScheduler, which makes unit-testing concurrent code possible.
Resources:
- Some examples of RX usage on Synchronicity and Code Project.
- The RX Workshop on Channel 9, including the one about parameterizing concurrency with schedulers.
- Virtual Time Scheduling
Thursday, 16 August 2012
More about Windows Services…
Following up from a previous blog post:
- How to create a service project?
- How to create a setup project to install a service?
- How to debug it?
- Uninstall a service manually
To violently remove a service from the Services window when uninstall fails, type at the command prompt:
sc delete "My Service"
Thursday, 26 April 2012
How to set the Thread Pool Size in Quartz.Net?
By default Quartz.net creates a pool with 10 threads. How to change the size of the pool?
This is not obvious from the documentation, but burried inside one of the examples that come with the library:
NameValueCollection properties = new NameValueCollection(); properties["quartz.threadPool.threadCount"] = "1"; ISchedulerFactory sf = new StdSchedulerFactory(properties); IScheduler sched = sf.GetScheduler();
Why did I need to change the pool size to 1? Well my jobs are executing third-party .NET wrappers around some native code that is not exactly thread-safe. I had a situation where I couldn’t schedule the same job twice: on first execution the job would run fine on thread 1. On second execution the same job would start on thread 2 then hang on a 3rd party method call. Forcing both runs to happen on the same thread fixed the problem.
Tutorial:
http://quartznet.sourceforge.net/tutorial/lesson_10.html
API Documentation
http://quartznet.sourceforge.net/apidoc/2.0/html/webframe.html
Monday, 5 March 2012
Currently Watching…
...while brushing my teeth
GoingNative 2012
- Threads and Shared Variables in C++11 (Hans Boehm) no need to use any platform-specific code to create threads, it’s now part of the language.
- STL11: Magic && Secrets (Stephan Lavavej) shared pointers have been improved over boost.
- A Concept Design for C++ (Bjarne Stroustrup) what concepts are and how they were implemented in C++ 11.
Misc
- Introducing Windows Runtime in Windows 8 (Bart De Smet) the language projections allow direct access to the Windows API with 0 interop code from native C++, managed C# and Javascript. The API appears natural to each language. That’s a big change over the annoying C-style Win32 with its LPTSTR and HSTRUCTs…
- Windows Runtime internals: understanding “Hello World”
- C#5, ASP.NET MVC4 and Asynchronous Web Applications more interesting than the title suggests. Gives a historical view with live demos of the various ways of making asynchronous calls from polling to await.
Monday, 1 December 2008
Get time in HH:mm format regardless of the local culture
Sunday, 11 May 2008
Things missing from C++
- No cpp/h separation
- Automatic creation of properties from a member field with snippets
- Automatic implementation of interface methods in concrete classes.
- The .NET collections (I am currently using the STL collections, they're a real pain in the bum)
- Interfaces: although you can simulate interfaces in C++ by creating a class only just virtual pure methods and no field, there is still a risk it turns into a base class if someone adds some code to a method...
Wednesday, 6 December 2006
Handy Collections
- Generic key/value pair collection that automatically sorts based on the key: SortedList<TKey,TValue>
- Same as above but without the automatic sorting and better performance with large number of items: Dictionary<TKey,TValue>
http://msdn2.microsoft.com/en-us/library/5tbh8a42.aspx
Declare either of them it as follows:public SortedList<int, Antenna> antennas;
for each (KeyValuePair<int, Antenna^> kvp in rigging->antennas)
{Antenna^ antenna = kvp.Value;
CString antennaId;
antennaId.Format(_T("%d"), kvp.Key);(...)
}
- I'm using key/value pair collections because I want to be able to retrieve an element given its key. But it is possible to perform much more complex retrievals in a very elegant way. Anything that implements IEnumerable can be searched.
Friday, 1 December 2006
C# or C++/CLI?
- no cpp/h, just one file
- better IDE support:
- better intellisense
- code auto-complete
- refactoring (auto rename, method extraction, ...)
- code snippets
- compile errors displayed as you type
- no, it's not Microsoft proprietary, there is an ECMA standard for it.
- support for mixing managed and unmanaged code. Excellent for re-use of legacy code.
- cool language extensions (e.g. for each, ...)
- no, it's not Microsoft proprietary, there is an ECMA standard for it
Articles: