How bloody illogical is .NET anyway
What is it with Microsoft, .NET and their inability to make any form of sense.
In what is now a completely aborted attempt to write a traffic grapher in VB.NET, I have discovered some of the most insane, illogical and impossible coding pitfalls and traps ever.
Using a DataTable from an event triggered by WinPCap's capture thread, I get random, and I mean random, null exceptions when attempting to call DataTable's NewRow(). Google is surprisingly unhelpful with this one, talking about uninitialized objects and so forth. Finally I stumble across an idea that it might be a race condition.
So I thread-lock the bastard. And what do you know, SyncLock generates an exception saying it cannot obtain a lock on a null object. Now what? I wrap the code in an If DataTable IsNot Nothing block, which stops the exceptions. Except now I'm losing packets when I capture, compared to Wireshark running with the same parameters. Gah.
After a while, I try to change the capture mode from x packets to continuous. Three ways of doing it according to the documentation.
I can Capture(-1) packets which then runs an infinite capture. Which is a blocking call and can't be stopped short of terminating the program. That ain't fun. Please do not recommend that in documentation.
Secondly, I can use a WinPCap function called StartCapture() that, it promises, will capture until StopCapture() is called. Fine. Works. Program is responsive. Except no packets are being logged. Quick check shows the ***king DataTable is always Nothing when the event handler is fired. Seems like a threading issue. Too much effort to figure this one out.
Lastly, I can eschew the event handler method and go directly to the GetNextPacket() function which returns a packet from the device, or Nothing if none appear before the timeout. Couple of mods to the event handler delegate and it becomes a function I can call. Works nicely. Still dropping packets, but not as many.
But, horror of horrors, I've hacked it together in a Do While Loop structure which spins in a circle waiting for a locking variable to be unset. Thing is, the handler for the Stop button is being blocked by the Do While. Or the actual UI is being blocked. I know this is evil, but it was intended as an intermediate step just to get the GetNextPacket() bit working.
So dig out some timer code and implement a Timer.Elapsed handler that loops the interface until a Nothing is returned, sleeps 100ms to allow UI bits, and goes on. Thing is, System.Timers.Timer is threaded and now I'm getting some cross-threading delegation complaints and I'm supposed to implement Invoke calls to reach functions on the other thread. Fine. Right up until I find out there is no Invoke function on a DataTable object. On the holder form and the DataGrid it's bound to, yeah, but not on the bloody DataTable.
Google says use a System.Windows.Forms.Timer to keep it in the main thread. So then the UI updates as hundreds of packets are added to the DataTable now make hitting the Stop button nigh on a miracle.
So I've trashed the idea and am looking at C and GTK on Windows for this bloody thing.

