I have an application which works on a 10 ms cycle (every 10 ms all the functions are run so it almost behave like an embedded controller). There is a function in my code which is more expensive (in terms of execution time) than I want. This function is called sendECSResponce (This send a string to a Client via TCP/IP, I have not included the code). To get my application light I decoupled this function and put it in a different thread called sendMessage. I made a function which will throw an event called (this has been referred from an article, below is the code)
public class EventThrower
{
public delegate void EventHandler(object sender, EventArgs args);
public event EventHandler ThrowEvent = delegate { };
public void SomethingHappened()
{
ThrowEvent(this, new EventArgs());
}
}
In access the event where I need (below is an example)
public class SomeOtherClass
{
private ATCom.Classes.EventThrower _Thrower = new ATCom.Classes.EventThrower();
public void SendMessagetoECS()
{
string msg = Globals.gMessageForECS;
_Thrower.ThrowEvent += (sender, args) => { sendECSResponce(msg); };
}
public void localControl()
{
Globals.gAllocRSPCode = AGV_Rsp_Code.C_ARC_OTHER_CONTROL;
ECSMessage lclCntrl = new ECSMessage();
lclCntrl.ID = MessageID.AT_LOCAL_CONTROL;
lclCntrl.length = 0;
_Thrower.EventThrower();
}
}
In localControl() I throw an event to send the message via TCP/IP (sendECSResponce(msg)). Now I start a Thread and assign the function to the Thread in my main function.
private void myMain()
{
System.Threading.Thread mainThread;
System.Threading.Thread sendMessage;
mainThread = new System.Threading.Thread(objectOfSomeClass.mainFunction);
sendMessage = new System.Threading.Thread(objectOfSomeOtherClass.SendMessagetoECS);
mainThread.Start();
sendMessage.Start();
}
Please help with this.
What I have tried:
Please refer the question for my try. It gives a clear (hopefully) picture of my intentions.