Open Com port failed (multi threading)

D

Droopy

Hi,

I wrote a C# program that is calling C++ legacy code to open a serial port
(using CreateFile (comPort,
GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL))
..
So far, it works.
Now, at startup, I start 4 C# threads.
These threads are opening Com ports #2, #3, #4 and #5.
Sometimes, these 4 Com ports are opened successfully but, most of the time,
1 Com port opening failed.
The GetLastError returns 5 (access denied).

So, my question is :

Is it safe to call 4 CreateFile (for different Com ports of course) in
parallel thread at the same time ?

Thanks in advance for your help,

Droopy.
 
D

Droopy

Hi,

I wrote a C# program that is calling C++ legacy code to open a serial
port (using CreateFile (comPort,
GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NU
LL)) .
So far, it works.
Now, at startup, I start 4 C# threads.
These threads are opening Com ports #2, #3, #4 and #5.
Sometimes, these 4 Com ports are opened successfully but, most of the
time, 1 Com port opening failed.
The GetLastError returns 5 (access denied).

So, my question is :

Is it safe to call 4 CreateFile (for different Com ports of course) in
parallel thread at the same time ?

Thanks in advance for your help,

Droopy.

Who can help me ?
 
W

Willy Denoyette [MVP]

Yes it is. The "Access denied" error code has IMO nothing to do with the
number of threads, are you sure this error is returned by the CreateFile
call?

Willy.
 
D

Droopy

Yes.
To be more precise, CreateFile returns INVALID_HANDLE_VALUE and
GetLastError () returns 5 (Access Denied)

Sorry for this late answer.
 
D

Droopy

Who can help me ?
Have I to start it sequentially or add any timeout between thread
starting ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top