Threading / DoEvents !?

  • Thread starter Thread starter Lasse Madsen
  • Start date Start date
L

Lasse Madsen

Hi all,

I have an application where i need to receive some data through the C#
seriel port component.

My receive event and uart class which holds the receive buffer etc. looks
like this:
private void seriel_DataReceived( object sender,
System.IO.Ports.SerialDataReceivedEventArgs e )

{

int number_of_bytes = seriel.BytesToRead;

seriel.Read (uart.buffer,uart.rx_wr_index,number_of_bytes);

uart.rx_wr_index += number_of_bytes;

}

class uart

{

static public byte[] buffer = new byte[4096];

static public int rx_wr_index=0;

}



In my application a timer looks at the buffer every 1mS and determines if we
have a "GO / NO GO" situration and asserts a flag correspondantly.

In my application i have a button ... if i press the button i transmit 118
packages of 10 bytes each to another computer over the serial port at 38400
baud ... every time a package is received by the other computer it sends a
"GO / NO GO" response telling me to either try again or proceed with the
next package.

The code could look like this where the go_no_go_flag is updated in the 1mS
timer looking at the buffer...

for ( x = 0 ; x < 118 ; x++ )

{

try_again:

go_no_go_flag = 0;

transmit_pacakge(x); // send the data..

while ( go_no_go_flag == 0 )

{

application.DoEvents();

}

if ( go_no_go_flag == 2 ) // NO GO

goto try_again;

}

My problem is that the data is comming in correctly but the
application.DoEvents(); takes ALOT of time so it slows my application down

In an old DOS program i've written some years ago the entire transmission
took under 1 second but now it takes roughly half a second pr
transmit_package !

What can i do to speed it up?



Best regards

Lasse Madsen
 
My problem is that the data is comming in correctly but the
application.DoEvents(); takes ALOT of time so it slows my application down

Why are you using DoEvents?

Do all the connection stuff in a background thread. You can then update the
UI using Control.Invoke

calling DoEvents is a risky proposition, if you look in the archives you
will see lot of persons discouraging its use
In an old DOS program i've written some years ago the entire transmission
took under 1 second but now it takes roughly half a second pr
transmit_package !

How r u sending them?
the framework does not have a serial component, what r u using?
 
Hi Ignacio,
Why are you using DoEvents?

If i just do:

while ( go_no_go_flag == 0 );

my program freezes
Do all the connection stuff in a background thread. You can then update
the UI using Control.Invoke

I've never tried this.
calling DoEvents is a risky proposition, if you look in the archives you
will see lot of persons discouraging its use

Okay I will look into this.
How r u sending them?
the framework does not have a serial component, what r u using?

The latest framework (2.0) and C# express 2005 has a serial component thats
the one I'm using.

Best regards
Lasse Madsen
 

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

Back
Top