How to kill a thread in c#?

  • Thread starter Thread starter bie2
  • Start date Start date
B

bie2

Hi,

Here's what I want to do:

1. Start the application.
2. A SplashScreen running in its own thread is started.
3. I'm checking if the application needs an update.
4. If an update is needed I close the SplashScreen and copy the file.
5. Restart the application.

My problem is happening at step 4. I tried everything that I know to
close the SplashScreen, but I seems to be unable to do it. When the
application file is being replace it's always saying that the
executable is used by another process.

If I remove the code related to the SplashScreen everything is working
fine.

I tried to call a function in the SplashScreen with this code:
ms_oThread.Abort();
ms_oThread.Join();
ms_oThread = null;

Was not working.
Anyone have any idea how I could be able to do that?

Thanks
 
bie2,

If you have an application checking itself for updates, you will need a
separate program that does it. This program will load the splash screen,
and then replace the main application executable if it finds it needs an
update. Otherwise, it just runs the main application. Since the binary is
loaded into the CLR, you can't delete the file.

Also, have you looked into the update management block from Microsoft?
Or ClickOnce (due out with .NET 2.0, if you can wait for it). Both of these
address this issue, and would probably reduce the amount of code you have to
write.

Hope this helps.
 
Hi, Nick,

Do you have information when .NET Framework 2.0, VS.NET 2005 and SQL 2005
will be released? We are planning to use it for our next generation
applications. Thanks!

John
Nicholas Paldino said:
bie2,

If you have an application checking itself for updates, you will need a
separate program that does it. This program will load the splash screen,
and then replace the main application executable if it finds it needs an
update. Otherwise, it just runs the main application. Since the binary
is loaded into the CLR, you can't delete the file.

Also, have you looked into the update management block from Microsoft?
Or ClickOnce (due out with .NET 2.0, if you can wait for it). Both of
these address this issue, and would probably reduce the amount of code you
have to write.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

bie2 said:
Hi,

Here's what I want to do:

1. Start the application.
2. A SplashScreen running in its own thread is started.
3. I'm checking if the application needs an update.
4. If an update is needed I close the SplashScreen and copy the file.
5. Restart the application.

My problem is happening at step 4. I tried everything that I know to
close the SplashScreen, but I seems to be unable to do it. When the
application file is being replace it's always saying that the
executable is used by another process.

If I remove the code related to the SplashScreen everything is working
fine.

I tried to call a function in the SplashScreen with this code:
ms_oThread.Abort();
ms_oThread.Join();
ms_oThread = null;

Was not working.
Anyone have any idea how I could be able to do that?

Thanks
 
John,

The best I could say is 2nd quarter 2005 (and that is a guess). No firm
release dates have been set.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

John Lee said:
Hi, Nick,

Do you have information when .NET Framework 2.0, VS.NET 2005 and SQL 2005
will be released? We are planning to use it for our next generation
applications. Thanks!

John
Nicholas Paldino said:
bie2,

If you have an application checking itself for updates, you will need
a separate program that does it. This program will load the splash
screen, and then replace the main application executable if it finds it
needs an update. Otherwise, it just runs the main application. Since
the binary is loaded into the CLR, you can't delete the file.

Also, have you looked into the update management block from Microsoft?
Or ClickOnce (due out with .NET 2.0, if you can wait for it). Both of
these address this issue, and would probably reduce the amount of code
you have to write.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

bie2 said:
Hi,

Here's what I want to do:

1. Start the application.
2. A SplashScreen running in its own thread is started.
3. I'm checking if the application needs an update.
4. If an update is needed I close the SplashScreen and copy the file.
5. Restart the application.

My problem is happening at step 4. I tried everything that I know to
close the SplashScreen, but I seems to be unable to do it. When the
application file is being replace it's always saying that the
executable is used by another process.

If I remove the code related to the SplashScreen everything is working
fine.

I tried to call a function in the SplashScreen with this code:
ms_oThread.Abort();
ms_oThread.Join();
ms_oThread = null;

Was not working.
Anyone have any idea how I could be able to do that?

Thanks
 
bie2 said:
1. Start the application.
2. A SplashScreen running in its own thread is started.
3. I'm checking if the application needs an update.
4. If an update is needed I close the SplashScreen and copy the file.
5. Restart the application.

How do you create the splash screen? What code runs Application.Run?

Note that you have two totally different objects, the .NET Form object and
the Windows window. You have to synchronize the lifetime of each, and
synchronize them with the message queue. The message queue is created by
Application.Run and this keeps the main thread alive. If the main thread is
kept alive, then the application is kept alive. If the application is alive
then you cannot overwrite the process's file.
My problem is happening at step 4. I tried everything that I know to
close the SplashScreen, but I seems to be unable to do it. When the
application file is being replace it's always saying that the
executable is used by another process.

