1

Is there any way to find out, if an console-application (VB.net) was started by a user or the windows scheduler.

There´s a very similar thread, but no result: http://www.44342.com/visual-basic-f948-t16091-p1.htm

1 Answers1

1

I don't know what you are trying to achieve but wouldn't it be easiest to launch the application with some argument when running as a scheduled task?

If you don't want to use argument you can base the detection on information about parent process. E.g. name of the parent process will be 'svchost' when running from scheduler.

if(System.Diagnostics.Process.GetCurrentProcess().Parent().ProcessName == "svchost")
{ 
    // Run once
}
else
{
    // Loop
}
Community
  • 1
  • 1
rocky
  • 7,506
  • 3
  • 33
  • 48
  • And what are you looking for? Why is looking at the parent process not sufficient for you? – rocky Nov 22 '14 at 23:54
  • That´s not what i´m looking for. Background: The application loops when started by a user, but should not loop and only run once when started by scheduler, because the scheduler hat it´s own interval / trigger. Im looking for a SIMPLE and SOLID way, to find out whether it´s been started by user or scheduler. – Sascha Bömken Nov 23 '14 at 00:05
  • I've modified my answer and I still think that the originally proposed solution is pretty reliable. – rocky Nov 23 '14 at 00:11
  • (The Parent() method can be found on the page linked in the second paragraph.) – rocky Nov 23 '14 at 00:15
  • That´s C#? I use VB.NET. So i changed it to "System.Diagnostics.Process.GetCurrentProcess().ProcessName". While the scheduler is running, the function returns the username. I watched the taskmanager while the scheduler is working, but it doesn´t show a "svhost"-process... *frustrated* – Sascha Bömken Nov 23 '14 at 00:35
  • Please read my answer properly. It's essential that you use information from PARENT process. To do that, read the article I linked within my post. For conversion C# to VB.NET use http://www.developerfusion.com/tools/convert/csharp-to-vb/ – rocky Nov 23 '14 at 00:42
  • If you need to start debugger in the "scheduled" mode. Use Debugger.Launch() in your code. – rocky Nov 23 '14 at 00:45
  • You´re right @rocky. Sorry. I haven´t noticed the link in your post when i read it first. I already tried to convert that using developerfusion.com in the meanwhile, but it doesn´t work. i get two errors in visual studio. from one problem to three others... ;) – Sascha Bömken Nov 23 '14 at 00:48
  • Shouldn't be that hard ;) There are no special constructs aside from extension method. (http://msdn.microsoft.com/en-us/library/bb384936.aspx) – rocky Nov 23 '14 at 00:53
  • Ohhh... that´s is too much for my head and this time of day. I will go on "puzzle" tomorrow. Many thanks for your help up to this point Petr! – Sascha Bömken Nov 23 '14 at 00:57