0
votes

I also checked previous questions on Stack Overflow before posting this.

Here is my problem:

I created a Windows Console Application on Visual Studio 2017. This application is supposed to fetch some data from tables and send email at regular intervals. Initially, I created a Windows Service and it is working fine but later I decided to execute this from Task Scheduler as changing schedules would be easy.

The MAIN code is as below:

public static void Main(string[] args)
{
   RetrieveData();
}

stativ void RetrieveData()
{
    // Fetch data and send mails
}

There are two problems:

(1) I am not able to attach it to a process. First the issue is that as I start it in debugging mode, the application exits as soon as it hits Main when trying to Attach to process.

(2) The .exe name is not appearing under process names when I click "Attach to Process".

How to debug this application when it is executed from a task scheduler? It is not even executing when Task Scheduler triggers this job.

1
What exactly happens when you open the project in VS like a regular console app and click Start Debugging? Does RetrieveData do synchronous or asynchronous work? - 41686d6564
Why are you trying to debug it on scheduler instead of running a console application using visual studio? - Priyank Panchal
@AhmedAbdelhameed No async calls. Just a simple void method. - RKh
You can put some Console.WriteLines in your application and redirect the output to a .txt file using >> operator in scheduler by using "Add arguments (optional)" textbox - Priyank Panchal
@RKh 1) You didn't answer my first question. 2) A "simple void method" can contain asynchronous work without having an async signature. That's one reason for an application to terminate before executing some code (which you seem to be saying in point #1 and that's why I asked you to confirm if that's what happens when you run the application from Visual Studio). - 41686d6564

1 Answers

1
votes

Try to use Debugger.Launch().

Put this line in your first line in the main, hold Visual Studio open in this project, compile your project, schedule a task to run your new compiled executable, and wait for your program will ask you to open Visual Studio to debug.