That's because it is. You may close the splash screen, but you've not closed
the process. Instead, spawn a new process that copies the file, but get it
to wait for a short time before it does this. After starting the new
process, close down the app with the splash screen. Depending on how you
show the splash screen, or how the rest of the app works determines how you
do this. In most cases Application.ExitThread should do it.
ms_oThread.Abort();

NEVER call Thread.Abort, that is a horrible way to kill a thread.

Richard
 
Here's how the application is structured

When the main application is started, before doing an Application.Run I
call the SplashScreen function ShowSplashScreen.

The function I'm calling is static . In this function I have this:
ms_oThread = new Thread( new
ThreadStart(SplashScreen.ShowForm));
ms_oThread.IsBackground = true;
ms_oThread.ApartmentState = ApartmentState.STA;
ms_oThread.Start();

In the function ShowForm I have this:
ms_frmSplash = new SplashScreen();
Application.Run( ms_frmSplash );

Thanks for your answer.
 
Hi,


2. A SplashScreen running in its own thread is started.

The main thread should be the only one interacting with the screen, create a
worker thread instead.
3. I'm checking if the application needs an update.
4. If an update is needed I close the SplashScreen and copy the file.
5. Restart the application.
My problem is happening at step 4. I tried everything that I know to
close the SplashScreen, but I seems to be unable to do it. When the
application file is being replace it's always saying that the
executable is used by another process.

It may due the fact that you create the form in another thread.
The other problem is logic, if you are running the applicacion you cannot
overwrite it.

Solution
1- Create a Splash application , do the check and then using Process.Start
load the "real" application, after that you can close the splash
Tip 1: You can make the Splash screen to stick in the foreground while the
app is loaded in the background
Tip 2: You could use some IPC to inform the splash app that the real app is
loaded and it can get close



Cheers,
 
Here more precision, that I left out.

When an update is necessary, I start a new process with an application
that will do the update.
The thing I don't understand is that I don't see anymore the process
for the main application. And it's still saying that the file cannot be
copied because a process is still using it.

Is it possible that the new process (the one for the update) is locking
my application?
 
Hi,

Do the opposite start running the application that checks for update, if
there is one download & install it then start the real application

It's much easier that way

Cheers,
 
I agree, but the problem is that to check for an update it's done
through the web and this can maybe take some time.
So I wanted a splash saying "Checking for an update".
 
Hi,

What stop you from using a win app for doing the check?

cheers,
 
Nicholas Paldino said:
John,

The best I could say is 2nd quarter 2005 (and that is a guess). No
firm release dates have been set.

I've heard 3rd quarter at the earliest, and even that may be pushed back
 
David Levine said:
I've heard 3rd quarter at the earliest, and even that may be pushed back

When was that, out of interest? I haven't heard anything beyond "H1"
officially, with unofficial suggests of late in Q2. It'll be a real
shame if it isn't out until Q3 :(
 
As best as I can recall, I heard it about a month or two ago. We originally
needed it around the August time frame and then we were told that it would
be held up at least a month or two, so now it's looking like September or
so - caused us to drop Whidbey and plan on using v1.1. These dates are iffy
and can very well change. I can get more current info if you need it...I can
forward the request on to my MSFT contact.
 
I tought about that at the beginning but if I changed this "updater"
I'll need to do the update it also.
But I think now it's the only solution that can be worth exploring.
I'll try to make it the most generic possible so I won't have any
update to do.

Thanks for your answers
 
bie2 said:
When the main application is started, before doing an Application.Run
I call the SplashScreen function ShowSplashScreen.

The function I'm calling is static . In this function I have this:
ms_oThread = new Thread( new
ThreadStart(SplashScreen.ShowForm));
ms_oThread.IsBackground = true;
ms_oThread.ApartmentState = ApartmentState.STA;
ms_oThread.Start();

In the function ShowForm I have this:
ms_frmSplash = new SplashScreen();
Application.Run( ms_frmSplash );

On your ms_oThread you create a new message loop with Application.Run(), and
you have another thread (the one that creates ms_oThread) which also calls
Application.Run. That is acceptable, but note that both threads will be kept
alive while Application.Run runs. How long does the SplashScreen form show?
How is it closed? A call to Application.ExitThread somewhere in the
SplashScreen should make sure that the call to Application.Run will finish,
releasing the thread. (Normally, closing a form by clicking on its close
button on the caption bar should do this, but I guess your splash screen
will not have a caption bar).

Richard
 
Back
Top