0

--- UPDATE ---

After some fiddle I confirm that calling the .Bat file manually works, running it with the Scheduler doesn't work, but running with the Scheduler the CallUrl.exe program works! So the problem is in the Start command in the .Bat? Does exist a specific way to call an external program on this setup? I've upped the task's privileges too.

--- Original message ---

I have programmed on a server a task that must run a batch file that does stuff and then call a program. This program simply calls a weburl passed as argument. It works, but the task signals that the process is still running, and this prevent the task to correctly run the next time.

This is the .Bat called from the task

//Do stuff and then
Start "" "CallUrl.exe" "http://www.UrlPassedAsArgument"

CallUrl is a simple console program that call an url and then must automatically close

    static void Main(string[] args)
    {
        string url = args[0];

        try
        {
            Console.WriteLine("Running " + url);
            Console.WriteLine("Wait");

            using (var client = new NoKeepAlivesWebClient())
            {
                string value = client.DownloadString(url);
                Console.WriteLine("[Complete]");
            }

            Environment.Exit(0);
            return;
        }
        catch(Exception ex)
        {
            Console.WriteLine("[Error]");
            Console.WriteLine(ex);
            Console.ReadLine();
        }
    }

In theory this must force close the process

Environment.Exit(0);
return;

This is the NoKeepAlivesWebClient() class that call the url and close the connection

public class NoKeepAlivesWebClient : WebClient
{
    protected override WebRequest GetWebRequest(Uri address)
    {
        var request = base.GetWebRequest(address);
        if (request is HttpWebRequest)
        {
            ((HttpWebRequest)request).KeepAlive = false;
        }

        return request;
    }
}

If I manually call the .Bat file it work perfectly and then close the console window, but when it's automatically executed by the task schedueler it leaves in the task manager the "Console window host" and "Windows command processor" processes.

The task ends with the "0x41031" code.

At the moment I've programmed the task scheduler so that it ends the process after a given time, but the minimum value is 30 minutes and need it to be processed more frequently.

Fehu
  • 381
  • 1
  • 3
  • 18
  • I know, when called from the scheduler the program doesn't end. I don't know if I must set something in the scheduler, or must add something to the c# code. – Fehu Jan 20 '16 at 12:10
  • Why are you calling Environment.Exit rather than just returning from Main? See http://stackoverflow.com/a/5253543/43846 – stuartd Jan 20 '16 at 12:11
  • I'm trying with just return 0; – Fehu Jan 20 '16 at 12:15
  • It keep returning the 0x41031 code :( – Fehu Jan 20 '16 at 12:21
  • Add an `exit` call after `Start` in your batch file – stuartd Jan 20 '16 at 12:39
  • Added but still doesn't work. If I manually call the url opened in the c# program it works, so no error in this point. Can in some qway be the _weblient_ that awaits something and doesn't end the _using_? – Fehu Jan 20 '16 at 13:00
  • You may need to [set the timeout on the webclient](http://stackoverflow.com/questions/1789627/how-to-change-the-timeout-on-a-net-webclient-object) – stuartd Jan 20 '16 at 13:04
  • How is the task configured in Task Scheduler? – Squashman Jan 20 '16 at 13:34
  • I've modified the class to manage a timeout of 1 minut, but it's still running in the background – Fehu Jan 20 '16 at 13:38
  • The trigger in the Scheduler is simply "_Repeat task every 5 minutes_" and "_for a duration of Indefinility_" – Fehu Jan 20 '16 at 13:40
  • 1
    Provide the full path to `CallUrl.exe` in your batch file... – aschipfl Jan 20 '16 at 19:14

0 Answers0