Multithreading is still on my to-do list, so the title could be totally wrong :)

My object is listening to serial port, like this:

```
class MyClass
{
    MyOpticalScanner _scanner;

    public MyClass()
    {
        _scanner = new MyOpticalScanner();
        _scanner.CodeScanned += CodeScannedEventHandler;
    }

    private void CodeScannedEventHandler(object sender, CodeScannedEventArgs e)
    {
        Debug.WriteLine("ThreadID: " + Thread.CurrentThread.ManagedThreadId + " ||| " + e.ScannedCode);
        ....
        // Some code, query the database, etc...
    }
}

```

The scanner is sending ordered data: `001`, `002`, `003`, `004`, `005`, ... But if the code in the CodeScannedEventHandler takes too long to process, then another event is raised and I got inconsistent ordering. The `Debug.WriteLine` in event handler could give me this:

```
ThreadID: 8 ||| 001
ThreadID: 9 ||| 002
ThreadID: 10 ||| 003
ThreadID: 10 ||| 006
ThreadID: 8 ||| 004
ThreadID: 8 ||| 008
ThreadID: 8 ||| 009
ThreadID: 8 ||| 010
ThreadID: 10 ||| 007
ThreadID: 9 ||| 005

```

How can I assure, that each new event starts to process only when the old one is finished?

Edit 1 - I didn't told you everything, actualy I am not listening to COM port during the testing, instead I created my own mock object. This object (ScannerMock) uses an internal System.Timer and raises the CodeScanned event on Timer.OnTick event. Could the problem be here?

From the comment in [Hans Passant's answer](https://stackoverflow.com/questions/4946508/how-to-synchronize-event-handlers/4946754#4946754): *SerialPort has an internal lock that ensures that the DataReceived event cannot be called again while it is running.* Should I include similar lock in my mocking scanner and how?

Edit 2: I added a lock to my scanner object and put the code, that raises my event, into it. It looks like it works :)