Scheduled VB.NEt program

L

LittleRob

I'm having problems using Windows Task scheduler (or AT or SCHTASKS) to
run a VB.NET program unattended.

I'm able to reduce it to some really simple code that still fails

My program has a Sub Main something like

Sub Main()

MainForm.showdialog

End Sub

In MainForm the New() does execute, but code in the MainFormLoad event
doesn't. I then get a System.InvalidOperationException in the Eventlog
for that machine.

Why is this so hard, when VB6 can do this no problem?

Thanks

Rob
 
P

Patrice

On which statements does it fail ?

IMO it is caused more likely by the execution context rather than by some
VB6 vs VB.NET issue. For example an application that would try to use the
printer under a service account could fail as in this case the user profile
is not loaded and sop there are no default printers available...
 
L

LittleRob

Patrice said:
On which statements does it fail ?

IMO it is caused more likely by the execution context rather than by some
VB6 vs VB.NET issue. For example an application that would try to use the
printer under a service account could fail as in this case the user profile
is not loaded and sop there are no default printers available...
Patrice

Thanks for the reply, but it doesn't fail on any of my code.

I added some logging:

1. Logging in the Sub Main() is OK

2. Logging in the form's New() is OK

3. Logging in the FormLoad event never gets executed.

It seems to me that I just can't show a form (using .ShowDialog) if the
code is to run from a scheduler, but I can't be the only person out
there with this problem. Well actually I know I'm not as I do get some
hits when I Google, but only a few, and never with a workaround.

Thanks

Rob
 
A

Armin Zingler

LittleRob said:
I'm having problems using Windows Task scheduler (or AT or SCHTASKS)
to run a VB.NET program unattended.

I'm able to reduce it to some really simple code that still fails

My program has a Sub Main something like

Sub Main()

MainForm.showdialog

End Sub

In MainForm the New() does execute, but code in the MainFormLoad
event doesn't. I then get a System.InvalidOperationException in the
Eventlog for that machine.

Why is this so hard, when VB6 can do this no problem?


I tried it and it works without an error. Are you logged-in when the task
starts? If you are not, it is not possible. You'd have to write a service
w/o a UI.


Armin
 
P

Phill W.

LittleRob said:
I'm having problems using Windows Task scheduler (or AT or SCHTASKS) to
run a VB.NET program unattended.
Sub Main()
MainForm.showdialog
End Sub

Using ShowDialog on your /main/ form is a Bad Idea. Use ...

Application.Run( New MainForm )

.... instead.

