Click here to Skip to main content
15,894,328 members
Home / Discussions / C#
   

C#

 
AnswerRe: system.reflection.targetinvocationexception when pas null referecne to COM Pin
Abhinav S4-Apr-12 21:59
Abhinav S4-Apr-12 21:59 
GeneralRe: system.reflection.targetinvocationexception when pas null referecne to COM Pin
yanghua944-Apr-12 22:57
yanghua944-Apr-12 22:57 
Questionhelp needed for Virtual memory Pin
tatsat4-Apr-12 19:27
tatsat4-Apr-12 19:27 
QuestionHow to edit textbox that is bound with int Pin
kruegs354-Apr-12 10:50
kruegs354-Apr-12 10:50 
AnswerRe: How to edit textbox that is bound with int Pin
JOAT-MON4-Apr-12 14:22
JOAT-MON4-Apr-12 14:22 
GeneralRe: How to edit textbox that is bound with int Pin
kruegs355-Apr-12 5:11
kruegs355-Apr-12 5:11 
GeneralRe: How to edit textbox that is bound with int Pin
JOAT-MON6-Apr-12 8:28
JOAT-MON6-Apr-12 8:28 
QuestionIndexOutOfRangeExceoption while parsing a Netstat process Pin
CCodeNewbie4-Apr-12 10:46
CCodeNewbie4-Apr-12 10:46 
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:
C#
//set & call process netstat -ano
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))
    {
//remove all spaces/padding/charactes so that only spaces remain between values
        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(' ')
//tcp protocol has 7 values, udp connections to external IP address have 7 values
        if (s.Length == 7)
            {
//insns is a sqlcommand - this works faultlessly every time 
            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
//non-connected udp ports have no 'state' thus they have only 6 values
            {
            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];
            }
//check for the end of the output
        if (Line == null)
        break;
        }
//clear up
    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?
AnswerRe: IndexOutOfRangeExceoption while parsing a Netstat process Pin
Eddy Vluggen4-Apr-12 11:20
professionalEddy Vluggen4-Apr-12 11:20 
GeneralRe: IndexOutOfRangeExceoption while parsing a Netstat process Pin
CCodeNewbie4-Apr-12 11:37
CCodeNewbie4-Apr-12 11:37 
GeneralRe: IndexOutOfRangeExceoption while parsing a Netstat process Pin
CCodeNewbie7-Apr-12 13:41
CCodeNewbie7-Apr-12 13:41 
AnswerRe: IndexOutOfRangeExceoption while parsing a Netstat process Pin
Eddy Vluggen7-Apr-12 22:21
professionalEddy Vluggen7-Apr-12 22:21 
GeneralRe: IndexOutOfRangeExceoption while parsing a Netstat process Pin
CCodeNewbie8-Apr-12 0:14
CCodeNewbie8-Apr-12 0:14 
GeneralRe: IndexOutOfRangeExceoption while parsing a Netstat process Pin
Eddy Vluggen8-Apr-12 0:28
professionalEddy Vluggen8-Apr-12 0:28 
GeneralRe: IndexOutOfRangeExceoption while parsing a Netstat process Pin
CCodeNewbie8-Apr-12 1:12
CCodeNewbie8-Apr-12 1:12 
AnswerRe: IndexOutOfRangeExceoption while parsing a Netstat process Pin
Eddy Vluggen8-Apr-12 1:48
professionalEddy Vluggen8-Apr-12 1:48 
GeneralRe: IndexOutOfRangeExceoption while parsing a Netstat process Pin
CCodeNewbie8-Apr-12 2:09
CCodeNewbie8-Apr-12 2:09 
GeneralRe: IndexOutOfRangeExceoption while parsing a Netstat process Pin
CCodeNewbie8-Apr-12 8:21
CCodeNewbie8-Apr-12 8:21 
GeneralRe: IndexOutOfRangeExceoption while parsing a Netstat process Pin
Eddy Vluggen8-Apr-12 8:29
professionalEddy Vluggen8-Apr-12 8:29 
GeneralRe: IndexOutOfRangeExceoption while parsing a Netstat process Pin
CCodeNewbie8-Apr-12 8:33
CCodeNewbie8-Apr-12 8:33 
GeneralRe: IndexOutOfRangeExceoption while parsing a Netstat process Pin
Eddy Vluggen8-Apr-12 8:56
professionalEddy Vluggen8-Apr-12 8:56 
GeneralRe: IndexOutOfRangeExceoption while parsing a Netstat process Pin
CCodeNewbie8-Apr-12 8:58
CCodeNewbie8-Apr-12 8:58 
GeneralRe: IndexOutOfRangeExceoption while parsing a Netstat process - update Pin
CCodeNewbie8-Apr-12 0:30
CCodeNewbie8-Apr-12 0:30 
AnswerRe: IndexOutOfRangeExceoption while parsing a Netstat process Pin
Luc Pattyn4-Apr-12 11:38
sitebuilderLuc Pattyn4-Apr-12 11:38 
GeneralRe: IndexOutOfRangeExceoption while parsing a Netstat process Pin
CCodeNewbie4-Apr-12 11:41
CCodeNewbie4-Apr-12 11:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.