Parallel Processing Routine C #

4

I would like to start a data-handling method whenever I receive a String with START content through the Transceiver.ReceiveLineEventHandler () event; however this method will have a processing time because there will be timeout among other resource.  The problem is that while doing this processing, I can not stop the program, because the event can receive another string that must be processed by other methods implemented, in addition there will always be an address of the receiver contained in the string, and there are cases where it will be necessary to process the string received by the same SendAndWaitForResponse () method and use the received address to destine the data, however this may already be used by a START received previously from another address, can anyone help me with such problem? Any light?

I was able to use the SendAndWaitForResponse () method on a separate Thread, so I did not stop the whole program. However there will be cases where I need to execute the Method to handle other information coming from another address simultaneously

    Transceiver.ReceiveLine += new Transceiver.ReceiveLineEventHandler(ProcessReceived);
    public string address = "";

    void ProcessReceived(string line)
    {   

        string[] dados = line.Split('|');
        address = dados[1];
        if (dados[0] == "RECOVERY"){...}
        if (dados[0] == "COMMANDMODE"){...}
        else if (dados[0] == "START"){
        SendAndWaitForResponse(address,line);
    }

    public void SendAndWaitForResponse(string endereco, string line)
    {
     SendMessage(endereco,line);
         ...
    }
    
asked by anonymous 25.07.2016 / 20:35

1 answer

1

You can make an asynchronous call in EventHandler.:

Transceiver.ReceiveLine += new Transceiver.ReceiveLineEventHandler(ProcessReceived);
public string address = "";

async void ProcessReceived(string line)
{   
    string[] dados = line.Split('|');
    address = dados[1];
    if (dados[0] == "RECOVERY"){...}
    if (dados[0] == "COMMANDMODE"){...}
    else if (dados[0] == "START"){
    await Task.Run(() => {
        SendAndWaitForResponse(address,line);
    });
}

public void SendAndWaitForResponse(string endereco, string line)
{
    SendMessage(endereco,line);
    ...
}
    
26.07.2016 / 15:14