tags:

views:

113

answers:

3

How do i pass method to asynchronous delegate invocation, so that it could be explicitly processed asynchronously on second processor? Thanks in advance.

ADDED: I had problem with encoding internet so sorry for upper case. I have tried:

BTW Have no javascript enabled in browser so I can't add comments to this post. Will be editing this post.

using System;
using System.Threading;
using System.Runtime.Remoting.Messaging;

namespace Delegate
{
    public class SampleClass
    {
        public static bool SampleMethod()
        {
            Console.WriteLine("Inside sample method ...");
            return true;
        }

        public static void SampleThreadMethod()
        {
            while (true)
            {
                Console.WriteLine("Inside sample thread method ...");
                Thread.Sleep(500);
            }
        }
    }

    public delegate bool SampleMethodCaller();

    public class DelegateSample
    {
        ManualResetEvent waiter;

        public void CallBackMethodForDelegate(IAsyncResult result)
        {
            SampleMethodCaller smd = (SampleMethodCaller)((AsyncResult)result).AsyncDelegate;
            bool returnValue = smd.EndInvoke(result);
            Console.WriteLine("Callback Result:- {0}", returnValue);
            waiter.Set();
        }

        public void CallDelegateAndWait()
        {
            Console.WriteLine("Sample 1: Calling delegate using BeginInvoke and waiting for it to complete");
            SampleMethodCaller smd = new SampleMethodCaller(SampleClass.SampleMethod);
            IAsyncResult result = smd.BeginInvoke(null, null);
            Console.WriteLine("After invoking delegate no callback...");
            bool returnValue = false;
            returnValue = smd.EndInvoke(result);
            Console.WriteLine("Wait Result:- {0}", returnValue);
        }

        public void CallDelegateUsingCallBack()
        {
            Console.WriteLine("Sample 2: Calling delegate using BeginInvoke and waiting on call back");
            waiter = new ManualResetEvent(false);
            SampleMethodCaller smd = new SampleMethodCaller(SampleClass.SampleMethod);
            IAsyncResult result = smd.BeginInvoke(CallBackMethodForDelegate, null);
            Console.WriteLine("After invoking delegate in callback...");
            waiter.WaitOne();
        }

        public void CallDelegateUsingInvoke()
        {
            Console.WriteLine("Sample 3: Calling delegate using Invoke");
            SampleMethodCaller smd = new SampleMethodCaller(SampleClass.SampleMethod);
            bool result = smd.Invoke();
            Console.WriteLine("Invoke call Result:- {0}", result);
        }

        public void CallDelegate()
        {
            Console.WriteLine("Sample 4: Calling delegate straight forward");
            SampleMethodCaller smd = new SampleMethodCaller(SampleClass.SampleMethod);
            bool result = smd();
            Console.WriteLine("Simple call Result:- {0}", result);
        }

        public void CallDelegateDynamicInvoke()
        {
            Console.WriteLine("Sample 5: Calling delegate using DynamicInvoke");
            SampleMethodCaller smd = new SampleMethodCaller(SampleClass.SampleMethod);
            bool result = (bool)smd.DynamicInvoke();
            Console.WriteLine("Dynamic Invoke call Result:- {0}", result);
        }

        public void CallingDelegateOnAThread()
        {
            Console.WriteLine("Sample 6: Calling delegate on a thread");
            Thread nt = new Thread(SampleClass.SampleThreadMethod);
            nt.Start();
            Thread.Sleep(2000);
            Console.ReadLine();
            nt.Abort();
        }

        public static void Main()
        {
            DelegateSample ds = new DelegateSample();
            ds.CallDelegateAndWait();
            Console.WriteLine(" -----------------");
            ds.CallDelegateUsingCallBack();
            Console.WriteLine(" -----------------");
            ds.CallDelegateUsingInvoke();
            Console.WriteLine(" -----------------");
            ds.CallDelegate();
            Console.WriteLine(" -----------------");
            ds.CallDelegateDynamicInvoke();
            Console.WriteLine(" -----------------");
            ds.CallingDelegateOnAThread();
        }
    }
}
+2  A: 

What are you trying to do that you can't do with

new Thread(delegateMethod).Start();

?

Jader Dias
or ThreadPool.QueueUserWorkItem.
Paul
or `delegateMethod.BeginInvoke(...)`.
Allon Guralnek
A: 

How do i pass method to asynchronous delegate invocation, so that it could be explicitly processed asynchronously on second processor?

I am not sure what you mean by "pass method to asynchronous delegate invocation" so I cannot comment on how you could accomplish whatever it is you are after here. You will need to update your question to make it a little less ambiguous.

You cannot change the processor affinity of a thread, at least not easily. There are no mechanisms in the BCL that allow you to assign a Thread to a specific processor. I suspect there never will be either since a Thread does not necessarily have a one-to-one relationship with a native OS thread. I am not willing to make a blanket statement that it should never be necessary to do that, but I strongly suspect that doing so will not lead to the desired result anyway.

You can, however, change the affinity on a process and the individual threads in the process. But, the ability provided through the Process class is not very granular and I think you are going to have a hard time mapping the items in Process.Threads collection to an actual CLR thread. Nevermind the fact that I would be highly dubious of this strategy to begin with.

Brian Gideon
A: 

I think what you are trying to do is the following:

    public delegate String SomeMethod(Int32 param);
    public String MethodImplementation(Int32 param) {
        return param.ToString();
    }
    public void InvokingMethod() {
        SomeMethod del = new SomeMethod(MethodImplementation);
        del.BeginInvoke(4, new AsyncCallback(CallbackMethod), del);
    }
    private void CallbackMethod(IAsyncResult ar) {
        SomeMethod del = ar.AsyncState as SomeMethod;
        String result = del.EndInvoke(ar);
        // do something with the result...
        return;
    }

Given a delegate that is pointing to a method, you can call the BeginInvoke method on the delegate to have the method call done on another thread. There is no way to specify what processor the thread the method call is run on will be on, nor in fact when the thread will run, so some sort of synchronization may be required depending on your requirements

Steve Ellinger