473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

C#, Application does not terminate

Hi all,

my problem is that the application doesn't terminate when I close the form using the close box on the caption bar. Actually it seems as if the program is closed but task manager's process list shows that it is not.
There is one thread that I manully create and start in forms load event. But I set the IsBackground property of the thread to true. So, it should terminate while exiting the application.
There are other threads that refresh the grids on my form. Since I am using 2.0 framework, I set the CheckForIllegalCrossThreadCalls property of the form to false to avoid exceptions. Can the problem depend on this?
The weird thing is that I don't encounter this problem on my computer. On my computer the application terminates successfully.
May 7 '07 #1
16 48380
kenobewan
4,871 Expert 4TB
Have you tried using the application unload event?
May 7 '07 #2
Try defining the thread you are creating manually as a Background worker,
when you define the thread

thread t = new thread(new threadstart(function));
t.IsBackground = true;
t.Start();

check that.. I'm not shure the property is named IsBackGround, it says something like that.. but with that property activate that thread finishes when the main thread finish.

I hopy it helps you :D

enjoy your day :D
May 7 '07 #3
Thank you george, but it is exactly what I do.
It works on my computer. But a thread continues working when I test my application on another computer. There is no .NET framework on that computer. But I put the framework in setup project. May the problem depends on it ??

Thank you kenobewan. But I don't see the point in using application unload event. Can yo explain it please?
May 8 '07 #4
if you want to force the program termination you can use Environment.Exit(0); this will terminate all the working environmnent try it may help
May 8 '07 #5
vanc
211 Expert 100+
this sounds a bit weird, try Application.Exit(); on close button event then.
May 9 '07 #6
I think what the OP is trying to say is that if you click on the 'X' using the ControlBox or use 'ALT-F4' key combination to close the main form, then the Form will directly call Application.Exit() without going through any other validation. It will not use the Form.Closed(), Form.Closing() or any other method on its way out and they are obsolete in .NET v3 anyway.

Does anybody know if the function they are calling can be overwritten?

