none
Thread/timer with dynamic function with variable number and type of parameters RRS feed

  • Question

  • I need to create ThReads/Timers but in a function to standardize. ThReadFunction may have variable number and type of Parameters. Same with Elapsed and timer. Thank you

    System.Threading.Thread Fcn_ThReadNew(dynamic ThReadFuncion)
    { System.Threading.Thread MyThRead = new System.Threading.Thread(ThReadFuncion);
    return MyThRead;
    }

    private static void Fcn_TimerElapsed(dynamic MyElapsed)
    { System.Timers.Timer MyTimer = new System.Timers.Timer();
    MyTimer.Elapsed += MyElapsed;
    }
    Monday, January 14, 2019 6:35 AM

All replies

  • Consider Action:

       private static void Fcn_TimerElapsed(Action MyElapsed)

       {

          System.Timers.Timer MyTimer = new System.Timers.Timer();

          MyTimer.Elapsed += (s,a) => MyElapsed();

    }

    When you call it, use a short lambda expression that invokes your function with arguments, for example:

       Fcn_TimerElapsed( ( ) => MyFunction(a, b, c, d) );

     

    Monday, January 14, 2019 8:58 AM
  • But what I want is to centralize the execution of the timer / thread in one place and the call function (ThReadFuncion / MyElapsed) is always different and with different number of arguments.
    • Edited by zequion1 Monday, January 14, 2019 9:29 AM
    Monday, January 14, 2019 9:28 AM
  • You didn't really provide us a realistic example of exactly what problem you're trying to solve. You're going to get any # of solutions that may or may not help you. Please provide some real examples of what you're trying to do to speed this discussion up.

    If you need to pass differing sets of parameters to "different" functions that change over time such that an Action wouldn't work then you're going to have to go with a class. Create an abstract base type (or perhaps interface) that defines the logic you need to call from the timer. Create derived types to represent each unique variant you need. The "data" for the functionality are properties on the type. Then have the timer use the abstract base type as an input. Create instances of your derived types based upon what you need and then "register" them with the timer.

    abstract class BaseType
    {
       public abstract void DoWork ();
    }
    
    class ReadFile : BaseType
    {
       public string FileName { get; set; }
    
       public override void DoWork ()
       {
          //Read the file
       }
    }
    
    class SendEmail : BaseType
    {
       public string Email { get; set; }
       
       public override void DoWork ()
       {
          //Send an email
       }
    }
    
    //Create the "functionality" you need and add to some list that the timer code is using (note: not thread safe)
    _tasks.Add(new ReadFile() { FileName = "temp.txt" });
    _tasks.Add(new SendEmail() { Email = "bob@gmail.com" });
    
    //Inside timer event
    foreach (var task in _tasks)
       task.DoWork();
    

    If your functionality is so dramatically different that no "type" would work then you may do better just raising an event and letting the disconnected pieces of your app listen for the event instead.


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, January 14, 2019 3:11 PM
    Moderator
  • You can use different functions and arguments:

       Fcn_TimerElapsed( ( ) => MyFunction1(a, b, c, d) );

       Fcn_TimerElapsed( ( ) => MyFunction2(e, f) );

       Fcn_TimerElapsed( MyFunction3 );

     


    • Edited by Viorel_MVP Monday, January 14, 2019 3:13 PM
    Monday, January 14, 2019 3:13 PM
  • What I want is that all of Thread's creations go through a central function in order to better manage the opening of threads instead of having them scattered throughout the program. The same with Timer and Elapsed.

    The functions can have any number of arguments. The answers do not seem to have resolved.
    Monday, January 14, 2019 3:28 PM
  • "What I want is that all of Thread's creations go through a central function in order to better manage the opening of threads"

    Why? If you want a thread manager then create a class to manage the creation of threads and use it instead of creating threads explicitly. This class would accept the Action to call (which could accept anything and do anything).

    Note that if you want to control the # of threads or their context then you may do better switching to tasks and using a custom TaskScheduler.


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, January 14, 2019 3:40 PM
    Moderator
  • Do you show us?
    Tuesday, January 15, 2019 5:59 AM
  • Show you what - thread manager class or using a task scheduler?

    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, January 15, 2019 2:49 PM
    Moderator