Thread

K

Kate Luu

Hi All,

First of all, My computer have only one comm port, and I did set up the port
as looping call back. I have one form with one button. When the form load, I
call WriteFile to open the port and then create a thread for reading back
the port if some thing arrive into the port. When I click the button, I
write the word: "Hello World" to the comm port. When data access the port,
the second thread was trigger. when the second thread read a first
character, its gave up its thread to its parent, it doesn't read the whole
thing as "Hello World". How can I prevent the second thread gave up its
thread to its parent until it's finish reading all the characters were came
to the port. Any help is greatly appreciated.

Kate Luu
 
N

Nicholas Paldino [.NET/C# MVP]

Kate,

I am not quite sure what you are trying to do, but in your callback, you
might want to check until you have read all of the information that came
through the port, caching it until you want to read it all. When you
finally know you have enough data, then you can continue processing.

Hope this helps.
 
K

Kate Luu

Thanks you so much for your help, I'm apprecated. I did have a loop to read
all the character, it is look like this:

do

{

gotbytes = 0;

if( !Win32Com.ReadFile( hPort, buf, 1, out gotbytes, unmanagedOv ) )

{

throw new CommPortException("IO Error [004]");

}

if( gotbytes == 1 )

OnRxChar( buf[0] );

} while( gotbytes > 0 );



But because this project support OVERLAPPED, mean Asynchronous, therefore
the thread just jump back and forth between window thread( parent thread )
and user thread. It must be able to control the parent thread some how but
I'm really don't know how to do it. Have a good one...

Kate Luu





Nicholas Paldino said:
Kate,

I am not quite sure what you are trying to do, but in your callback, you
might want to check until you have read all of the information that came
through the port, caching it until you want to read it all. When you
finally know you have enough data, then you can continue processing.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Kate Luu said:
Hi All,

First of all, My computer have only one comm port, and I did set up the port
as looping call back. I have one form with one button. When the form
load,
I
call WriteFile to open the port and then create a thread for reading back
the port if some thing arrive into the port. When I click the button, I
write the word: "Hello World" to the comm port. When data access the port,
the second thread was trigger. when the second thread read a first
character, its gave up its thread to its parent, it doesn't read the whole
thing as "Hello World". How can I prevent the second thread gave up its
thread to its parent until it's finish reading all the characters were came
to the port. Any help is greatly appreciated.

Kate Luu
 

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

Similar Threads


Top