473,657 Members | 2,538 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Applicati on.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 CommReaderSetti ngs(); // Just com port stuff
rxThreadLvl2 = new Thread(new ThreadStart(thi s.readLevel2));
rxThreadLvl2.Na me = "RxLvl2";

rxThreadLvl2.Pr iority = ThreadPriority. Normal;
rxThreadLvl2.St art();
}

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=f alse;
public bool valFound=false;
public bool acknakFound=fal se;
}

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(Syn cMe))
{
syncObj.clearQu eue=true;
syncObj.logging =false;
syncObj.byteFou nd=false;
}
Thread.Sleep(10 00);
if(syncObj.byte Found==true)
{
lock(typeof(Syn cMe))
{ syncObj.byteFou nd=false; }
Thread.Sleep(10 00);
}
}

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 1682
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.Applicati on.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 CommReaderSetti ngs(); // Just com port stuff
rxThreadLvl2 = new Thread(new ThreadStart(thi s.readLevel2));
rxThreadLvl2.Na me = "RxLvl2";

rxThreadLvl2.Pr iority = ThreadPriority. Normal;
rxThreadLvl2.St art();
}

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=f alse;
public bool valFound=false;
public bool acknakFound=fal se;
}

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(Syn cMe))
{
syncObj.clearQu eue=true;
syncObj.logging =false;
syncObj.byteFou nd=false;
}
Thread.Sleep(10 00);
if(syncObj.byte Found==true)
{
lock(typeof(Syn cMe))
{ syncObj.byteFou nd=false; }
Thread.Sleep(10 00);
}
}

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
1446
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 takes some time for the table to be created. I want to store the table in a singleton and update it periodicly, basicaly when a user logs into the system. By keeping 1 table allocated I don't have to worry about memmory usage and if a user does...
77
5259
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 the moment. I'd be *very* grateful if people with any interest in multi-threading would read it (even just bits of it - it's somewhat long to go through the whole thing!) to check for accuracy, effectiveness of examples, etc. Feel free to mail...
4
1975
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 output the formatted string. So far everything process correctly (the method is called without any issue) but my problem is that the output is htmlencoded, it means that my method (wich work as intended in other contexts) is called but the...
4
326
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 static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
8
8192
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 party COM component that takes about 5 seconds to run one of its functions (Network IO bound call). Since I dont want my GUI to freeze
6
2865
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 TimerState object similar to the example in the System.Threading.Timer documentation. The method which handles the timer events, among other things, periodically calls a method in this TimerState object which raises an event to the startup form,...
18
3317
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 with the StaticClass.Method1 before it can use it? What if I removed static methods and made all the threads instantiate its own copy of the class? Would that remove the waiting contention?
126
6682
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: http://www.cse.wustl.edu/~schmidt/ACE.html the same way that the STL (and subsequently BOOST) have been subsumed? Since it already runs on zillions of platforms, they have obviously worked most of the kinks out of the generalized threading and processes idea (along with many...
13
1764
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 from here: http://alex.ag-software.de/SocketTest.zip If you press the "Connect in new Thread" button from the test application you may be able to cause the System.IO.IOException: Unable to write data to the transport connection. While the...
0
8324
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8842
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8740
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8513
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7352
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2742
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.