用于时间关键应用程序的事件和多线程

用于时间关键应用程序的事件和多线程

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

问题描述

我有一个应用程序可以在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.

推荐答案


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

08-13 01:44