macemers
macemers

Reputation: 2222

Task Scheduler can't run console .net program correctly

My machine: Win7 32bit, .net 4

I have a console program based on .net 4 that generate 3 excel files and send them one by one through SMTP Server.

When I double click the exe or run in cmd, it runs correctly and sent 3 emails with these 3 excel files.

But when I put the exe in the Task Scheduler, and click "Run". Only the first email was sent and the other 2 emails were somehow can't be sent.

Any idea?

Upvotes: 0

Views: 3615

Answers (2)

Avi Pawar
Avi Pawar

Reputation: 1

Problem: Code a .net C# Console Application to upload documents from a share drive to SharePoint document library. When logged in as Service Acct, no issues were found while executing, the files transfered into SP document library without issues. However, executing via Task Scheduler ran into "system.io.directorynotfoundexception...." error. Also note using personal account I had no issues executing the code while logged in on the server or via Task Scheduler executing the .exe using my personal account.

Analysis: When Service Acct. is logged in the mapped drive in the code was accessible. However Task scheduler could not read the mapped drive (namely s:\xxxxx Reports\).

Resolution: Noted that files accessed was from a EMC VNX Windows File Sharing. Changed the consoleapplication code referencing the document location to the following path \server.com\vnxcifs01\Saved_xxxx_Report_Outputs\xxxxxxxxxxx Scheduled Reports. After this change Rebuilt the exe and the Task Scheduler worked like a charm.

I hope this helps some one working on a similar envrionment.

Upvotes: 0

Peter
Peter

Reputation: 14508

To really know what is going on when the program is run by the Task Scheduler you can put Trace statements at vital steps in your code such as:

Trace.WriteLine("Start creating mail object");

and

Trace.WriteLine(string.Format("Exception occured: {0}\r\n{1}", ex.Message, ex.StackTrace);

Don't forget to put a Trace.WriteLine inside your exception handler so you know if something goes wrong.

You take control over where these statements are written by configuring listeners inside your .config file.

The following example adds an EventLogTraceListener object named myListener to the Trace.Listeners collection. The initializeData parameter specifies the name of the event log source that is to be passed to the EventLogTraceListener(String) constructor.

<configuration>
  <system.diagnostics>
    <trace autoflush="false" indentsize="4">
      <listeners>
        <add name="myListener"
          type="System.Diagnostics.EventLogTraceListener"
          initializeData="TraceListenerLog" />
      </listeners>
    </trace>
  </system.diagnostics>
</configuration>

You can open the EventViewer by going to Start > eventvwr

Hope this helps you to figure out what the problem is because at this point it's anyone's guess.

Upvotes: 4

Related Questions