Serial port creates threads until memory is exhausted

A

Anton

Hi,

I am new to C# + netcf 2.0sp2 + WinCE 5 and do not understand the problem:

- I use a SerialPort object which reads data with 9600 baud
- on the serial line, the data is arriving continuously
- I have a gui app with a normal timer with 20 ms interval
- the OnTimer function does:
- ask how many bytes are in the receice queue (BytesToRead)
- read all these bytes

What I see:
- In my Visual Studio 2005 SP1 (using ActiveSync over USB) in
the Output window, I can
see that threads are created and exited .. non-stop
(this doesnt come from the timer since no threads are created if
I do not open the serialport)

- after a while the wince device is out of memory :-(

- I did a test: if nothing is incoming on the serial line no threads
are created

It seems that the SerialPort object creates threads for every incoming byte
or whatever.
I increased the Threshold for readind bytes to 10000 but with no effect
(as you see I do a simple polling in the timer).

The same code works on Windows XP without creating thousands of threads.
I remember having read somewhere that WinCE and WinXP handle serial ports
differently (WinCE doesnt have overlapped IO or whatever this means ...).

Can anybody point me out a C# example which works?

Googeling around didn't help until now.

Otherwise I will need to create a C++ dll doing
the stuff, and calling the functions by P/Invoce.

Thanks,

Anton
 
G

Guest

Why are you polling in the first place? That's what events are for. You
also don't give us much to go on on how you're reading (or even what kind of
Timer you're using). I know several people using the CF-supplied serial
port classes successfully, so I can only assume they're fine based on that
and that there's something you've got wrong in your code - perhaps a leak.
I doubt the Threads being created have anything to do with the OOM.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded World
www.OpenNETCF.com
 
A

anton

Hi Chris,

I just made a small test:
- I use the SerialPort class from Net Compact Framework 2.0 SP2:

- I have a form application for wince5 wich only one checkbox to open/
close the
serial communication as you see:

private void checkBoxOpenComm_CheckStateChanged(object sender,
EventArgs e)
{
if (checkBoxOpenComm.Checked)
{
serialPort1.Open();
timer1.Enabled = true;
}
else
{
timer1.Enabled = false;
serialPort1.Close();
}
}

private void timer1_Tick(object sender, EventArgs e)
{
int test1 = serialPort1.BytesToRead;
serialPort1.DiscardInBuffer();
int test2 = serialPort1.BytesToRead;
int testdiff = test2 - test1;
}
- Now I use the ".NET Compact Framework Remote Performance Monitor "
which is here since SP1
in C:\Program FIles\Microsoft.NET\SDK\CompactFramework\v2.0\bin
\NetCFRPM.exe

- I do *nothing* else than skipping the bytes in the input buffer
every 100 ms

- But as soon as I start the app:
- In Visual Studio 2005 debug mode I see a bunch of threads which
terminate
- in the ".NET Compact Framework Remote Performance Monitor "
I see the following entries increasing fast:

- Work Items Queued (22000 after 290 s)

- Scheduled Timers (22000 after 290 s)

- Managed Bytes Allocated gets > 5 MB after 420 sec


I am just waiting to see if it hangs now again...

After this I will try the OpenNETCF (I used it in the past
for serial communication with NETCF 1.0 but since NETCF 2.0
has its own serial comm class I didnt use it anymore)

Anton
 
G

Guest

Hello Anton:

Just a quick note ... in the desktop world of .NET v2.0 a Timer object (at
least if it's from the System.Threading or System.Threading.Timer namespace)
uses threads from the TreadPool to perform its work. Those threads are
"returned" once the call completes instead of the thread being disposed of.

The Timer object in CF v2.0 actually runs on the thread that created it ....
usally the GUI thread if it's your typical Forms application on a mobile
device (this is why you usually have to take long running operations and have
them run from a thread from the ThreadPool, even though you used the Tick
event handler from the GUI thread to "kick things off").

If you could provide some larger code sample or additional details I might
be able to offer some thoughts on why you are burning through your resources.
 
G

guru

Hi,

I am new to C# + netcf 2.0sp2 + WinCE 5 and do not understand the problem:

- I use a SerialPort object which reads data with 9600 baud
- on the serial line, the data is arriving continuously
- I have a gui app with a normal timer with 20 ms interval
- the OnTimer function does:
- ask how many bytes are in the receice queue (BytesToRead)
- read all these bytes

What I see:
- In my Visual Studio 2005 SP1 (using ActiveSync over USB) in
the Output window, I can
see that threads are created and exited .. non-stop
(this doesnt come from the timer since no threads are created if
I do not open the serialport)

- after a while the wince device is out of memory :-(

- I did a test: if nothing is incoming on the serial line no threads
are created

It seems that the SerialPort object creates threads for every incoming byte
or whatever.
I increased the Threshold for readind bytes to 10000 but with no effect
(as you see I do a simple polling in the timer).

The same code works on Windows XP without creating thousands of threads.
I remember having read somewhere that WinCE and WinXP handle serial ports
differently (WinCE doesnt have overlapped IO or whatever this means ...).

Can anybody point me out a C# example which works?

Googeling around didn't help until now.

Otherwise I will need to create a C++ dll doing
the stuff, and calling the functions by P/Invoce.

Thanks,

Anton

Anton,

You can try OpenNetCF Serial port library. I did have some kind of
problem with Serial port data exchange.

Mail back if you need more help...

regards
Venkat
 

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