1

In my application i am checking my files by open Wireshark process before add to my Listbox in this way:

protected string[] invokeProcess(WiresharkProcesses process, string args)
{
    string processToInvoke = null;
    validateProcess(process);

    switch (process)
    {
        case WiresharkProcesses.Capinfo:
            processToInvoke = Path.Combine(getbBasePath, "capinfos.exe");
            break;
        case WiresharkProcesses.Editcap:
            processToInvoke = Path.Combine(getbBasePath, "editcap.exe");
            break;
        case WiresharkProcesses.Tshark:
            processToInvoke = Path.Combine(getbBasePath, "tshark.exe");
            break;
        case WiresharkProcesses.Wireshark:
            processToInvoke = Path.Combine(getbBasePath, "wireshark.exe");
            break;
    }

    ProcessStartInfo capinfosProcess = new ProcessStartInfo(processToInvoke);
    capinfosProcess.Arguments = args;
    capinfosProcess.WindowStyle = ProcessWindowStyle.Hidden;
    capinfosProcess.RedirectStandardOutput = true;
    capinfosProcess.RedirectStandardError = true;
    capinfosProcess.CreateNoWindow = true;
    capinfosProcess.UseShellExecute = false;
    capinfosProcess.ErrorDialog = false;

    List<string> lineList = new List<string>();
    using (Process pros = Process.Start(capinfosProcess))
    {
        StreamReader reader = pros.StandardOutput;
        while (!reader.EndOfStream)
        {
            lineList.Add(reader.ReadLine());
        }

        pros.WaitForExit();
    }

    return lineList.ToArray();
}

private static string getbBasePath
{
    get
    {
        if (Directory.Exists(@"C:\Program Files (x86)\Wireshark"))
        {
            return @"C:\Program Files (x86)\Wireshark";
        }
        else
        {
            return @"C:\Program Files\Wireshark";
        }
    }
}

private void validateProcess(WiresharkProcesses process)
{
    switch (process)
    {
        case WiresharkProcesses.Capinfo:
            break;
        case WiresharkProcesses.Editcap:
            break;
        case WiresharkProcesses.Tshark:
            break;
        case WiresharkProcesses.Wireshark:
            break;
    }
}

}

When i checking this files i am doing this via Thread in order to prevent my Ui to freeze so when i am choose root folder a lot of process opened in the same time and if i am close my application in the middle all those processes remained open and this thing take a lot on memory. how can i prevent it from happen ? maybe by open this process in separate thread and configure this thread to become isBackground = true ?

user2214609
  • 4,713
  • 9
  • 34
  • 41

0 Answers0