Without this, your form has no Windows Message loop to run in and so it
ends just as soon as it starts (which is probably why the Form's
constructor (sub New) is getting called but the Form is never loaded or
shown.

Also, if your application is running "unattended", why is it showing a
Form (that no-one will be able to see or interact with)?

The Task Scheduler is a Windows Service which, generally speaking,
cannot display anything on screen. Indeed, showing a /dialog/
(typically a MsgBox) from a service /usually/ gets intercepted by
Windows and written to the Event Log instead!

HTH,
Phill W.
 
P

Patrice

And what are the task parameters ? Do you have an open session when this
tasks runs ? Under which account does it run ?

Basially the idea is that if you don't have any active desktop then the
application can't show a form... I'm not sure if a scheduled tasks with a ui
should be shown in the current user desktop if one is available ? You could
try using notepad for example to see how it reacts...

If you want to run a UI without any desktop this is just not possible. You
may want to explain what is the exact purpose of this application...
 
L

LittleRob

Patrice said:
And what are the task parameters ? Do you have an open session when this
tasks runs ? Under which account does it run ?

Basially the idea is that if you don't have any active desktop then the
application can't show a form... I'm not sure if a scheduled tasks with a ui
should be shown in the current user desktop if one is available ? You could
try using notepad for example to see how it reacts...

If you want to run a UI without any desktop this is just not possible. You
may want to explain what is the exact purpose of this application...
Patrice (and everyone else)

Thanks for your reply.

I understand that showing a form during an unattended session seems a
little odd, but my point is that I want to be able to run the same code
whether logged in or not. If I run my program interactively then I see a
progressbar and some IO. If I'm not logged in then that goes off into
the aether, but the program should still run.

I have re-written the code to run differently when I pass it a
commandline parameter its just a shame I need to do this.

The other issue is that VB6 could do this with no problems. So it seems
a backwards step that I have to code around this.

Rob
 
L

LittleRob

Armin said:
I tried it and it works without an error. Are you logged-in when the task
starts? If you are not, it is not possible. You'd have to write a
service w/o a UI.


Armin
Armin

No I'm not logged in when it runs. I'm not even in the same postcode, if
you see my reply to Patrice you'll see why I'm a bit confused, and
disappointed in VB.NET

Thanks

Rob
 
L

LittleRob

Phill said:
Using ShowDialog on your /main/ form is a Bad Idea. Use ...

Application.Run( New MainForm )

... instead.

Without this, your form has no Windows Message loop to run in and so it
ends just as soon as it starts (which is probably why the Form's
constructor (sub New) is getting called but the Form is never loaded or
shown.

Also, if your application is running "unattended", why is it showing a
Form (that no-one will be able to see or interact with)?

The Task Scheduler is a Windows Service which, generally speaking,
cannot display anything on screen. Indeed, showing a /dialog/
(typically a MsgBox) from a service /usually/ gets intercepted by
Windows and written to the Event Log instead!

HTH,
Phill W.
Phil

Thanks for taking the time to reply. If you see my reply to Patrice
you'll see what I'm trying to achieve. It seems odd that this was so
easy in VB6 and so hard in VB.NET

Thanks

Rob
 
A

Armin Zingler

LittleRob said:
Patrice (and everyone else)

Thanks for your reply.

I understand that showing a form during an unattended session seems
a little odd, but my point is that I want to be able to run the same
code whether logged in or not.

Why ignore the circumstances? Showing a Form without being logged in doesn't
make sense.
If I run my program interactively
then I see a progressbar and some IO. If I'm not logged in then that
goes off into the aether, but the program should still run.

I have re-written the code to run differently when I pass it a
commandline parameter its just a shame I need to do this.

No, it's a good solution.
The other issue is that VB6 could do this with no problems. So it
seems a backwards step that I have to code around this.


Maybe VB6 was more tolerant with this error and still continued without
terminating. My taskplanner shows a return error 0xc0000142 with the VB6
application, so it's not really ok. In addition, after logging in, where's
the application? The process is still running but I have no interface.

In general, you will have many points that might have been simpler in VB6
but not always better.


Armin
 
L

LittleRob

Armin said:
No, it's a good solution.
Armin

I totally disagree with you. Having 2 pathways through the code and
needing 2 shortcuts installed on site is a recipe for disaster. What
would have been a good solution is the VB6 way of doing things.

So what if I display a form that nobody sees. It might look odd, but
does it matter?

Already today I ran interactively using the wrong method and of course I
sat there waiting for something to happen, which it was - invisibly.

I wonder if there is a way to tell in code what is going on.

Rob
 
A

Andrew Morton

Could you write a separate program for the user interface? You could have
the UI program start the processing program.

Andrew
 
A

Armin Zingler

LittleRob said:
Armin

I totally disagree with you. Having 2 pathways through the code and
needing 2 shortcuts

Why 2 shortcuts? In you app, check Environment.UserInteractive. Make your
app behave minding the circumstances.
installed on site is a recipe for disaster. What
would have been a good solution is the VB6 way of doing things.
So what if I display a form that nobody sees.

Why show a Form that nobody sees?
It might look odd, but does it matter?

I remember an awesome basic discussion about that...

Some say: "Why not do something doesn't make sense but that doesn' hurt?"
I say: "Why do something that doesn't has to be done, even if it doesn't
hurt?"

Already today I ran interactively using the wrong method and of
course I sat there waiting for something to happen, which it was -
invisibly.

Sorry, I don't get this.
I wonder if there is a way to tell in code what is going on.



Armin
 
L

LittleRob

Armin said:
I remember an awesome basic discussion about that...

Some say: "Why not do something doesn't make sense but that doesn' hurt?"
I say: "Why do something that doesn't has to be done, even if it doesn't
hurt?"

Armin

Well Imagine you and I are in a room together and you are counting out
loud. If I leave the room do you *have to* stop talking out loud (but
continue counting) just because I'm not there?

At the end of the day I *have* fixed my code so that it runs without any
UI. It just seems a shame that I need to do this on a program that may,
or may not have somebody watching it.

If I'd set out to write a service then clearly all discussion of "what's
the point..." would have been valid.

Environment.UserInteractive

Didn't know that one. I'll try it out.

Thanks for the help. I have to say developing this stuff is a pain
because I have to keep scheduling a task, logging out, and twiddling my
fingers. Its *really* boring !

Rob
 
B

Bryan

I have been reading you alls post waiting for a work around but all I see is "why show the ui?", so here is my answer. I have the exact problem and I have to have a ui when no one sees it because I get a bunch of data from the db and manipulate it and then using that draw a graph on the winform. Then converting the form(frame) to a bitmap and saving it on the hard drive. Then I send out a email with the .bmp attached. This works perfect while I am logged in but when I am not it does not draw so it does not save so it does not send it as an attachment. So now that I have answered why you would need a winform when no one can see it, can anyone provide me with a work around, I would appreciate it.
 
A

Armin Zingler

I thought I've sent this already earlier...



LittleRob said:
Armin

Well Imagine you and I are in a room together and you are counting
out loud. If I leave the room do you *have to* stop talking out loud
(but continue counting) just because I'm not there?

Yes, because I mustn't talk if you're not there. These are the rules for the
room that you are currently ignoring. Look at the docs
(Environment.UserInteractive property):

"If this property is false, do not display modal dialogs or message boxes
because there is no graphical user interface for the user to interact with."


The plattform SDK clearly states:

"The interactive window station, Winsta0, is the only window station that
can display a user interface or receive user input. It is assigned to the
logon session of the interactive user, and contains the keyboard, mouse, and
display device."

http://msdn2.microsoft.com/en-us/library/ms687096.aspx

I must admit I'm not a specialist in this, so you'd have to read it up on
your own. Maybe the link is a starting point.

At the end of the day I *have* fixed my code so that it runs without
any UI. It just seems a shame that I need to do this on a program
that may, or may not have somebody watching it.

If I'd set out to write a service then clearly all discussion of
"what's the point..." would have been valid.

Environment.UserInteractive

Didn't know that one. I'll try it out.

Thanks for the help. I have to say developing this stuff is a pain
because I have to keep scheduling a task, logging out, and twiddling
my fingers. Its *really* boring !

I don't understand. What's the problem? You only have to not show the Form
if no user is logged in.


Armin
 
B

Bryan

Draw your graph directly to a
How do I draw directly to a bitmap.
I am using GDI+ and to my understanding it is used to draw on
the form, which then can be saved
to a bitmap. If this is not true,
how do you write directly to a
bitmap. I am currently doing it
in the OnPaint() but since it runs
when I am not logged it in never runs the OnPaint(). Here is my
code sample:
protected override void OnPaint(PaintEventArgs pe)
{
Graphics graph = pe.Graphics;
Bitmap bmp =
new Bitmap(this.Size.Width,
this.Size.Height);
graph = Graphics.FromImage(bmp);
frameView.Image = bmp;
frameView.Refresh();
bmp.Save(strDir);
}

Any help would be appreciated!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top