Pages

Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Sunday, 29 June 2014

Overview Of Asynchronous Tools in C# and C++

Asynchronous programming is satisfying, it feels good to write code that doesn't block.

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

How I used Adapter, Singleton and Dependency Injection (in real life, without the dry diagrams).


Monday, 12 May 2014

Java vs C++ vs C#: What Is The Best Programming Language?

From Akihabara in Tokyo how to choose a programming language?

Tuesday, 11 September 2012

Using Reactive Extensions with Bloomberg

 

2012-09-17 15.44.26Lately 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:

Thursday, 16 August 2012

More about Windows Services…

Following up from a previous blog post:

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

Misc

Monday, 1 December 2008

Get time in HH:mm format regardless of the local culture

DateTime.Now.ToShortTimeString() returns a string that depends on the culture. DateTime.Now.ToString("HH:mm") is deterministic. Use DateTime.Now.ToUniversalTime.ToString("HH:mm") to get the current HH:mm time in UTC.

Sunday, 11 May 2008

Things missing from C++

I'm using unmanaged C++ for a new project at work. For a while we hesitated between C++ and C# and eventually went for unmanaged C++ because of legacy libraries. I miss some of the C# features such as:
  • 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;
To iterate through it:
        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?

Advantages of C#
  • 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.
Advantages of C++/CLI
  • 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: