You must Sign In to post a response.
  • Category: .NET

    How to kill process ?

    Hi friends,

    i want to close process and run every time using for loop ?

    But Process run only first time and after that show error process stop , not start next process automatically

    DirectoryInfo d = new DirectoryInfo(txtfolderpath.Text);
    Files = d.GetFiles("*.kmz").Union(d.GetFiles("*.kml")).ToArray(); //Getting kml,kmz files
    str1 = new string[Files.Length];

    for (int i = 0; i < Files.Length; i++)
    {
    str1[i] = Files[i].Name;

    // Create new stopwatch.
    Stopwatch stopwatch = new Stopwatch();

    // Begin timing.
    stopwatch.Start();

    // Start Google Earth
    Process process = Process.Start(txtfolderpath.Text + "\\" + str1[i]);

    int timese = Convert.ToInt16(Convert.ToInt16(txttime.Text) * 1000);

    // Wait process
    Thread.Sleep(timese);

    // Stop timing.
    stopwatch.Stop();

    // Stop Google Earth
    process.Kill();

    }
  • #768326
    Hi,

    As you have coded.

    Process.Kill() method should kill your process and it should not get restarted. But you are mentioning that it get restarted even after killing it. One issue I can think of was for loop you are using that might be the reason for repetitive process run.

    You can try to implement your coding by the below way.


    try
    {
    for (int i = 0; i < Files.Length; i++)
    {
    str1[i] = Files[i].Name;

    // Create new stopwatch.
    Stopwatch stopwatch = new Stopwatch();

    // Begin timing.
    stopwatch.Start();

    // Start Google Earth
    Process process = Process.Start(txtfolderpath.Text + "\\" + str1[i]);

    int timese = Convert.ToInt16(Convert.ToInt16(txttime.Text) * 1000);

    // Wait process
    Thread.Sleep(timese);

    // Stop timing.
    stopwatch.Stop();

    // Stop Google Earth
    process.Kill();

    }

    }
    catch(Exception ex)
    {
    process.Kill();
    MessageBox.Show(ex.Message); // Here you can find why it is not getting killed
    }


    Thanks,
    Mani

  • #768329
    basically process.kill does not immediately kill your process so your this problem gets occur. try to put log in your code and check what exactly happens and where your code exactly gets stuck
    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


Sign In to post your comments