• ThreadPool + Async methods + ManualResetEvent = Bad

    Okay, today it was re enforced that the ThreadPool is not good for long running tasks, specifically those that might use the ThreadPool or use async methods or ResetEvents. I didn’t really track down the root cause, but generally it locked while waiting for a set event. Regardless, the solution as is usually the case was to simplify the code. First by not using a ResetEvent and ThreadPooling for queuing the workload. In place of that a simple QueueRunner class was created using the Monitor class. Using the Monitor.Wait and Monitor.Pulse was new to me, but works well and seems to be very fast. There is a great article here about threading in c#. I’m not going to re-explain what already been said there, but below is the code. It could be enhanced for other uses and performance.

    [TestFixture]
    public class QueueRunnerTest
    {
    private int _count = 0;

    [Test]
    public void Test100ExpectSuccess()
    {
    Queue<int> queue = new Queue<int>();
    for (int i = 0; i < 100; i++)
    queue.Enqueue(i);

    _count = 0;

    QueueRunner<int> runner = new QueueRunner<int>(Work, queue);
    runner.RunAndWait();

    Assert.That(_count, Is.EqualTo(100));
    }

    private void Work(object obj)
    {
    Console.WriteLine("Working " + obj);
    Interlocked.Increment(ref _count);
    }
    }

    class QueueRunner<T>
    {
    private readonly object _syncLock = new object();
    private readonly Queue<T> _queue = new Queue<T>();
    private int _running = 0;
    private readonly int _maxThreads;
    private readonly ParameterizedThreadStart _workerDelegate;
    private int _toBeCompleted = 0;

    public QueueRunner(ParameterizedThreadStart workerDelegate, Queue<T> queue)
    : this(workerDelegate, queue, 5)
    {
    }

    public QueueRunner(ParameterizedThreadStart workerDelegate, Queue<T> queue, int maxThreads)
    {
    _workerDelegate = workerDelegate;
    _maxThreads = maxThreads;
    _queue = queue;
    _toBeCompleted = _queue.Count;
    }

    public void RunAndWait()
    {
    new Thread(Start).Start();
    Wait();
    }

    private void Start()
    {
    lock (_syncLock)
    while (_queue.Count > 0)
    {
    while (_running < _maxThreads)
    {
    new Thread(DelegateWrapper).Start(_queue.Dequeue());
    Interlocked.Increment(ref _running);
    }
    Monitor.Wait(_syncLock);
    }
    }

    private void DelegateWrapper(object state)
    {
    _workerDelegate(state);
    DecrementCount();
    }

    private void Wait()
    {
    lock (_syncLock)
    while (_toBeCompleted > 0)
    Monitor.Wait(_syncLock);
    }

    private void DecrementCount()
    {
    Interlocked.Decrement(ref _running);
    Interlocked.Decrement(ref _toBeCompleted);
    lock (_syncLock)
    Monitor.PulseAll(_syncLock);
    }
    }
    </p>

    Resources:
    Threading in C# - Joseph Albahari
    Delegate BeginInvoke and ManualResetEvent.WaitOne()

  • Deleting multiple objects with NHibernate using a query

    I needed to delete a bunch of objects based on some parameters of those objects. It fairly obvious that one of the Delete overloads on ISession would work, but a couple of things throw me. First you need a full select query, not too hard to figure out. But second, where are IType’s defined? In NHibernateUtil (thanks to this). Anyway, here is some sample code. I’m guessing there might be a more efficient way, but this should work for most cases.

    
    
    
    session.Delete(“select b from Bar as b where b.Instrument = :instrument and b.BarMinutes = :barMinutes”,
    new object[] {instrument, barMinutes},
    new IType[]
    {
    NHibernateUtil.Entity(typeof (Instrument)),
    NHibernateUtil.Enum(typeof (BarMinutes))
    });

  • Update on the AIM bot, new Amazon web services S3 Library

    </p>

    I’m still working on the AIM Bot, converting from boo to c#. I really liked working in boo, but just don’t feel like I have enough comfort in it to continue working in it. Anyway, I’ve decided to use the Amazon web service S3 for my data storage and have created an open source project, AwsS3Library, to connect to it.

    It’s pretty basic so far, listing buckets, and object, and getting, putting, and deleting buckets and objects. The usage isn’t too hard to follow, I’d suggest looking at the unit tests if your stuck. You will need to use your own AWS key and AWS Secret key.

    AwsBucket awsBucket = new AwsBucket("bucketName");
    AwsS3Service service = new AwsS3Service("AwsKey", "AwsSecret")
    awsBucket = service.Put(awsBucket);

    There is an intermediate issue with putting objects, which results in a timeout. I think it has to do with trying to put an object right after creating a new bucket. I’m not sure how or if this should be addressed by the library. I’m going to change the tests to create a bucket for the objects and one for testing the bucket functionality. I think that should help.

    It’s released under the BSD License to feel free to use and submit fixes.

  • Race between the If statement and the Delegate Swapping/Substitution Pattern

    Which is faster delegate swapping or simple if statements? I wanted to know too and it turns out it’s a tie, as far as I can see. The idea is that you might have some code you want to  execute until some condition occurs then execute some different code. This might be a run once thing or a run until X is true. Either way you could use an if statement. However, many times I prefer to use a delegate, I think it looks better and the code is easier to read. Anyway, I created a small program to test this and here are the results:races

    10000:10000 calls using Delegates averaging 0.005937576 seconds.
    10000:10000 calls using Ifs       averaging 0.005312568 seconds.
    5000:10000 calls using Delegates averaging 0.005156316 seconds.
    5000:10000 calls using Ifs       averaging 0.005156316 seconds.
    2:10000 calls using Delegates averaging 0.005000064 seconds.
    2:10000 calls using Ifs       averaging 0.005156316 seconds.
    1:10000 calls using Delegates averaging 0.005000064 seconds.
    1:10000 calls using Ifs       averaging 0.005156316 seconds.

    I declare a tie because different runs of the program yield slightly different results even though the run times are averaged over 100 runs. The first number is the number of times the TaskOne method is called, the second number is the total number of times the run method was called. Each set was run 100 times for the averaging seconds number.

    Pseudo code for the Ifs method:

    public void Ifs()
    {
    if (!TaskOneIsComplete())
    TaskOne();
    TaskTwo();
    }

    Pseudo code for the Delegates method:

    public void Delegates()
    {
    _executeMe();
    }

    private void InitialDelegate()
    {
    TaskOne();
    TaskTwo();

    if (TaskOneIsComplete())
    _executeMe = AfterDelegate;
    }

    private void AfterDelegate()
    {
    TaskTwo();
    }

    DelegateSubstitutionVSIfStatements.cs (2.96 KB)

  • Giving up on boo for the AIM bot due to incomplete Generics support.

    Well after getting an internal compiler error and trying to find out what the problems was, I decided to see if I could recreate the issue. In doing that, I wanted to know how to set constraints on generic types, and as it turns out this is still in progress as detailed in this post. Oh well, I guess having the AIM bot code in c# will readable by more people anyway. 

    I really enjoy working in boo and still have more of the language to explore including Syntactic Macros and Generators.

  • Using the Prototype Javascript Library to Fix my CSS Layout

    I made some tweaks to my blog layout specifically the columns and added a footer. The footer is where the problem started. With CSS using float for the side columns works great and the footer will fall below the main content as long as the main content column is taller then the floating columns. If not, the floating column will float over the footer. To fix this I used the prototype{#hm8i} javascript library to adjust the height of the main column if the floating column is taller. I added a MadKast{#e12e} size constant because the MadKast script will usually load after the height adjustment leaving the main content column short about 25 pixels. Anyway, here’s the code:

    <script language="javascript">
    <!--
    _madKastSize = 25;
    
    Event.observe(window, "load", function() {
    if ($('left') && $('left').offsetHeight > $('right').offsetHeight)
    $('right').setStyle({height:($('right').offsetTop + $('left').offsetHeight + _madKastSize) + "px"});
    });
    // --> </script>
    </p>
  • Building an AIM Bot in .Net using boo

    I’ve been working on a AIM Bot using the aimcc sdk. After signing up and downloading the sdk from developer.aim.com I found the documentation for .net lacking and found just getting up and running to be a long process. Below are some of the issues I ran into.

    **Application.Run()</p>

    </b>To start with I needed to create a class which extends Control. Then add a method to start the application, at the beginning of the method call CreateControl() at the end of the method you need to call Application.Run() to setup the message loop. I’m working in boo but this should work fine in c#.

    class Bot(Control):
    def Start():
    CreateControl()
    #AccSession setup to go here
    Application.Run()

    PInvoke

    Next pinvoke is required to creat the AccSession instance. This is in some of the SDK samples, but it wasn’t obvious looking at the java and c++ sample bots. It only calls acccore.dll, but other dll’s in the accsdk/dist/release/ are needed I’m just using the full path, but should copy the required dll’s to my project file.

    
    
    
        [DllImport(“C:\accsdk\dist\release\acccore.dll”, EntryPoint:“#111”, PreserveSig:false, SetLastError:true)] 
        private static def AccCreateSession(
        [MarshalAs(UnmanagedType.LPStruct)] riid as Guid,
        [MarshalAs(UnmanagedType.IDispatch)] ref session as object) as IntPtr:
            pass

    Calling the method is pretty strait forward, just pass in an object and then cast the object to a AccSession.


    CreateControl()
    o as object
    AccCreateSession(typeof(IAccSession).GUID, o)
    session = o as AccSession

    </pre>

    Login

    The next step was to wire up the events I wanted to get catch, I choose to wire up all the events for logging. You need to provide your API key and then login.


    handler = AccSessionEventHandler(BoxesImResponder())
    handler.WireSession(</em>session)
    session.ClientInfo.Property[AccClientInfoProp.AccClientInfoProp_Description] = key
    session.Identity = username
    session.SignOn(password)
    Application.Run()

    That’s finally all I needed to get the bot running. I fought with setting preferences for a long while like the java example shows, but I ended up following the c++ example to filter which messages get accepted. More to follow in later posts. In addition, I plan on posting the full source code when the application is working.

    </p>