(Also before anybody says 'Hide the ControlBox' you can still 'ALT-F4' even if it's hidden to get the same results, and setting the thread's IsBackground property doesn't work either.)
May 30 '07 #7
TRScheel
638 Expert 512MB
Hi all,

my problem is that the application doesn't terminate when I close the form using the close box on the caption bar. Actually it seems as if the program is closed but task manager's process list shows that it is not.
There is one thread that I manully create and start in forms load event. But I set the IsBackground property of the thread to true. So, it should terminate while exiting the application.
There are other threads that refresh the grids on my form. Since I am using 2.0 framework, I set the CheckForIllegalCrossThreadCalls property of the form to false to avoid exceptions. Can the problem depend on this?
The weird thing is that I don't encounter this problem on my computer. On my computer the application terminates successfully.

Are you sure its the thread?

Try this:


Expand|Select|Wrap|Line Numbers
  1. Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
  2.         ...
  3.  
  4.         static void Application_ApplicationExit(object sender, EventArgs e)
  5.         {
  6.                WorkerThread.Abort();
  7.         }
that should force the workerthread to do a hard close, so i wouldnt suggest doing it in production, but it will show you if the problem really is the thread...
May 30 '07 #8
Yes the issue is that the threads are still running, or you want the 'X' method to use your 'Close' method that cleans stuff up. However stopping the application abruptly is a bad thing as it could have left things half done...

A bit more browsing has brought me to this:
Expand|Select|Wrap|Line Numbers
  1.         public const int SC_CLOSE = 0xF060;
  2.         public const int WM_SYSCOMMAND = 0x0112;
  3.  
  4.         protected override void WndProc(ref System.Windows.Forms.Message m)
  5.         {
  6.             if (m.Msg == WM_SYSCOMMAND && (int)m.WParam == SC_CLOSE)
  7.                 MyClose();  
  8. // your method that cleans everything up and then runs
  9. // System.Environment.Exit(0) which WILL close the threads forcefully if needed
  10.  
  11.             base.WndProc(ref m);
  12.         }
  13.  
May 30 '07 #9
Plater
7,872 Expert 4TB
I had that trouble with Threads too.
You gotta send the thread the Abort() call.
It was throw all kinds of ThreadAborting exceptions in the thread and with that you can determine inside the thread when to exit.

With a little bit of sneakiness I put it in the dispose method of the calling form usually
May 30 '07 #10
Gremsy
1
eflatunn, did you every find the solution to this problem? I'm having it too. I set all my threads to IsBackground = true, yet sometimes on a test machine the process doesn't close even though the applications form is closed.
Aug 5 '08 #11
@Plater
Hi

I also have problems with my c# application which manages threads and sockets connections. I noticed that when I press close for running Application.Exit(), the program keeps on memory.

So I decided to put

Expand|Select|Wrap|Line Numbers
  1. Process.GetCurrentProcess().Kill();
it seemed to work pretty well. After reading your note here, I've put

Expand|Select|Wrap|Line Numbers
  1. Thread.CurrentThread.Abort();
It seemed to work too. But I was wondering which approach is the best for closing my application and release memory??
May 16 '09 #12
Bassem
344 100+
I forced the same problem with threads when I was doing database applications.
What I did is, On the thread that uses the database queries I test the connection if close the thread is aborted, and add event handler Closing or Closed to close this connection.
As the previous tone, you could declare a global field (accessed by both main thread and the other one) let's say it'll be bool workAbort is set to true Closed event handler change it to false, and your other thread should test it before any action it does.
I think now you can control many threads as you want.

Thanks,
Bassem
May 16 '09 #13
I also use database access but with Enterprise Library 4. I think it'll be good no to worry and let this framework take care of any thread issues. That's why I didn't create any custom event handlers for closing threads. I've never needed it.

I noticed yesterday that when I use

Expand|Select|Wrap|Line Numbers
  1. Thread.CurrentThread.Abort();
it doesn't caused me problem and problems in debug mode, but when I execute my .exe application file directly from the bin folder, it throws an exception about cannot abort thread. The most weird thing is that this command was nested within a try cath {} block!

So I left only the statement

Expand|Select|Wrap|Line Numbers
  1. Process.GetCurrentProcess().Kill();
on my form_Closing event. Now I can start and close the applications, and I've got no thread issues so far.
May 16 '09 #14
@george1106
Thanks george1106

I tried your advice about the

thread t = new thread(new threadstart(function));
t.IsBackground = true;
t.Start();

"IsBackground" is the right property name and it worked out great. As soon as i terminate the program, the thread terminates and disappears from the task manager.
Apr 26 '10 #15
Plater
7,872 Expert 4TB
Huh, how about that, learn something new everyday.
A thread is either a background thread or a foreground thread. Background threads are identical to foreground threads, except that background threads do not prevent a process from terminating. Once all foreground threads belonging to a process have terminated, the common language runtime ends the process. Any remaining background threads are stopped and do not complete.
Apr 26 '10 #16
I have experienced tis too, and this is what i did

private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
{


Application.Exit();



}
I hope this works, cos it wrked for me
Jul 20 '10 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: G.D | last post by:
hi i create an excel application (MS Excel 2000 installed) in vb.net. At the end, i close all workbook and quit from excel aplication. I also set them nothing and call garbage collection. But excel...
3
by: Matthias Jentsch | last post by:
Hello, I have a simple testapplication: public class Test : ApplicationContext { private static void Main () { Application.Run(new Test()); }
6
by: Max | last post by:
I have the following code on a form that launches Microsoft Outlook and creates a new email message for the user: Outlook.Application oApp = new Outlook.Application(); Outlook.MailItem oMail...
3
by: ohayo | last post by:
In a windows form app (which acts as a front end to my quickfix application), I get the following error on compiling Application does not contain a definition for 'Run'. What does this mean,...
4
by: Charts | last post by:
I have a C# console application and I need pop up a Messagebox. However the IntelliSense does not show for using System.Windows.Forms; So, the application does not recognize the MessageBox. Please...
0
by: stephen | last post by:
Hi, Please oblige if this is a repeat message. I was working on W2k OS and hd a windows application to monitoring my folders (FileSystemWatcher)... it writes any changes to a log file, due to...
1
by: Amtechie | last post by:
Hi all Have been into a problem of not finding the database of the VB application which i have been installed in the Vista machine. The installation of the software takes place normally in all the...
11
by: Andrus | last post by:
Managed code .NET 3.5 C# WinForms application runs OK from Vista when in local drive. When application is copied to mapped network drive or started from \\othercmp\c\myapp\myapp.exe folder , it...
1
by: Prats | last post by:
I have an application developed in C++/CLI using dot NET 3.5 I tested on windows XP, Windows Vista Basic and Vista Home premium It does not run on vista Home Premium. The application installed...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.