473,405 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Threading issue & static objects

I'm experiencing a threading problem that's really perplexing me. I
have a multithreaded application that reads car ECU information that's
sent over the serial port by the ECU and received by the
laptop/program.

I'll try to give a concise synopsis as the program is easily 10k+
lines.

main.cs - contains the application start function. Here's the entire
code (minus misc junk) as it's rather small -

public class main
{
public static UIForm UI;
public static Reader terminal;

[STAThread]
public static void Main()
{
terminal = new Reader();
testform = new UIForm();
System.Windows.Forms.Application.Run(UI);
}
}

Nothing terribly complex here. UI is the user interface the user
interacts with.

Reader is the communications class. It does all the com port stuff as
well as processing and making sense of the data that's coming in.
When it opens a com port it spawns a new process that does the
low-level junk of monitoring the com port and stuffing incoming bytes
into a shared queue. Everything works A-OK in this department.

There is a "second level" reader function that takes those raw bytes,
dequeues them and handles synchronization and stuffing those bytes
into correctly-formed 16 byte arrays (ECU "packets") that the program
needs to decode. It's a big while loop that keeps repeating and doing
whatever it needs to do to the incoming bytes to massage them into the
correct packet formation. This function is spawned off as a separate
process and runs until the app is terminated. It is spawned off in
the constructor of the Reader class. Specifically:

public Reader()
{
settings = new CommReaderSettings(); // Just com port stuff
rxThreadLvl2 = new Thread(new ThreadStart(this.readLevel2));
rxThreadLvl2.Name = "RxLvl2";

rxThreadLvl2.Priority = ThreadPriority.Normal;
rxThreadLvl2.Start();
}

Now the rest of the reader class contains a bunch of different
functions that do different stuff but basically boil down to three
things - write data out the com port, write info the to UI, and
communicates with the RxLvl2 thread by reading and writing variables
in a SyncMe object.

Here's a snippet of the SyncMe class:

public class SyncMe
{
public bool ack=false;
public bool nak=false;
public bool Synced=false;
public bool lookingForVal=false;
public bool valFound=false;
public bool acknakFound=false;
}

Nothing too sexy there. The SyncMe class is declared as a non-static
object within the statically created (in main.cs) Reader
class/terminal object.

public class Reader
{
public SyncMe syncObj = new SyncMe();
public Reader() {...}
public void readLevel2() {...}
public void StopLogging() {...}
}

Now for my problem. I send a command to the ECU to stop sending data
and my problem occurs when trying to clear out the com port queue.
Obviously I can clear out the queue in software, but there are
invariably some bytes either in the out buffer of the ECU or in the in
buffer of the laptop. So I get anywhere from 0-20 bytes coming
through after I send the command to the ECU to stop sending data.

My strategy for this was simple - declare a boolean in the SyncMe
class that indicates that the readLevel2() function should continue to
receive bytes but simply throw them away. This boolean is flagged via
the UI calling a "Stop Logging" function in the Reader object which in
turn actually sets the bool.

If the readLevel2() function/loop sees a byte and has to throw it
away, it flags in the SyncMe obj that it has found one.

Here is the stop logging function:

public void StopLogging()
{
lock(typeof(SyncMe))
{
syncObj.clearQueue=true;
syncObj.logging=false;
syncObj.byteFound=false;
}
Thread.Sleep(1000);
if(syncObj.byteFound==true)
{
lock(typeof(SyncMe))
{ syncObj.byteFound=false; }
Thread.Sleep(1000);
}
}

However, the readLevel2() thread never completely throws away the
data. I've debugged and can see that it properly enters the code
block when the bools are flagged, so everything is fine there.

My suspicion is that when I tell the static Reader class (static
terminal object) to sleep it's also causing the readLevel2() thread to
sleep. This would mean that the readLevel(2) would have nearly zero
time to do any throwing away of bytes.

I don't know why or how, but it's the only explanation I can think of.
Does this make any sense at all? I know it "shouldn't" be happening,
but none of the MSDN samples or other threading samples I have scoured
the web for give ANY examples of spawning/sleeping/etc with static
objects.

Is this sleep call indeed sleeping both threads? If you can't tell
from the code and description, is there some way I can determine this
myself?

Thanks,
Ray Ackley
Nov 16 '05 #1
1 1654
Ray.

Mate! You sound like you need to use asynchronous calls, because you can actually tell the asynch process to stop immediately.
Since a thread is non-deterministic, its difficult to tell when your boolean value signalling it will have an effect. You would need to ensure that you are signalling all stages in the chain to stop processing. This can be achieved quite simply with an asynch process, and also you can get your worker process to signal to you what is happening easily.

Threads are good, but all they allow you to do is tell some process to go off and do its job. You can signal it, but I'd guess you'll have a lot more trouble achieving the same using threads - One other thing you might be able to do, is abort the thread, and catch the threadabort exception at the caller, so long as it doesn't matter that you loose data that hasn't been processed.
You might also get the thread to kick off a method that creates a class to do the work, then when you abort the thread, the object instance dies, and your destructor can handle cleanup?
I still say the asynch stuff would be much cleaner with a lot less guessing... I thought threads were the way to go, until I used asych. Now I hardly use threads at all, because they're so hit and miss....

I hope this helps you a little.

"Ray Ackley" wrote:
I'm experiencing a threading problem that's really perplexing me. I
have a multithreaded application that reads car ECU information that's
sent over the serial port by the ECU and received by the
laptop/program.

