--- 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.