By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,247 Members | 2,145 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,247 IT Pros & Developers. It's quick & easy.

Scheduled VB.NEt program

P: n/a
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
Dec 5 '07 #1
Share this Question
Share on Google+
17 Replies


P: n/a
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...
"LittleRob" <gr*************@greymouse.co.uka écrit dans le message de
news: 5r*************@mid.individual.net...
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

Dec 5 '07 #2

P: n/a
Patrice wrote:
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...
"LittleRob" <gr*************@greymouse.co.uka écrit dans le message de
news: 5r*************@mid.individual.net...
>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

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
Dec 5 '07 #3

P: n/a
"LittleRob" <gr*************@greymouse.co.ukschrieb
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

Dec 5 '07 #4

P: n/a
LittleRob wrote:
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.
Dec 5 '07 #5

P: n/a
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

"LittleRob" <gr*************@greymouse.co.uka écrit dans le message de
news: 5r*************@mid.individual.net...
Patrice wrote:
>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...
"LittleRob" <gr*************@greymouse.co.uka écrit dans le message de
news: 5r*************@mid.individual.net...
>>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

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

Dec 5 '07 #6

P: n/a
Patrice wrote:
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

"LittleRob" <gr*************@greymouse.co.uka écrit dans le message de
news: 5r*************@mid.individual.net...
>Patrice wrote:
>>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...
"LittleRob" <gr*************@greymouse.co.uka écrit dans le message de
news: 5r*************@mid.individual.net...
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
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

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
Dec 5 '07 #7

P: n/a
Armin Zingler wrote:
"LittleRob" <gr*************@greymouse.co.ukschrieb
>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
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
Dec 5 '07 #8

P: n/a
Phill W. wrote:
LittleRob wrote:
>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.
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
Dec 5 '07 #9

P: n/a
Armin Zingler wrote:
>
>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.
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
Dec 6 '07 #10

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

Andrew
Dec 6 '07 #11

P: n/a
Armin Zingler wrote:
>
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

Dec 6 '07 #12

P: n/a
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.
Dec 7 '07 #13

P: n/a
Draw your graph directly to a bitmap.
"Bryan" wrote in message news:u6**************@TK2MSFTNGP04.phx.gbl...
>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.
Dec 7 '07 #14

P: n/a
I thought I've sent this already earlier...

"LittleRob" <gr*************@greymouse.co.ukschrieb
Armin Zingler wrote:

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?
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

Dec 7 '07 #15

P: n/a
>Draw your graph directly to a
>bitmap.
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!!!
Dec 10 '07 #16

P: n/a
<Bryanschrieb
Draw your graph directly to a
bitmap.

How do I draw directly to a bitmap.
Create a graphics object by calling Graphics.FromImage.

see also:
http://msdn2.microsoft.com/en-us/lib...07(VS.90).aspx

Don't forget to dispose the Graphics object (like in the example).

Armin
Dec 11 '07 #17

P: n/a
>>Create a graphics object by calling >>Graphics.FromImage.
>>see also:
http://msdn2.microsoft.com/en-us/lib...07(VS.90).aspx
>>Don't forget to dispose the >>Graphics object (like in the example).
Thank you...
Dec 11 '07 #18

This discussion thread is closed

Replies have been disabled for this discussion.