Task scheduler says the task is completed but upon checking, my .exe didn't produce some output. there's nothing in my log as well.
I created a .BAT file to execute my C# program .exe in task scheduler.
Here's my .BAT file code -
echo #off
start "Bank" C:\Users\mySpace\Desktop\bank.exe
Here's the screenshot of my Task scheudler -
Related
I have built a Selenium project in C# and would like to run it every day as a task in task scheduler.
I would like to know how to make the project exec file or how to do it effectively.
You can schedule the task by doing the following
Required Tools
1. NUnit3 Console. Location: https://docs.nunit.org/articles/nunit/running-tests/Console-Command-Line.html
2. Windows Task Scheduler
Download NUnit.Console-*.msi file.
Install exe file.
Create an nunit command to run your tests:
> nunit3-console testsfile.dll
More details here: https://github.com/nunit/docs/wiki/Console-Command-Line
Next, create the scheduled task
List item
1. Open Task Scheduler
2. Under Actions, Click Create a Basic Task
3. Provide a descriptive name
4. Choose the starting date and time.
5. Choose Start a Program as the type of action
6. In Program/Script add nunit3-console
7. In Arguments add testsfile.dll
7. In Start in add nunit3 console location
8. Click Finish.
Now it will run on the schedule you provided.
I have a batch file that has these commands to start a virtual environment and run a python file. when I start batch files manually through CMD it is running as expected. I have a different program (.NET web application) to start this batch file and arguments are passed from the web application to the batch file. However, the batch file takes the arguments but doesn't start the virtual-environment or python file.
#echo on
set filename="D:\test.txt"
echo %1_%2_%3 >%filename%
set root=C:\Users\TMF\Anaconda3
call %root%\Scripts\activate.bat %root%
call activate sales_analysis
C:\Users\TMF\Anaconda3\envs\sales_analysis\python.exe D:/sales/prediction_client.py %1 %2 %3
pause
After few hours of google search,I found that there are some restrictions with the default IIS user (usually it’s called “ApplicationPoolIdentity”).So I created a new service account on the server and assign my web application to new user and restart the application pool.After that batch file was able to start the python process as expected.
My C# and WPF application task can't be deleted by Task Manager or by console. It's a very big application but there's nothing new that can make the task 'bulletproof'
I tried:
wmic process where name="MyApp.exe" call terminate
But it says: return value 2 and it does't kill the process.
With
Taskkill /IM MyApp.exe /T /F
It returns "Access denied" even when I run it as administrator.
I don't really know what is happening and why Windows can't close the task of my application. If I restart the computer the task finish.
TL;DR
Why
System.Reflection.Assembly.GetExecutingAssembly().Location
returns "<Unknown>"?
The whole story:
When I run my console application manually, everything goes well. When I set Task Scheduler to run my app, there is a problem: my app can't find some dependent files.
These files are written in the .config file like this:
<add key="SomeFile" value="SomeFolder\SomeFile.xml"/>, I need only relative pathes.
When I run my app manually, 'current folder' = 'application folder', but when I run with Task Scheduler, 'current folder' = C:\Windows\system32, because with Task Scheduler my app runs under taskeng.exe which lies in system32.
So to find assembly path I want to use this code:
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
from this answer https://stackoverflow.com/a/16102640/6184866.
But it doesn't work.
After native compilation, the assembly no longer exists on the file system.
A more recent way to pull the current directory is:
AppContext.BaseDirectory
This works in .net6
I had this same issue also with a console application.
I only received this error when:
The assembly is built as release with optimisations on
The program is run outside of Visual Studio and then the process is attached to
The command Assembly.GetExecutingAssembly().Location is executed via the Immediate Window.
It can be replicated with the following code. The sleep in a loop is just to give me time to attach the debugger.
static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine(10 - i);
Thread.Sleep(1000);
}
Console.WriteLine(Assembly.GetExecutingAssembly().Location);
Console.WriteLine(Directory.GetCurrentDirectory());
Console.ReadLine();
}
Breakpointing this code (when the above constraints are true) and from the Immediate Window executing the command Assembly.GetExecutingAssembly().Location returns <Unknown>. Executing Directory.GetCurrentDirectory() returns Cannot evaluate expression because the code of the current method is optimized.
However, both commands execute and print the expected correct values to the console.
I have a console application,whose target version is .Net Framework 3.5. When I try to trigger the complied exe manually, the program executes and does its job perfectly. But the exe is not at all getting triggered when using a Task Scheduler.
The error details in Task Sceduler is as follows:
last run result 0xE0434F4D
Edits:
The event log is as follows,
Fault bucket , type 0
Event Name: CLR20r3
Response: Not available
Cab Id: 0
Problem signature:
P1: flvtomp4converter.exe
P2: 1.0.0.0
P3: 4ffa8abc
P4: mscorlib
P5: 2.0.0.0
P6: 4e1539fa
P7: 349e
P8: 119
P9: System.IO.DirectoryNotFound
P10:
What is happening here?
Welcome to wonderful world of Windows 2008 family.
This is about under what user your application scheduled to run in Task Scheduler and what permission that specific user have to all(!!!) folders involved in processing by your application.
Also you must make sure that this user authorized to run batch processes in security settings.
Just been member of administrators group no longer is enough!
In my case, it's because my console app trying to create an log.txt file inside C:\Windows\System32 and the task scheduler has no permission to create a new file inside that folder.
This is happen because i don't specify an absolute filename where to put the log file.
Let me explain this with an example of my code :
I have a program inside this directory D:\somefolder\ and the .exe directory is D:\somefolder\program.exe
This is a snapshot of my code (visual basic)
Wrong :
Dim logFileName As String = "log.txt"
/*
Create file based on logFileName directory
*/
If I execute .exe via windows explorer -> it'll create a log file with directory D:\somefolder\log.txt (no problem)
If I execute .exe via task scheduler -> it'll create a log file with directory C:\Windows\System32\log.txt (This will leave last run result 0xE0434F4D)
Correct :
Dim logFileName As String = AppDomain.CurrentDomain.BaseDirectory + "/log.txt"
/*
Create file based on logFileName directory
*/
If I execute .exe via windows explorer -> it'll create a log file with directory D:\somefolder\log.txt (no problem)
If I execute .exe via task scheduler -> it'll create a log file with directory D:\somefolder\log.txt (no problem)
So you need to specify an absolute filename where to put the log file. In my example I use AppDomain.CurrentDomain.BaseDirectory