Nice Threads!

G

Guest

I could really use some help here...

I am using C# 2005 and have created a splash screen that runs on a seperate
thread. I can invoke the splash screen in the Main() method, but then I
can't get rid of it! I am attempting to put a line in the Form1_Load (or
maybe Form1_Shown) that calls a static method from the splash screen to close
that form. But, when I do so, it complains about an invalid cross-thread
operation. How can I get this screen to close?

Here are the relevant parts to my splash screen:

// Static Public Method to launch Splash Screen
static public void ShowSplash()
{
if (ms_frmSplash != null) return;
ms_Thread = new Thread(new ThreadStart(frmSplash.ShowForm));
ms_Thread.IsBackground = true;
ms_Thread.SetApartmentState(ApartmentState.STA);
ms_Thread.Start();
}

// Static Private Method to Display Splash Screen
static private void ShowForm()
{
ms_frmSplash = new frmSplash();
Application.Run(ms_frmSplash);
}

// Static Public Method to Close Splash Screen
static public void CloseForm()
{
if (ms_frmSplash != null) ms_frmSplash.Close();
}
 
B

Bruce Wood

Oooh! Oooh! (Doing best Horshack impression)

Perhaps you need to do this...?

static public void CloseForm()
{
MethodInvoker closeDelegate = new
MethodInvoker(ms_frmSplash.Close);
ms_frmSplash.BeginInvoke (closeDelegate );
}

I'm guessing here....
 
J

Jon Skeet [C# MVP]

XDJ said:
I could really use some help here...

I am using C# 2005 and have created a splash screen that runs on a seperate
thread. I can invoke the splash screen in the Main() method, but then I
can't get rid of it! I am attempting to put a line in the Form1_Load (or
maybe Form1_Shown) that calls a static method from the splash screen to close
that form. But, when I do so, it complains about an invalid cross-thread
operation. How can I get this screen to close?

See http://www.pobox.com/~skeet/csharp/threads/winforms.shtml
 

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