开发者

Generate an Custom event from DLL in C#

I am writting a DLL in C# using VS2008.

This DLL works as an interface between Application and Serial Port. Application sends data in Specific format (Or It follows a Standard Protocol) to this DLL. DLL converts it for Serial Transmission and Transmits over Serial Port. Same Application running on other PC (Means acting Like Client) and connected via Serial receives this data. Same DLL gets this data. Now it converts back in to Standard Protocol and place it开发者_C百科 in a buffe so that Application can read it.

There is no problem in Sending the data, But while receiving, I used Serial Data Received event in DLL so that it can get the data and Process it. My Problem is how can I indicate to Application that Data is processed and it is available for it and it can read the buffer?

Can I use SetEvents in DLL and can it be used to indicate Application using that DLL


You should create an event and fire it after you processed the data in Serial Data Received event. You can pass the data with custom event args if needed.

This is the code for your DLL:

public delegate void dllFinishedHandler(object sender, object tag);
    public event dllFinishedHandler DllFinished;
    protected virtual void OnDllFinished(object e)
    {
        if (DllFinished!= null)
            DllFinished(this, e);
    }

after implementing the code you should set the event handler in your application.

The code below passes an object to the app. You can either use SerialDataReceivedEventArgs or your custom Event Handler too it may look like this:

 public delegate void dllFinishedHandler(object sender, SerialDataReceivedEventArgs eventArgs);
    public event dllFinishedHandler DllFinished;
    protected virtual void OnDllFinished(SerialDataReceivedEventArgs e)
    {
        if (DllFinished!= null)
            DllFinished(this, e);
    }

of inherit from EventArgs and use it in the code below instead of SerialDataReceivedEventArgs:

public class DllEventArgs : EventArgs
{
    private byte[] buffer;

    public byte[] Buffer
    {
        get
        {
            return buffer;
        }
    }

    public DllEventArgs(byte[] buff)
    {
        buffer = buff;
    }
}

then your app can reach the buffer(i guess its byte[] directly from the event).


Do you know when data is processed. If yes,

if(Processed != null)
 Processed(this, new EventArgs());

Decalre event in the class as

public event EventHandler Processed

You can also pass custom event arguments and use generic EventHandler

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