Creating New Threads to Show a Splash and Login Screen

G

Gaz

I am having a bit of a problem getting my application to work
properly.

RIght here is my problem...

WHen my C# windows app loads up the start form, i create a new thread
and show the splash on the new thread and put the main thread to sleep
until the splash screen has done the business, then i kill the new
thread and start another to show the login and again put the main one
to sleep. Problem i have is that my splash screen will show ie has
focus but when my login box appears it is minimised to the taskbar??
and doesnt show up on the screeen like i want it to.

Here is my Code:

//Load the Splash Screen
private void DoSplash()
{
Splash sp = new Splash();
DialogResult Res;

Res = sp.ShowDialog();
if (Res == DialogResult.Abort)
{
KillApp = true;
}

WakeThread = true;

}

//Here is my Login Form
private void ShowLogin()
{
Login lg = new Login();
DialogResult Res;

Res = lg.ShowDialog();
if (Res != DialogResult.OK)
{
KillApp = true;
}

WakeThread = true;

}

and finally my initial form constructor



//Show Splash Screen
Thread th = new Thread(new ThreadStart(DoSplash));
th.Start();
while (WakeThread == false)
{ Thread.Sleep(1000); }

th.Abort();

if (KillApp)
{ KillApplication(); }
//end of splash screen


//Show Login Box
WakeThread = false;
Thread thLogin = new Thread(new ThreadStart(ShowLogin));
thLogin.Start();
while (WakeThread == false)
{ Thread.Sleep(1000); }

thLogin.Abort();

if (KillApp)
{ KillApplication(); }
//end of Login

//GOOD TO GO.....
InitializeComponent();


Can anyone offer a suggestion to what i am doing wrong or if someone
has a better way of doing what im trying to then please let me know :)

Thanks
 
C

Cor Ligthert[MVP]

Gaz,
WHen my C# windows app loads up the start form, i create a new thread
and show the splash on the new thread and put the main thread to sleep
until the splash screen has done the business


What is the senze of your solution, this you can do with a simple.

\\\
Form splasy = new Form();
//Set what you want on splasy
splasy.ShowDialog();
splazy.Dispose();
///

The effect is the same.

The meaning of multithreading with a splash screen is to do initialization
operations while the splash screen is showed, not to stop it.

Cor
 
I

Ignacio Machin ( .NET/ C# MVP )

Can anyone offer a suggestion to what i am doing wrong or if someone
has a better way of doing what im trying to then please let me know :)

Check the archives for "Ignacio machin" "splash screen"
I have posted code for doing this exactly.
 
P

parez

I am having a bit of a problem getting my application to work
properly.

RIght here is my problem...

WHen my C# windows app loads up the start form, i create a new thread
and show thesplashon the new thread and put the main thread to sleep
until thesplashscreen has done the business, then i kill the new
thread and start another to show the login and again put the main one
to sleep. Problem i have is that mysplashscreen will show ie has
focus but when my login box appears it is minimised to the taskbar??
and doesnt show up on the screeen like i want it to.

Here is my Code:

//Load theSplashScreen
private void DoSplash()
{
Splashsp = newSplash();
DialogResult Res;

Res = sp.ShowDialog();
if (Res == DialogResult.Abort)
{
KillApp = true;
}

WakeThread = true;

}

//Here is my Login Form
private void ShowLogin()
{
Login lg = new Login();
DialogResult Res;

Res = lg.ShowDialog();
if (Res != DialogResult.OK)
{
KillApp = true;
}

WakeThread = true;

}

and finally my initial form constructor

//ShowSplashScreen
Thread th = new Thread(new ThreadStart(DoSplash));
th.Start();
while (WakeThread == false)
{ Thread.Sleep(1000); }

th.Abort();

if (KillApp)
{ KillApplication(); }
//end ofsplashscreen

//Show Login Box
WakeThread = false;
Thread thLogin = new Thread(new ThreadStart(ShowLogin));
thLogin.Start();
while (WakeThread == false)
{ Thread.Sleep(1000); }

thLogin.Abort();

if (KillApp)
{ KillApplication(); }
//end of Login

//GOOD TO GO.....
InitializeComponent();

Can anyone offer a suggestion to what i am doing wrong or if someone
has a better way of doing what im trying to then please let me know :)

Thanks

This is what i have done..


// I do the login first and then load if necessary

{
splashForm = new SplashForm();
SplashForm.CheckForIllegalCrossThreadCalls = false;

BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += new
DoWorkEventHandler(worker_DoWork);

worker.RunWorkerAsync();

////////////

//Do Work Here

////////////////

splashForm.Close();

}

void worker_DoWork(object sender, DoWorkEventArgs e)
{
splashForm.ShowDialog();
}
 
J

Joachim Van den Bogaert

Hi,
        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            splashForm.ShowDialog();
        }- Hide quoted text -

You are actually doing the work in the first thread, and just showing
the splashForm in the worker thread.
Wouldn't it make sense to do it the other way around?

You could show the splash screen, do the work in the worker thread,
wait for the work to end and then
define a RunWorkerCompletedEventHandler to close the splash screen.

This will require a cross-thread operation I think, but you can
achieve this by
following this method:

http://msdn.microsoft.com/en-us/library/ms171728(VS.80).aspx

Joachim
 

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