Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / C#

High Performance Multi-threaded Work Item / Event Scheduling Engine

Rate me:
Please Sign up or sign in to vote.
4.94/5 (57 votes)
16 Mar 2008CPOL16 min read 137.8K   2.4K   247  
High performance solution for scheduling and executing work items.
using System;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BrainTechLLC;
using BrainTechLLC.EmlenMud;
using BrainTechLLC.EmlenMud.Objects;
using BrainTechLLC.EmlenMud.Interfaces;
using BrainTechLLC.ThreadSafeObjects;

namespace Article1
{
    public class DemoWorkItem : IExecutableWorkItem 
    {
        private static long _workItemsExecuted;

        public DemoWorkItem(DateTime executionTime) { ExecutionTime = executionTime; }
        public DateTime ExecutionTime { get; set; }
        public bool Cancelled { get; set; }
        public void Execute()
        {
            long n = Interlocked.Increment(ref _workItemsExecuted);
            long nMod5 = n % 5;
            switch (nMod5)
            {
                case 0:
                    WorkItemResult = (n + 1000) / 3;
                    break;
                case 1:
                    WorkItemResult = new TestThing(Convert.ToInt32(n / 500000), nMod5.ToString());
                    break;
                case 2:
                    WorkItemResult = 12;
                    break;
                case 3:
                    WorkItemResult = "This is a test";
                    break;
                case 4:
                    WorkItemResult = "ABC";
                    break;
            }
        }
        public object WorkItemResult { get; set; }
    }

    public class TestThing
    {
        public TestThing(int testValue, string testStringValue)
        {
            TestValue = testValue;
            TestStringValue = testStringValue;
        }
        public int TestValue {get;set;}
        public string TestStringValue {get;set;}
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Troppus Software
United States United States
Currently working as a Senior Silverlight Developer with Troppus Software in Superior, CO. I enjoy statistics, programming, new technology, playing the cello, and reading codeproject articles. Smile | :)

Comments and Discussions