Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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)

C#
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)
C#
public class SomeOtherClass
{
    private ATCom.Classes.EventThrower _Thrower = new ATCom.Classes.EventThrower();
    public void SendMessagetoECS()
    {
        string msg = Globals.gMessageForECS;
        //_Thrower = new EventThrower();
        //using lambda expression..could use method like other answers on here
        _Thrower.ThrowEvent += (sender, args) => { sendECSResponce(msg); };
    }

    public void localControl()
    {
        // Decalre ECS message
        Globals.gAllocRSPCode = AGV_Rsp_Code.C_ARC_OTHER_CONTROL;
        ECSMessage lclCntrl = new ECSMessage();
        lclCntrl.ID = MessageID.AT_LOCAL_CONTROL;
        lclCntrl.length = 0;
        // EVENT: Sending Message 
        _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.
C#
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.
Posted
Updated 22-Feb-16 15:50pm
Comments
Sinisa Hajnal 22-Feb-16 2:50am    
Good description. Lacks one thing though. What is the question? What is the problem? Any errors? What happens when you execute the program?
Member 12112067 22-Feb-16 3:04am    
@sinisa, The code is compiled without Errors but the function sendECSResponse is not fired somehow. Even when the function localControl() is reached in the code.

1 solution

It is hard to make sense of that code...

You raise the event in SomethingHappened but that method is not called anywhere.

It look like you are trying to explicitly call EventThrower constructor method (just after the comment sending message). I'm surprise that this code would even compile!

It is very suspicious that in function SendMessagetoECS, you attach an event handler (and not raise an event). Good programming habit is to name method according to what they do. Here either the method is badly named or the code does not do what one would thing from function name.

Also, why constructing an empty event args when you can use EventArgs.Empty.

In your function myMain, why do you declare your variable before initializing them. This is a very bad habit (mainly from C language).

Also be careful when manually attaching events... You have to ensure that it is correct to keep them attached. Sometime, you have to detach events either to ensure that you don't have memory cycle that prevent memory to be reclaimed by the garbage collector or to ensure that event are not raised anymore if the target can be destroyed first.

By the way, it would not hurt to follow a more consistent naming convention. For example, you have a public method that start with an uppercase and another with a lowercase. Generally, you should follow .NET convention when writing code in C#. Also name like Thrower is badly chosen. One might thing that the intend of the function if to throw some exception while in reality it raise a notification to an event handler.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900