Hello I have packed my standard code in a class dll. I am calling this dll from my C# service apps. But in the dll,at one point, there should be called a method with fixed name "func_X" that is not standard and has to be defined by the dll caller app. How can I realise this?
The challanging point is that the func_X is not called at a fix point in my dll. According to the flow, it is called at a different point.
My service where I call the dll
using Concheetah_Class; // my dll
namespace Concheetah_Service_Bahmuller
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
Concheetah_Class.Main_Prog main_Prog = new Concheetah_Class.Main_Prog();
main_Prog.Main_Start(); // starting point of my dll
}
public void func_X()
{
// some supplementary code
}
}
}
My dll code
public void Main_Start()
{
// some long code
func_X(); // Here I should call the method that has to be defined on the caller side
// some long code
}
Update-1 My dll code
System.Timers.Timer timer1 = new System.Timers.Timer();
public void Main_Start()
{
Initialize_timer1();
}
public void Initialize_timer1()
{
timer1.Elapsed += new ElapsedEventHandler(OnTimedEvent_timer1);
timer1 = 35;
timer1.Start();
}
private void OnTimedEvent_timer1(object sender, EventArgs e)
{
//some code
func_x();
}
You will to need pass the function to your dll program.
Update according to your latest edit:
Approach 1: You can pass your function to this constructor of Main_Prog
and store it in a variable.
public class Main_Prog
{
System.Timers.Timer timer1 = new System.Timers.Timer();
Action func_x;
public Main_Prog(Action func_x)
{
this.func_x = func_x;
Initialize_timer1();
}
public void Initialize_timer1()
{
timer1.Elapsed += new ElapsedEventHandler(OnTimedEvent_timer1);
timer1.Interval = 35;
timer1.Start();
}
private void OnTimedEvent_timer1(object sender, EventArgs e)
{
this.func_x();
}
}
Approach 2: Instead of storing it globally pass the function to OnTimedEvent
:
public class Main_Prog
{
System.Timers.Timer timer1 = new System.Timers.Timer();
public Main_Prog(Action func_x)
{
Initialize_timer1(func_x);
}
public void Initialize_timer1(Action func_x)
{
timer1.Elapsed += (sender, args) => OnTimedEvent_timer1(sender, args, func_x);
timer1.Interval = 35;
timer1.Start();
}
private void OnTimedEvent_timer1(object sender, EventArgs e, Action func_x)
{
func_x();
}
}
In your Service1
pass func_x
as an argument.
protected override void OnStart(string[] args)
{
Concheetah_Class.Main_Prog main_Prog = new Concheetah_Class.Main_Prog();
main_Prog.Main_Start(func_X);
}
In your Main_Prog
receive it as an Action.
public void Main_Start(Action func_X)
{
func_X();
}
Depending on your need you can switch between Func & Action. Action is used when the return type is void and Func is used when return type is not void.