Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Visual J# / September 2004

Tip: Looking for answers? Try searching our database.

WinForms: System.exit() doesn't work from 'main'

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
BGU - 16 Sep 2004 11:59 GMT
Quick question:

I am writing a WinForms program that terminates itself from the 'main'
method:

    public static void main(String[] args)
        {
             Application.Run(new Form1()) ;
             // a Thread.sleep() call sits here
             System.exit( 0 ) ;
        }

The System.exit() call is not working.  The window stays up and running.
I'm sure there is a quick fix for this?

I also tried Application.Exit() and System.Environment.Exit().  No dice.  I
can find no MSDN page indicating the correct way to exit a WinForms app.

It can be repro'd easily by creating a new WinForms project in J# & adding
just the System.exit() call to the 'main' method.

Thanks,
Gordon
Lars-Inge T?nnessen [VJ# MVP] - 16 Sep 2004 14:38 GMT
It is expected.

> Application.Run(new Form1()) ;

This will start the windows message loop on the current thread (not in a new
thread).

>  // a Thread.sleep() call sits here

This will never be called before the message loop exits the loop and
returns. The WinForm must first exit.

Only "one" thread is running.

What do you want the app to do? How do you want it to exit? Maybe we can
come up with an example.

Regards,
Lars-Inge T?nnessen
BGU - 17 Sep 2004 04:50 GMT
Hi, thanks.  The goal is simple:

   - User launches my program
   - It displays some information for 30 seconds
   - It exits automatically

If you can suggest a way to make my VJ#/WinForms program terminate without
being prompted to do so by the user, that would help.

Peace,
Gordon

> It is expected.
>
[quoted text clipped - 15 lines]
> Regards,
> Lars-Inge T?nnessen
Lars-Inge T?nnessen [VJ# MVP] - 17 Sep 2004 13:54 GMT
I would suggest a timer. Here is an example I have written for you. This is
a J# WinForms app that will run for 5 seconds and then shutdown.

I'm using a timer (private System.Threading.Timer) to start a method
(private void TimerShutdown( Object arg )) after 5 seconds. As you can see
in the example, I'm creating the timer instance in the constructor.

import System.Drawing.*;
import System.Collections.*;
import System.ComponentModel.*;
import System.Windows.Forms.*;
import System.Data.*;

/**
* Summary description for Form1.
*/
public class Form1 extends System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;

// Timer
private System.Threading.Timer timer = null;

public Form1()
{
 InitializeComponent();

 // Maka the Timer
 this.timer = new System.Threading.Timer(
  new System.Threading.TimerCallback(TimerShutdown), // Method to be called
  null,
  5000, // 5 sec
  0 );
}

// The timer method
private void TimerShutdown( Object arg )
{
 System.exit(0);
}

protected void Dispose(boolean disposing)
{
 if (disposing)
 {
  if (components != null)
  {
   components.Dispose();
  }
 }
 super.Dispose(disposing);
}

private void InitializeComponent()
{
 this.SuspendLayout();
 this.set_AutoScaleBaseSize(new System.Drawing.Size(5, 13));
 this.set_ClientSize(new System.Drawing.Size(292, 266));
 this.set_Name("Form1");
 this.set_Text("Form1");
 this.ResumeLayout(false);

}

/** @attribute System.STAThread() */
public static void main(String[] args)
{
 Application.Run(new Form1());
}
}

Regards,
Lars-Inge T?nnessen

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.