Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Email Screen Image From a Sheduled Task 1

Status
Not open for further replies.

Auguy

Programmer
May 1, 2004
1,206
US
Windows Server 2008 R2. I have a .Net app that I want to run from a scheduled task. What I am trying to do is display a form and copy the form image to a file and email it. This works fine from a batch file or manually starting the app. the task bombs out at
Code:
oForm.ShowDialog()
I have code in the form to save the form image, email the image and close the form. From my testing I'm guessing I can't display anything on the screen when running from a scheduled task. Is there any way around this or another method to do this?

Auguy
Sylvania/Toledo Ohio
 
I have been trying and I am unable to replicate the issue. I set up a simple program that runs from Sub Main(), opens one form with ShowDialog, and then that form opens another with ShowDialog. All of that worked with no problem. Could you post the settings of the scheduled task?

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!
 
A scheduled task doesn't always have a desktop to interact with
 
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!
 
Thanks for the thorough investigation. I was afraid this might be the case.

Auguy
Sylvania/Toledo Ohio
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top