Tech Off Thread

6 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Action to Dispatch Method?

Back to Forum: Tech Off
  • User profile image
    phreaks

    I'm trying to design a class that can be used to Dispatch methods off to another thread.

    I want to use the new Action delegates, but am seeming to have a difficult time wrapping my head around it.

     

    Anyone want to give me a quick lerning...

    
    public class Mock
        {
            public Object FakeCount()
            {
                return 10;
            }
        }
    
    public class ThreadControlDispatch
        {
            delegate void Dispatch();
            private static Object _obj;
            public static void DispatchObject(Object obj)
            {
                _obj = obj;
            }
            public static Object Execute(Action action)
            {
                Dispatch dispatcher = new Dispatch(action);
                return action.DynamicInvoke(null);
            }
        }
    
       private void Form1_Load(object sender, EventArgs e)
            {
                Mock m = new Mock();
                ThreadControlDispatch.DispatchObject(m);
                ThreadControlDispatch.Execute(new Action(m.FakeCount));
            }
     
    

     

     

    ERROR:  Mock.FakeCount() has the wrong return type.

     

    I am totally missing something here...

  • User profile image
    spivonious

    Actions don't have return values?

     

    public delegate void Action<in T>(
    T obj
    )

  • User profile image
    phreaks

    spivonious said:

    Actions don't have return values?

     

    public delegate void Action<in T>(
    T obj
    )

    I see. So Actions aren't what I want to be using... I was hoping to do this more elagantly than resorting to reflection...

  • User profile image
    phreaks

    phreaks said:
    spivonious said:
    *snip*

    I see. So Actions aren't what I want to be using... I was hoping to do this more elagantly than resorting to reflection...

    This works as expected but forces me to refactor class methods out to be property centric for returning values. Not sure if that smells right...

     

     public class Mock
        {
            public void FakeCount()
            {
                Result = 10;
            }
            public Object Result;
        }
     
     
        public class ThreadControlDispatch
        {
            delegate void Dispatch();
            private static Object _obj;
            private static Object _result;
            public static Object Result
            {
                get{ return _result; }
            }
            public static void DispatchObject(Object obj)
            {
                _obj = obj;
            }
            public static void Execute(Action action)
            {
                Dispatch dispatcher = new Dispatch(action);
                _result = action.DynamicInvoke(null);
            }
        }
     
     //implementation
      private void Form1_Load(object sender, EventArgs e)
            {
                Mock m = new Mock();
                ThreadControlDispatch.DispatchObject(m);
                ThreadControlDispatch.Execute(m.FakeCount);
                MessageBox.Show(m.Result.ToString());
            }
    

  • User profile image
    Sven Groot

    Why does the delegate have to be Action? Why not use Func<object>, or create your own delegate type?

  • User profile image
    phreaks

    Sven Groot said:

    Why does the delegate have to be Action? Why not use Func<object>, or create your own delegate type?

    I ended up just using my own delegate types.

    I'm not sure why I was trying to force the use of an action. Shiney things distract me.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.