title: How to call methods with different parameters as Action?
body: <p>I'm working on a game in Unity and Unity is not threadsafe so whenever I do some calculations on different thread and want to go back to Unity I need to queue calls in some kind of queue so Unity can take those tasks and execute them on Unity thread. This is my code with tool that kind of gets job done:</p>
  
  <pre><code>public class Test : MonoBehaviour
  {
      Thread someThread;
  
      private void Awake() //Executes on application start
      {
          someThread = new Thread(SomeThreadMethod);
          someThread.Start();
      }
  
      void SomeThreadMethod()
      {
          ExecuteInUpdate(MethodCalledNotInUnityThread);
      }
  
      // methods that I call from not unity thread but want to execute it IN unity thread
      void MethodCalledNotInUnityThread()
      {
          Debug.Log("Hello from UnityThread");
      }
  
      //--------------------------------------------------------------------------------
      //Tool for executing things in Unity Thread
  
      List&lt;Action&gt; actionQueues = new List&lt;Action&gt;();
      List&lt;Action&gt; actionCopiedQueue = new List&lt;Action&gt;();
  
      private volatile static bool noActionQueue = true;
  
      public void ExecuteInUpdate(Action _action)
      {
          lock (actionQueues)
          {
              actionQueues.Add(action);
              noActionQueue = false;
          }
      }
  
      public void Update()//runs all time, sort of while(true) loop in unity thread
      {
          if (noActionQueue)
          {
              return;
          }
  
          actionCopiedQueue.Clear();
  
          lock (actionQueues)
          {
              actionCopiedQueue.AddRange(actionQueues);
              actionQueues.Clear();
              noActionQueue = true;
          }
  
          for (int i = 0; i &lt; actionCopiedQueue.Count; i++)
          {
              actionCopiedQueue[i]();
          }
      }
  }
  </code></pre>
  
  <p>But the problem is it only works with methods that take no parameters. If I made a method like:</p>
  
  <pre><code>    void MethodCalledNotInUnityThread1(int _arg)
      {
          Debug.Log("Hello from UnityThread" + _arg);
      }
  </code></pre>
  
  <p>I could not call it cause it has parameter. I tried working with Action with generic parameter like</p>
  
  <pre><code>    Action&lt;int&gt; action
  </code></pre>
  
  <p>but then I can only pass methods that take only one parameter and it's int. I have many methods that take diffenet parameters in different amounts so how do I do it?</p>
  
  <p>I want to achieve something like that:</p>
  
  <pre><code>    Dictionary&lt;Action, object[]&gt; paramsForAction = new Dictionary&lt;Action, object[]&gt;();
  
      public void ExecuteInUpdate(Action _action, params object[] _args)
      {
          paramsForAction.Add(action, args);//save params for action
  
          //add action to queue
      }
  
      public void Update()
      {
          //find action to execute
  
          object[] args = paramsForAction[actionToExecute];//get params for action
          actionToExecute(args);
  
      }
  </code></pre>
  
  <p>Of course it doesn't work and makes no sense but hopefully you can get what I'm trying to do.</p>