.NET avoiding function within event handler?

I'm pretty new to .NET i must admit, but i'm stumped by this. It's on a pocket pc but i dont see how that makes a difference.
This is in regard to handling SerialPort DataReceived event. The event fires ok (well mostly sometimes it hangs up for a few seconds).
"void onReceiveData(object sender, EventArgs e)
{
String str;
Regex reg = new Regex("(.*\\r\\n)?(^\\$(?<String>.*)\\r\\n)*(?<Excess>.*)?", RegexOptions.Multiline);
if (serial.BytesToRead > 0)
str = gpsBuffer + serial.ReadExisting();
else
return;
Match m = reg.Match(str);
foreach (Capture cap in m.Groups["String"].Captures)
ProcessLine(cap.Value);
gpsBuffer = m.Groups["Excess"].Value;
Paint(null);
}
"
processing incoming gps data here. all the regex and stuff works ok. The Paint method runs fine and without fail every single time. But .NET avoids my ProcessLine!!! . How do I nkow... cuz when I stick a breakpoint in there when debugging, it actually bothers to run it instead of totally skipping it. Liek slowing down the code .NET has some time to run code it has already dam well passed over. If I don't interrupt the code with debugging, i'll be lucky to see ProcessLine run once every minute or two, its like all the functions are getting queued up someplace, I'm so confused can anyone explain it?
[1381 byte] By [Adiraz] at [2007-12-16]
# 1

This is really a petty suggestion and probably has nothing to do with anything but try including braces around the foreach block.

foreach (Capture cap in m.Groups["String"].Captures)
{
ProcessLine(cap.Value);
}

It shouldn't make any difference and I'd hate to think that the compiler was actually having a brain-fart over something like that.

ShaunHayward at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 2
I pretty much knew it wouldn't work. You are allowed one instruction without the { }. Never the less I am really desperate!!
I have been trying out lock { } to no avail and also ThreadPool. I thought maybe because the ProcessLine writes to variables that my Paint is reading that maybe .NET has run 2 threads (one for the event maybe and one for the paint). Both don't solve this delema. Same ***, wait 3 minutes for something that should be updating every second. All functions proven to be working as they should be................. I'm lost for ideas :( probably something really simple............ lol.
Adiraz at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 3
Hello Adiraz,

Is the call to ProcessLine taking a few minutes, or is the enumerator's MoveNext the bottleneck?

I'm not personally familiar with the Capture API you are using. I would suggest breaking into a debugger, or adding tracing code to determine which part of the code is triggering the slowdown (e.g. under a managed debugger).

If you put your code under a native debugger, you may also be able to determine which threads are blocking and where (e.g. view callstacks with symbols).

You can also change the foreach to a simple for loop [to remove the enumeration question].

Hope that helps,
Stephen
http://blogs.msdn.com/stfisher

StephenFisher-MS at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 4

That foreach loop will only ever run if your regular expression matches the input string. One way that I would start debugging would be to add a Console.WriteLine(str) and Console.WriteLine(m.Success) to see if your regex is actaully matching the input string and if not, why not.

When debugging regular expressions, I find Eric Gunnerson's Regular Expression Workbench an invaluable tool. It allows you to enter your regular expression, some input text, and the regex options (in this case multiline). Then you can hit run, and it runs it for you displaying all matches and captures. By seeing these results, you can then tweak the regex until you get it right. There's also other advanced features such as hover interpetation of your expression and compilation to an assembly.

You might also find it easier to use C#'s @"" string feature, so that you don't have to escape all of your \ characters:


Regex reg = new Regex(@"(.*\r\n)?(^\$(?<String>.*)\r\n)*(?<Excess>.*)?", RegexOptions.Multiline);

When Regex's start to get big and complex, removing all the double \ characters does a great deal to help out readability Smile

-Shawn

ShawnFarkas-MS at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Base Class Library...

.NET Development

Site Classified