After some more testing, I can get the InvalidOperationError if I set the Scheduled Task to "Run whether user is logged on or not". It errors whether I run it with my user account, a Domain Admin account that is added as a local Admin, or the local Admin account. n If I set it to "Run only when user is logged on" it runs fine.
I found this batch script:
@echo off
echo %date% %time% Start of task > c:\test.log
echo User=%UserName%, Path=%path% >> c:\test.log
c:\Tools\YourTask.exe 1>>c:\test.log 2>c:\test.err
echo ErrorLevel of c:\Tools\YourTask.exe=%ErrorLevel% >> c:\test.log
echo %date% %time% End of task >> c:\test.log
Put that in a .bat file, change the application to yours (replace c:\Tools\YourTask.exe, there are 2 references), modify the task to run the batch file, and it will create a log file and an error file. The error file that mine created has this:
Unhandled Exception: System.InvalidOperationException: Showing a modal dialog box or form when the application is not running in UserInteractive mode is not a valid operation. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application.
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
at TaskSchedulerTest.Module1.main()
So in this case it is indeed what strongm said: there is no desktop (i.e., not running in UserInteractiveMode), so the program cannot display a form.
I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
Arrrr, mateys! Ye needs ta be preparin' yerselves fer
Talk Like a Pirate Day!