I'll try to give a concise synopsis as the program is easily 10k+
lines.

main.cs - contains the application start function. Here's the entire
code (minus misc junk) as it's rather small -

public class main
{
public static UIForm UI;
public static Reader terminal;

[STAThread]
public static void Main()
{
terminal = new Reader();
testform = new UIForm();
System.Windows.Forms.Application.Run(UI);
}
}

Nothing terribly complex here. UI is the user interface the user
interacts with.

Reader is the communications class. It does all the com port stuff as
well as processing and making sense of the data that's coming in.
When it opens a com port it spawns a new process that does the
low-level junk of monitoring the com port and stuffing incoming bytes
into a shared queue. Everything works A-OK in this department.

There is a "second level" reader function that takes those raw bytes,
dequeues them and handles synchronization and stuffing those bytes
into correctly-formed 16 byte arrays (ECU "packets") that the program
needs to decode. It's a big while loop that keeps repeating and doing
whatever it needs to do to the incoming bytes to massage them into the
correct packet formation. This function is spawned off as a separate
process and runs until the app is terminated. It is spawned off in
the constructor of the Reader class. Specifically:

public Reader()
{
settings = new CommReaderSettings(); // Just com port stuff
rxThreadLvl2 = new Thread(new ThreadStart(this.readLevel2));
rxThreadLvl2.Name = "RxLvl2";

rxThreadLvl2.Priority = ThreadPriority.Normal;
rxThreadLvl2.Start();
}

Now the rest of the reader class contains a bunch of different
functions that do different stuff but basically boil down to three
things - write data out the com port, write info the to UI, and
communicates with the RxLvl2 thread by reading and writing variables
in a SyncMe object.

Here's a snippet of the SyncMe class:

public class SyncMe
{
public bool ack=false;
public bool nak=false;
public bool Synced=false;
public bool lookingForVal=false;
public bool valFound=false;
public bool acknakFound=false;
}

Nothing too sexy there. The SyncMe class is declared as a non-static
object within the statically created (in main.cs) Reader
class/terminal object.

public class Reader
{
public SyncMe syncObj = new SyncMe();
public Reader() {...}
public void readLevel2() {...}
public void StopLogging() {...}
}

Now for my problem. I send a command to the ECU to stop sending data
and my problem occurs when trying to clear out the com port queue.
Obviously I can clear out the queue in software, but there are
invariably some bytes either in the out buffer of the ECU or in the in
buffer of the laptop. So I get anywhere from 0-20 bytes coming
through after I send the command to the ECU to stop sending data.

My strategy for this was simple - declare a boolean in the SyncMe
class that indicates that the readLevel2() function should continue to
receive bytes but simply throw them away. This boolean is flagged via
the UI calling a "Stop Logging" function in the Reader object which in
turn actually sets the bool.

If the readLevel2() function/loop sees a byte and has to throw it
away, it flags in the SyncMe obj that it has found one.

Here is the stop logging function:

public void StopLogging()
{
lock(typeof(SyncMe))
{
syncObj.clearQueue=true;
syncObj.logging=false;
syncObj.byteFound=false;
}
Thread.Sleep(1000);
if(syncObj.byteFound==true)
{
lock(typeof(SyncMe))
{ syncObj.byteFound=false; }
Thread.Sleep(1000);
}
}

However, the readLevel2() thread never completely throws away the
data. I've debugged and can see that it properly enters the code
block when the bools are flagged, so everything is fine there.

My suspicion is that when I tell the static Reader class (static
terminal object) to sleep it's also causing the readLevel2() thread to
sleep. This would mean that the readLevel(2) would have nearly zero
time to do any throwing away of bytes.

I don't know why or how, but it's the only explanation I can think of.
Does this make any sense at all? I know it "shouldn't" be happening,
but none of the MSDN samples or other threading samples I have scoured
the web for give ANY examples of spawning/sleeping/etc with static
objects.

Is this sleep call indeed sleeping both threads? If you can't tell
from the code and description, is there some way I can determine this
myself?

Thanks,
Ray Ackley

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Zuel | last post by:
Sup everyone! I wrote this code for Tomcat appserver but I am told from an associate that it has threading issues. The basic idea is to store a read only data table for everyone to use. It...
77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
4
by: Gauthier | last post by:
Hi, I've a simple issue with the use of extension objects. I'm trying to call a text formating method from an object that I add to my arguments collection, this method take an input string and...
4
by: Ayende Rahien | last post by:
The following first throws the nasty exception dialog, and only then show me the message. Is there a way to avoid this? This occur both inside & outside VS. public class Test { private...
8
by: Z D | last post by:
Hello, I'm having a strange problem that is probably due to my lack of understanding of how threading & COM Interop works in a WinForms.NET application. Here's the situation: I have a 3rd...
6
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a...
18
by: Frank Rizzo | last post by:
Hello, I have a class with all static methods that is called by multiple threads. I was wondering what effect that has on the competing threads. Does Thread2 have to wait until Thread1 is done...
126
by: Dann Corbit | last post by:
Rather than create a new way of doing things: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html why not just pick up ACE into the existing standard:...
13
by: Alexander Gnauck | last post by:
Hello, while using async sockets I ran into a strange problem which occurs only on some machines. I wrote a small demo application which can be used to reproduce the problem. You can download it...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.