用于时间关键应用程序的事件和多线程 [英] Events and multithreading for a time critical application

查看:86
本文介绍了用于时间关键应用程序的事件和多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序可以在10毫秒的周期内工作(每10毫秒运行一次所有的功能,所以它几乎像嵌入式控制器一样)。我的代码中有一个函数比我想要的更昂贵(就执行时间而言)。这个函数叫做sendECSResponce(这个通过TCP / IP向客户端发送一个字符串,我没有包含代码)。为了让我的应用程序亮起,我解耦了这个函数并将它放在一个名为sendMessage的不同线程中。我做了一个函数,它会抛出一个叫做的事件(这个是从文章中引用的,下面是代码)



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 = 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();
    }
}



在localControl()中,我抛出一个事件来通过TCP / IP发送消息(sendECSResponce(msg))。现在我启动一个Thread并将函数分配给我的main函数中的Thread。


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.

推荐答案

很难理解该代码...



您在 SomethingHappened 中举起活动,但该方法在任何地方都没有被调用。



看起来你正试图显式调用 EventThrower 构造函数方法(在注释发送消息之后)。我很惊讶这段代码甚至可以编译!



在函数 SendMessagetoECS 中你是非常可疑的,你附加事件处理程序(而不是引发事件)。良好的编程习惯是根据他们的工作来命名方法。这里要么命名方法,要么代码不能从函数名称做什么。



另外,为什么在你可以使用时构造一个空事件args code> EventArgs.Empty 。



在你的函数 myMain 中,为什么在初始化之前声明变量。这是一个非常坏的习惯(主要来自C语言)。



手动附加事件时也要小心......你必须确保保持它们是正确的连接。有时,您必须分离事件,以确保您没有内存循环,以防止垃圾收集器回收内存,或者确保在目标可以被首先销毁时不再引发事件。



顺便说一下,遵循更一致的命名约定并不会有什么坏处。例如,您有一个以大写字母开头而另一个以小写字母开头的公共方法。通常,在C#中编写代码时应遵循.NET约定。还有像Thrower这样的名字被严格挑选。一个可能是函数的意图是如何抛出一些异常,而实际上它会向事件处理程序发出通知。
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.


这篇关于用于时间关键应用程序的事件和多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