Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Assume I have a stream of records that need to have some computation. Records will have a combination of these functions run Sum, Aggregate, Sum over the last 90 seconds, or ignore.

A data record looks like this:

Date;Data;ID

Question

Assuming that ID is an int of some kind, and that int corresponds to a matrix of some delegates to run, how should I use C# to dynamically build that launch map?

I'm sure this idea exists... it is used in Windows Forms which has many delegates/events, most of which will never actually be invoked in a real application.

The sample below includes a few delegates I want to run (sum, count, and print) but I don't know how to make the quantity of delegates fire based on the source data. (say print the evens, and sum the odds in this sample)

using System;
using System.Threading;
using System.Collections.Generic;
internal static class TestThreadpool
{

    delegate int TestDelegate(int  parameter);

    private static void Main()
    {
        try
        {
            // this approach works is void is returned.
            //ThreadPool.QueueUserWorkItem(new WaitCallback(PrintOut), "Hello");

            int c = 0;
            int w = 0;
            ThreadPool.GetMaxThreads(out w, out c);
            bool rrr =ThreadPool.SetMinThreads(w, c);
            Console.WriteLine(rrr);

            // perhaps the above needs time to set up6
            Thread.Sleep(1000);

            DateTime ttt = DateTime.UtcNow;
            TestDelegate d = new TestDelegate(PrintOut);

            List<IAsyncResult> arDict = new List<IAsyncResult>();

            int count = 1000000;

            for (int i = 0; i < count; i++)
            {
                IAsyncResult ar = d.BeginInvoke(i, new AsyncCallback(Callback), d);
                arDict.Add(ar);
            }

            for (int i = 0; i < count; i++)
            {
                int result = d.EndInvoke(arDict[i]);
            }


            // Give the callback time to execute - otherwise the app
            // may terminate before it is called
            //Thread.Sleep(1000);

            var res = DateTime.UtcNow - ttt;
            Console.WriteLine("Main program done----- Total time --> " + res.TotalMilliseconds);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }

        Console.ReadKey(true);
    }


    static int PrintOut(int parameter)
    {
        // Console.WriteLine(Thread.CurrentThread.ManagedThreadId + " Delegate PRINTOUT waited and printed this:"+parameter);
        var tmp = parameter * parameter;
        return tmp;
    }

    static int Sum(int parameter)
    {
        Thread.Sleep(5000); // Pretend to do some math... maybe save a summary to disk on a separate thread
        return parameter;
    }

    static int Count(int parameter)
    {
        Thread.Sleep(5000); // Pretend to do some math... maybe save a summary to disk on a separate thread
        return parameter;
    }

    static void Callback(IAsyncResult ar)
    {
        TestDelegate d = (TestDelegate)ar.AsyncState;
       //Console.WriteLine("Callback is delayed and returned") ;//d.EndInvoke(ar));
    }

}
share|improve this question
    
Not related to you question, but why are you calling ThreadPool.SetMinThreads()? I think you should have a very good reason to do that. And of course, calling Thread.Sleep(1000) doesn't make sense at all. Also, you should use better variable names than rrr and ttt. –  svick May 10 '12 at 12:46
add comment

1 Answer

up vote 2 down vote accepted

Assuming that ID is an int of some kind, and that int corresponds to a matrix of some delegates to run, how should I use C# to dynamically build that launch map?

How is that dynamic? It sounds as though you simply want a Dictionary<int,Func<int,int>> where the ID is the key and the delegate is the value. Simple function composition (or perhaps just taking advantage of the multicast nature of C# delegates) will handle scenarios where you need multiple effects.

share|improve this answer
    
I haven't used Func<T,T> yet... I suppose this is my time to learn. Thanks I'll look at it! –  makerofthings7 May 10 '12 at 13:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.