How to get code to run w/o user click?

  • Thread starter Thread starter Tom Bower
  • Start date Start date
T

Tom Bower

(VB newbie, no flames please)

I have a simple one-form VB application which I want, on
invocation, to start up, initialize and display the form
(the UI), and then go execute some more code (a loop that
checks for certain things etc.).

Where would I put this "more code" -- now, I can make it
do whatever I want in response to a standard UI event
(like user clicks button) but I want the form to load and
initialize, then go off and do this "more code" w/o the
user having to make any clicks.

Is there an event (form initialized?) I can tie this to?
I see a Form1_Load Sub that I can use to initialize some
things, but once the form is loaded what can be used to
trigger further code w/o explicit user interaction?

Thanks,

Tom.
 
Tom Bower said:
(VB newbie, no flames please)

Ah shucks, okay :-)
Where would I put this "more code"

What sort of "more code" are you hoping to run? Lots of stuff can be done
at load time. Do you mean you want it to run in the background while
keeping an eye on the controls or just that whatever it is doing needs to be
visible to the user until it finishes?
Thanks,
Tom.

Your welcome,
Tom
 
* "Tom Bower said:
I have a simple one-form VB application which I want, on
invocation, to start up, initialize and display the form
(the UI), and then go execute some more code (a loop that
checks for certain things etc.).

Where would I put this "more code" -- now, I can make it
do whatever I want in response to a standard UI event
(like user clicks button) but I want the form to load and
initialize, then go off and do this "more code" w/o the
user having to make any clicks.

Is there an event (form initialized?) I can tie this to?
I see a Form1_Load Sub that I can use to initialize some
things, but once the form is loaded what can be used to
trigger further code w/o explicit user interaction?

You will have to start the execution in a new thread (keyword:
"multithreading"). Have a look for this topic in the documentation --
if you have any questions, feel free to post them.
 
What sort of "more code" are you hoping to run? Lots of
stuff can be done at load time.
Do you mean you want it to run in the background while
keeping an eye on the controls or just that whatever it
is doing needs to be visible to the user until it
finishes?

Well, it's the latter, basically. This "more code" is a
loop, looking for a certain condition to be true, waiting
to take action. When I put it in the Form1_Load sub, the
form itself never showed up on the screen.

I want the form to show up, then have this additional
code loop to monitor the condition it needs to look for
every so often. Does that make sense? Thanks for your
help, I'll also have a look at multithreading but that's
a new area for me!
 
Tom Bower said:
Well, it's the latter, basically. This "more code" is a
loop, looking for a certain condition to be true, waiting
to take action. When I put it in the Form1_Load sub, the
form itself never showed up on the screen.

I want the form to show up, then have this additional
code loop to monitor the condition it needs to look for
every so often. Does that make sense? Thanks for your
help, I'll also have a look at multithreading but that's
a new area for me!

That turns out to be the former actually. You want it to run in the
background while the main app continues to operate. So yes you will have to
have the routine run as another thread. The reason (as you've probably
figured out) that your form never appears is that the code that is executing
is your loop so it never has a chance to continue on and do the "normal"
things.

Tom
 
Hi Tom,

You mean something as this?
\\\\
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.Show()
Threading.Thread.Sleep(10000)
////

The threading.thread.sleep is just an example to show that it stops and go
on.

I hope this helps?

Cor
 
If you want to execute a code as soon as a form is
visible, just do the following:
1. In your Form1_Activate sub of Form1:
if you have some code here, after that write:
me.show
me.refresh
Call MoreCode ' the "more code" that you want to call
and execute.
2. In the Form's code, write the following:
' your "more code" starts here....
Public Sub MoreCode()
Dim condition As Boolean

Do While condition = False
'... determine your condition here ....
'... and
'... set "condition = True" when your want to return.
'
If condition = True Then
Exit Do
End If
Loop
End Sub
' ... your "more code" ends here.
This will ensue that your form is visible, while
the "MoreCode" sub (it can be a Function also) loops
until your "condition" becomes "true".
Hope this works for your requirement.
IRFAN.
 
Tom Bower said:
(VB newbie, no flames please)

I have a simple one-form VB application which I want, on
invocation, to start up, initialize and display the form
(the UI), and then go execute some more code (a loop that
checks for certain things etc.).

Where would I put this "more code" -- now, I can make it
do whatever I want in response to a standard UI event
(like user clicks button) but I want the form to load and
initialize, then go off and do this "more code" w/o the
user having to make any clicks.

Is there an event (form initialized?) I can tie this to?
I see a Form1_Load Sub that I can use to initialize some
things, but once the form is loaded what can be used to
trigger further code w/o explicit user interaction?

I think the main question (I have) is whether your only problem is to start
the "more code" directly after the form is shown, or whether you also want
that the user can use the Form while "more code" runs.

If it is only the former:

dim f as new form1

f.show
f.refresh
<more code>


If Form1 is the startup object (set in the project properties), you have to
write your own sub main:

shared sub main 'drop "shared" when in a module
dim f as form1

f = new form1
f.show
f.refresh
<more code>
application.run(f) 'drop this line if app should quit here
end sub

If you also want to keep the Form interact with the user, you've already
gotten Herfried's suggestion to execute it in a new thread.
 
Tom Bower said:
(VB newbie, no flames please)

I have a simple one-form VB application which I want, on
invocation, to start up, initialize and display the form
(the UI), and then go execute some more code (a loop that
checks for certain things etc.).

Where would I put this "more code" -- now, I can make it
do whatever I want in response to a standard UI event
(like user clicks button) but I want the form to load and
initialize, then go off and do this "more code" w/o the
user having to make any clicks.

Is there an event (form initialized?) I can tie this to?
I see a Form1_Load Sub that I can use to initialize some
things, but once the form is loaded what can be used to
trigger further code w/o explicit user interaction?

Thanks,

Tom.

Tom, easiest way is to call another Sub Procedure from the

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
' Which runs as soon as your form loads all of its contents
' and where Form1 is the name of your form

' -- do some code
Call MoreCode()

End Sub


Private Sub MoreCode()
' Put more code here
End Sub
 
Tom,

Just curious, why not use an event instead of a loop? I know it isn't
always possible, but when doable it is certainly more elegant.
 
Back
Top