|
I wonder if it would help to cast the value or specify the underlying type of the Nullable object:
private Nullable<Int32> _Discount;
Jack of all trades ~ Master of none.
|
|
|
|
|
Hi,
May I have some fresh eyes please?
Under a Windows service running as LocalSystem, on a 60 second timed interval, I am running the following:
Process Ns = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("netstat");
startInfo.Arguments = "-ano";
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.CreateNoWindow = true;
Ns.StartInfo = startInfo;
Ns.Start();
Ns.WaitForExit();
for (int i = 0; i < 4; i++)
Ns.StandardOutput.ReadLine();
while (true)
{
string Line = (Ns.StandardOutput.ReadLine());
if (!string.IsNullOrEmpty(Line))
{
Line = Line.Trim();
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(":", " ");
Line = Line.Replace('\r', ' ');
Line = Line.Replace('\n', ' ');
string[] s = Line.Split(' ')
if (s.Length == 7)
{
insns.Parameters["@Protocol"].Value = s[0];
insns.Parameters["@LocalHost"].Value = s[1];
insns.Parameters["@LocalPort"].Value = s[2];
insns.Parameters["@RemoteHost"].Value = s[3];
insns.Parameters["@RemotePort"].Value = s[4];
insns.Parameters["@State"].Value = s[5];
insns.Parameters["@PID"].Value = s[6];
}
else
{
insns.Parameters["@Protocol"].Value = s[0];
insns.Parameters["@LocalHost"].Value = s[1];
insns.Parameters["@LocalPort"].Value = s[2];
insns.Parameters["@RemoteHost"].Value = s[3];
insns.Parameters["@RemotePort"].Value = s[4];
insns.Parameters["@State"].Value = "0";
insns.Parameters["@PID"].Value = s[5];
}
if (Line == null)
break;
}
Ns.Close();
Ns.Dispose();
however, for no obvious reason, sometimes after only a few minutes of running or sometimes after many hours I get an exception:
System.IndexOutOfRangeException: Index was outside the bounds of the array.
Can I track which index this is and 'On Error Resume Next' around it? How can I negate this error or dispose of it and continue?
It doesn't matter too much if in one cycle I only get 71 instead of 72 connections dispalayed as, 1 minute later. I collect them all again anyway.
The problem it causes is that the netstat does quit and stays running in the Task Manager which means that all subsequent netstats start piling up behind it and, although the data gets written to the db, I can end up with dozens of netstats in my Task Manager which then have to be End-Tasked.
I am fairly sure that this is because the netstat never gets to .Close & .Dispose thus never receives an Exit signal so the .WaitForExit stays waiting. I am right in this assumption? How can I send it an Exit signal after the exception is called?
Any ideas please?
|
|
|
|
|
CCodeNewbie wrote: I am fairly sure that this is because the netstat never gets to .Close & .Dispose thus never receives an Exit signal so the .WaitForExit stays waiting. I am right in this assumption?
I wouldn't pick it as the first suspect for an "index out of range" message.
CCodeNewbie wrote: Can I track which index this is
Did the exception come with a stack-trace? What is the content of "Exception.ToString()"?
CCodeNewbie wrote: 'On Error Resume Next' around it?
Let's ignore that you mentioned this.
string[] s = Line.Split(' ')
if (s.Length == 7)
{
insns.Parameters["@Protocol"].Value = s[0];
insns.Parameters["@LocalHost"].Value = s[1];
insns.Parameters["@LocalPort"].Value = s[2];
insns.Parameters["@RemoteHost"].Value = s[3];
insns.Parameters["@RemotePort"].Value = s[4];
insns.Parameters["@State"].Value = s[5];
insns.Parameters["@PID"].Value = s[6];
}
elseif (s.Length == 6)
{
insns.Parameters["@Protocol"].Value = s[0];
insns.Parameters["@LocalHost"].Value = s[1];
insns.Parameters["@LocalPort"].Value = s[2];
insns.Parameters["@RemoteHost"].Value = s[3];
insns.Parameters["@RemotePort"].Value = s[4];
insns.Parameters["@State"].Value = "0";
insns.Parameters["@PID"].Value = s[5];
}
else
{
throw new Exception("This is where it probably goes boom");
}
Bastard Programmer from Hell
|
|
|
|
|
Quote: I wouldn't pick it as the first suspect for an "index out of range" message. I meant could that be the reason I get a traffic jam of netstats in the Task Manager
Quote: Did the exception come with a stack-trace? What is the content of "Exception.ToString()"?
I have coded in a .InnerException to try and get the trace. The existing error only gives "System.IndexOutOfRangeException: Index was outside the bounds of the array."
Quote: Let's ignore that you mentioned this. Hee Hee Hee
I have rebuilt and have to wait to see what exceptions may arise. Will advise asap.
|
|
|
|
|
Hi Eddy,
Sorry I haven't got back to you sooner. Did what you suggested but now it makes even less sense:
throw new Exception("failed " + s.Length + " : " + s + " : " + Line);
results in
failed 6 : System.String[] : UDP 0.0.0.0 445 * * 4
pardon my ignorance but why is it ignoring the
if (s.Length == 6) instruction?
|
|
|
|
|
CCodeNewbie wrote:
pardon my ignorance but why is it ignoring the
if (s.Length == 6) instruction?
It shouldn't, if the string can be divided into six different words. The line that has been logged, would be broken down like this;
- UDP
- 0.0.0.0
- 445
- *
- *
- 4
You can verify the result using the code below;
foreach (string someWord in s)
{
System.Diagnostics.Debugger.WriteLine("'{0}'", someWord);
}
Also, can you post your (updated) code again?
Bastard Programmer from Hell
|
|
|
|
|
Hi Eddy,
Usually it does divide. "UDP 0.0.0.0:445 *:* 4" put through
if (s.Length == 6)
{
insns.Parameters["@Protocol"].Value = s[0];
insns.Parameters["@LocalHost"].Value = s[1];
insns.Parameters["@LocalPort"].Value = s[2];
insns.Parameters["@RemoteHost"].Value = s[3];
insns.Parameters["@RemotePort"].Value = s[4];
insns.Parameters["@State"].Value = "0";
insns.Parameters["@PID"].Value = s[5];
} becomes
s[0]=UDP
s[1]=0.0.0.0
s[2]=445
s[3]=*
s[4]=*
s[5]=0
s[6]=4
if I remove the line "insns.Parameters["@State"].Value = "0";"
the output reports "Listening" even though there is no value in the netstat cmd-line output.
Sorry this is taking a while, worked out I had to use System.Diagnostics.Debug.WriteLine instead of System.Diagnostics.Debugger.WriteLine
So, I did
Process Ns = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("netstat");
startInfo.Arguments = "-ano";
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.CreateNoWindow = true;
Ns.StartInfo = startInfo;
Ns.Start();
Ns.WaitForExit();
for (int i = 0; i < 4; i++)
Ns.StandardOutput.ReadLine();
while (true)
{
Line = (Ns.StandardOutput.ReadLine());
if (!string.IsNullOrEmpty(Line))
{
Line = Line.Trim();
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(":", " ");
Line = Line.Replace('\r', ' ');
Line = Line.Replace('\n', ' ');
s = Line.Split(' ');
if (s[0] == "TCP")
{
if (s[1] != "0.0.0.0")
{
insns.Parameters["@Tstamp"].Value = DateTime.Now;
insns.Parameters["@SysID"].Value = SID;
insns.Parameters["@Protocol"].Value = s[0];
insns.Parameters["@LocalHost"].Value = s[1];
insns.Parameters["@LocalPort"].Value = s[2];
insns.Parameters["@RemoteHost"].Value = s[3];
insns.Parameters["@RemotePort"].Value = s[4];
insns.Parameters["@State"].Value = s[5];
insns.Parameters["@PID"].Value = s[6];
NSInfo.Open();
insns.ExecuteNonQuery();
NSInfo.Close();
}
}
if (s[0] == "UDP")
{
if (s.Length == 6)
{
insns.Parameters["@Tstamp"].Value = DateTime.Now;
insns.Parameters["@SysID"].Value = SID;
insns.Parameters["@Protocol"].Value = s[0];
insns.Parameters["@LocalHost"].Value = s[1];
insns.Parameters["@LocalPort"].Value = s[2];
insns.Parameters["@RemoteHost"].Value = s[3];
insns.Parameters["@RemotePort"].Value = s[4];
insns.Parameters["@State"].Value = "0";
insns.Parameters["@PID"].Value = s[5];
NSInfo.Open();
insns.ExecuteNonQuery();
NSInfo.Close();
}
if (s.Length == 7)
{
insns.Parameters["@Tstamp"].Value = DateTime.Now;
insns.Parameters["@SysID"].Value = SID;
insns.Parameters["@Protocol"].Value = s[0];
insns.Parameters["@LocalHost"].Value = s[1];
insns.Parameters["@LocalPort"].Value = s[2];
insns.Parameters["@RemoteHost"].Value = s[3];
insns.Parameters["@RemotePort"].Value = s[4];
insns.Parameters["@State"].Value = s[5];
insns.Parameters["@PID"].Value = s[6];
NSInfo.Open();
insns.ExecuteNonQuery();
NSInfo.Close();
}
else
{
foreach (string someWord in s)
{
System.Diagnostics.Debug.WriteLine("'{0}'", someWord);
}
}
}
when the timer triggered my CPU ramped up to 99% usage by my app and I had to End-Task it. Nothing was written to the Output or the Event log.
|
|
|
|
|
CCodeNewbie wrote: becomes
s[0]=UDP
s[1]=0.0.0.0
s[2]=445
s[3]=*
s[4]=*
s[5]=0
s[6]=4
That's strange; if s[] contains six elements, how did we get seven items here? s[5] should be 4 IMHO, and s[6] shouldn't be there.
CCodeNewbie wrote: worked out I had to use System.Diagnostics.Debug.WriteLine instead of System.Diagnostics.Debugger.WriteLine
Aw, my apologies; I tend to mix them up outside of the IDE. It should list all the words in the array in the output-window of Visual Studio.
CCodeNewbie wrote: when the timer triggered my CPU ramped up to 99% usage by my app and I had to End-Task it. Nothing was written to the Output or the Event log.
Could it be that it's reading a lot of empty lines?
while (true)
{
Line = (Ns.StandardOutput.ReadLine());
if (!string.IsNullOrEmpty(Line))
Bastard Programmer from Hell
|
|
|
|
|
from a cmd-line netstat -ano you can see that the field usually used by "State" tends to be empty under the UDP protocol unless there is some stream coming in in which case it will show "Established" or whatever. Because I am writing the values to a SQL table I need to keep s[6] in the PID column.
Quote: Could it be that it's reading a lot of empty lines? don't think so, usually it works fine until it hits a null/empty value somewhere in the s[] array. What would be ideal is a way of saying:
if (s[whatever].isNullorEmpty){s[whatever] = "0";}
|
|
|
|
|
Select the menu "Debug" and then "Toggle Breakpoint", and use F10 to step through your code.
CCodeNewbie wrote: What would be ideal is a way of saying:
Something like this?
string FetchFromArray(string[] source, int idx)
{
if (idx <= source.Length)
return source[idx];
else
return String.Empty;
}
public Form1()
{
InitializeComponent();
string[] s = "Hello world from Mars!".Split(' ');
Text = FetchFromArray(s, 7);
}
Bastard Programmer from Hell
|
|
|
|
|
Trying to work out why mu CPU is going mad at the moment, I'll get back to this in a bit..
|
|
|
|
|
|
|
Had a leg of lamb myself, roast potatoes, roast butternut, sweetish-sharp gravy... All good...
|
|
|
|
|
CCodeNewbie wrote: Had a leg of lamb myself, roast potatoes, roast butternut, sweetish-sharp gravy... All good...
Sounds like an equal festive meal - today, life's good
Bastard Programmer from Hell
|
|
|
|
|
tell it like it is fellow gastronaut... and thanks for looking in on the other thing that's making me tear my hair out...
Why do so may seemingly simple things have to be so complicated...
|
|
|
|
|
tried
if (s.Length == 6)
{
insns.Parameters["@Tstamp"].Value = DateTime.Now;
insns.Parameters["@SysID"].Value = SID;
insns.Parameters["@Protocol"].Value = s[0];
insns.Parameters["@LocalHost"].Value = s[1];
insns.Parameters["@LocalPort"].Value = s[2];
insns.Parameters["@RemoteHost"].Value = s[3];
insns.Parameters["@RemotePort"].Value = s[4];
insns.Parameters["@State"].Value = string.Empty;
insns.Parameters["@PID"].Value = s[5];
NSInfo.Open();
insns.ExecuteNonQuery();
NSInfo.Close()
};
same thing - service starts, netstat launches, CPU 99%
|
|
|
|
|
1.
CCodeNewbie wrote: if (Line == null) break;
Line can't be null at that point
2.
Do as Eddy said, check for the presence of 6 parts if you're consuming 6 parts. Never trust input, sooner or later it will not be what you expected, so cope with that.
3.
Keep a copy of the original input line, and display it in the exception thrown by the code Eddy added.
And/or log all the lines you are getting from netstat.
|
|
|
|
|
Have added in a new exception to display s.length.
Would you rather recommend
if(Line.length = 0)?
|
|
|
|
|
you already have an emptyOrNull test at the start, use that to exit the loop.
and I tend to display the original inputs when things go wrong, and optionally also derived values.
chances are NetStat occasionally emits a line you never expected!
|
|
|
|
|
Nothing to do with your question, but consider changing:
string Line = (Ns.StandardOutput.ReadLine());
to
StringBuilder Line = new StringBuilder((Ns.StandardOutput.ReadLine()));
and just call:
Line.Replace(" ", " ");
....
instead of
Line = Line.Replace(" ", " ");
...
Here the code is sequential and usage between string and StringBuilder is not that different, but if you do stringmanipulation in a loop, the performance difference can become huge.
Just a tip.
V.
|
|
|
|
|
I'm just starting WPF/C#, so it might be an easy question.
I have a listview on the left side of my wfp form and when I click on one of the listview icons I want to load an other wpf next to the listview.
But how do I load wpf1 on the main wpf and how do I unload wpf2 and load wpf2 when I click one of the listview icons
|
|
|
|
|
Just for fun i think you people should check out this software! And build it too if it's possible!
|
|
|
|
|
VKOntakte API method wall.get returns Json of type {"response": [message_count, {"id": 219, ...}, {"id": 219, ...}]}. The field "message_count" does not allow to use class for deserializing the response like {"response": [{"id": 219, ...}, {"id": 219, ...}]}. Please suggest me what kind of class structure should be used.
|
|
|
|
|
tfile101 wrote: VKOntakte API method
Have you tried their forum? There won't be much CP-users that have experience with this particular API. Support is usually available from the vendor.
tfile101 wrote: does not allow to use class
Why? Did you receive a compile-time error? If yes, what did it state?
tfile101 wrote: Please suggest me what kind of class structure should be used.
If they provide a way to serialize, I'd expect them to provide a way to deserialize. If not, take a look at JSON.NET[^].
Bastard Programmer from Hell
|
|
|
|