Q: Threads and stuff

  • Thread starter Thread starter Geoff Jones
  • Start date Start date
G

Geoff Jones

Hi

Can anybody help me with the following?

I have a main application, called for the sake of argument Form1, which is
going to be the main source of my application code. I've been trying to
develop a splash screen to appear before the main application appears i.e.
Form1. This splash screen I am going to call Form2.

So, what I'm trying to do is start the application, display Form2 and then
when it closes, display the main form i.e. Form1.

I've searched the web and found a few bright ideas but being a bit of a
novice in VB I'm struggling a bit and a lot of what I've done is guesswork.

Could anybody give me some suggestions?

Geoff
 
Geoff,

The most simple one is in the *load* event of your mainform (in the load
event the form is not yet showed)

dim frm as new form2
dim frm2.showdialog
frm2.dispose

And than set in your form2 a timer which runs by instance 10 times
decreasing the opacity ten times with 10 and when it is done 10 times (or
the opacity is 0) say in that timer event me.close

Looks greath however in this way your program is of course waiting for your
splash screen to be ready.

When you have not really big start in processing that does not matter,
because 1 second to show is nothing while in 1 second mostly every startup
process is done by a modern computer, when you want to avoid that you can
use multithreading, however I find that a lot of work when it is for 1
second.

I hope this gives some ideas and when not feel free to ask?

Cor
 
Thanks Cor.

That works apart from Form2 appearing before Form1. Is there workround for
that?

By the way, why does this not work for:

dim frm as new form2
frm2.show ' i.e. use show() instead of showdialog()
frm2.dispose

Geoff
 
Geoff,

Please tell me what I don't understand beneath?

Your question
I've been trying to develop a splash screen to appear before the main
application appears i.e. Form1.

Your answer on my solution
That works apart from Form2 appearing before Form1. Is there workround for
that?
dim frm as new form2
frm2.show ' i.e. use show() instead of showdialog()
frm2.dispose
Probably this works however are your eyes not fast enough to see that frm2.

:-)

Cor
 
Hi Cor

Sorry, you're right. I'd obviously changed my strategy in mid-flow i.e. I'd
subsequently thought about shown Form1 first and then Form2.

As a matter of interest, I did it thus:

Me.Show()
frm2.ShowDialog()
frm2.Dispose()

Seems to work but knowing me I'll probably run into problems :)

Geoff
 
Geoff,

Now you show with that me.show your first form and becomes it not so nice in
my opinion.

Than I would take this one (I made it some minutes ago because I found my
other sample to difficult)

\\\Form1 needs 2 labels
Private WithEvents frm1 As Form2
Private Delegate Sub Frm1Handler(ByVal message As String)
Private MyThread As System.Threading.Thread
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim timer1 As New System.Windows.Forms.Timer
AddHandler timer1.Tick, AddressOf mytimer1
Label1.Text = "0"
Label2.Text = "Do you see me?"
timer1.Enabled = True
timer1.Interval = 400
Dim timer2 As New System.Windows.Forms.Timer
frm1 = New Form2
frm1.Owner = Me
frm1.Width = Me.Width - 40
frm1.Height = Me.Height - 40
frm1.Left = Me.Left + 20
frm1.Top = Me.Top + 20
AddHandler frm1.ready, AddressOf Frm1Ready
MyThread = New System.Threading.Thread(AddressOf frm1.Show)
MyThread.Start()
End Sub
Private Sub mytimer1(ByVal sender As Object, _
ByVal e As System.EventArgs)
Label1.Text = (CInt(Label1.Text) + 1).ToString
DirectCast(sender, System.Windows.Forms.Timer).Enabled = True
End Sub
Private Sub Frm1Ready(ByVal message As String)
Me.BeginInvoke(New Frm1Handler(AddressOf Frm1HandlerSub), New
Object() {message})
End Sub
Private Sub Frm1HandlerSub(ByVal message As String)
frm1.Close()
Label2.Text = message
MyThread.Abort()
End Sub
Private Sub Form1_Closing(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) _
Handles MyBase.Closing
If Not MyThread Is Nothing Then
MyThread.Abort()
End If
End Sub
///
\\\Form 2 needs 1 label
Friend Event ready(ByVal message As String)
Private Sub Form2_Activated(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Activated
Dim timenext As DateTime = Now.Add(TimeSpan.FromSeconds(10))
Do While timenext > Now
Label1.Text = Now.TimeOfDay.ToString
Application.DoEvents() 'to show the time
Threading.Thread.Sleep(50)
Me.Opacity -= 0.004
Loop
RaiseEvent ready(Now.TimeOfDay.ToString)
End Sub
Private Sub Form2_Closing(ByVal sender As Object, ByVal _
e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
RaiseEvent ready(Now.TimeOfDay.ToString)
End Sub
///

I think than this one is nicer, give it a try

I hope this helps?

Cor
 
Hi Cor

Wow - thanks for the code. It uses delegates, which I have only just started
reading about but I will certainly go through your example to see how it
works. Best way to learn in my opinion!

As a matter of interest, why is the me.show within the Load member
undesirable?

Geoff
 
Geoff
As a matter of interest, why is the me.show within the Load member
undesirable?
It freezes and that has on that place no sense, than I prefer it in a splash
situation not to show it.

Think by instance that this is a login screen. The users sees nothing than
when he has the wrong password. By instance with this code.

dim frm as new loginform
frm.showdialog
if frm.passwordcheck is false then me.close
'this above can much nicer however for the sample.

The user does not even see the program than.

However just my idea, I hope it makes sense?

Cor
 
I see. Yes, I see your point; although I'm not sure what you mean by
"freeze"

Here is the code from both forms - Form1 being the main application window:

'This is Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.Show()
Dim frm As New Form2
frm.ShowDialog()
frm.Dispose()
End Sub
+++++++++++++++++++++++++++++++++++++++++++
'This is Form2
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
Me.Close()
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Timer1.Start() ' Interval set to something "large" so we can see Form2 for a
bit
End Sub

It works, but I'm going to go through your example to learn about threads
(gulp!) and delegates (double gulp!) LOL. Has to be done at some point!!!

Thanks for your continuing help.

Geoff
 
Hi Cor

Just thought I'd let you know I have been through your code and am very
impressed. Some very good ideas.

Thanks

Geoff
 
You can set Form2 as the StartUp form or add a module to the project and
within it add a Shared Sub Main method. Inside the Sub Main method, add
a statement Application.Run(new Form2()) and set the start up object in
the project's properties to Sub Main.

In Form2, add a timer. In the timer_tick event procedure (after setting
a decent interval in the timer's properties), add a few statements,

sub timer1_tick
Me.Hide
Dim f As New Form1() and
f.Show()
end sub

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 

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

Back
Top