a simple threading question

E

Elie

Hi all,
I m doing initialization on the startup of my app like loading the
dataset and the forms that contain lots of control, to improve
performance.
Since it's taking long time i decided to display a splash screen that
will run until the initialization is completed

i downloaded the code of the splash screen from msdn:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetco
mp/html/casoast.asp?frame=true

this is the code in the load event handler of the main form:
Dim splashThread As Thread = New Thread(New ThreadStart(AddressOf
StartSplash))
splashThread.Start()

' Pretend that our application is doing a bunch of loading and
' initialization
Thread.Sleep((kMinAmountOfSplashTime_ms / 2))

' Sit and spin while we wait for the minimum timer interval if
' the interval has not already passed
While splash.GetUpMilliseconds() < kMinAmountOfSplashTime_ms
Thread.Sleep((kSplashUpdateInterval_ms / 4))
End While

' Close the splash screen
CloseSplash()

in this sample the splash screen run in another thread for a specific
amount of time and after that it's closed and the main form appears. but
since i don't know how long my initialization will take, i was thinking
of the folowing scenario:
1- main thread starts
2- splash screen thread starts and loop for ever
3- initialization starts on the main thread
4- when initialization finish call the CloseSplash

now I m not sure how to do the 4th step.Can the splash screen thread
wait for a variable in the mainthread to change (and how to do that).
how the main thread will signal to the splash screen thread that
initialization has finished and the splash screen should be closed.
should i use the ManuelResetEvent class , or should i use asynchronous
call with beginInitialize and endInitialize.
I never used these classes before or worked in multi threading
application, so i m wondering what to do.

thank you.
 
D

Darren Shaffer

Elie,

Simply remove the Thread.Sleep line from the MSDN sample code.
It is a just placeholder intended to indicate to you where you should place
YOUR initialization code. So your form load should look like this:

splashThread.Start()
// do your initialization
CloseSplash()

There is no need to put the thread to sleep or to check that the splash
screen has been visible for a minimum amount of time. If your
initialization
takes such a short amount of time that the splash screen isn't displayed
long enough for someone to read it's contents, you can certainly do the
check for minimum display time, but in that situation, you might want
to rethink why you even have a splash screen.

-Darren
 
E

Elie

hi darren,
yea that's very simple, i m gonna do it.

the reason why i didn't thought it work previously is that i thought
that if i loaded the dataset and the forms in the splash screen thread i
won't be able to obtain a reference to them in the main thread.

so if ur suggestion works, this means that different threads can access
the objects in other threads normally (i.e like usual through their
reference). so i was wondering then why i read many times that threads
communicate with each other by messages to obtain objects existing i
other threads ?

regards
 

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