473,769 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to abort a WinForms apps with no main window

Hi all,

I have a WinForms app that has no main window (just a module with a Sub
Main). My Main routine calls a subroutine that wants to politely abort the
application rather than return to its caller. How can I do this? I tried
Application.Exi t but that doesn't terminate my application.
Mar 11 '06 #1
5 2283
Hi Bob ,

Thanks for your post!

Normally, if we return the execution path to the Main method, the main
thread will exit and the winform application will exit without any problem.
This is the most recommanded way to exit an application. Can you show me
why you can not use this way?

Additionally, based on my test, Application.Exi t() should work in exiting
the application. If I have misunderstood you, please provide some more
information? Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Mar 13 '06 #2
Hi Jeffrey,

Here my source code. As you can see, the program first tests to see if some
conditions are met (registry keys exist, files exist, command line arguments
make sense, etc.). If any test fails then it calls a routine whose job is
to display the error and abort the program. It aborts the program by
throwing a custom exception which is caught by exception handler. This
scheme works, but it requires some extra code (the custom exception, the
extra exception Catch clause, etc.). What I really want to do is the
equivalent of just calling the C/C++ "Exit()" function in my Abort routine.

I had originally coded this with a call to Application.Exi t in place of the
Throw statement, but that statement did nothing; the Abort routine simply
returned to its caller after displaying the message box.

=============== =======

Module MainModule

Sub Main()
Try
' Error if conditions are not met
If <some test> Then Abort("First condition is not met")
If <another test> Then Abort("Some other condition is not met")

' Do the actual work
<Do something>

Catch ex As AbortException
' <Ignore the exception and let the application exit>

Catch ex As Exception
Abort(ex.Messag e)
End Try

End Sub

Private Sub Abort(ByVal errorMessage As String)
<Format the error message>
MessageBox.Show (<error message>)
Throw New AbortException
End Sub

Private Class AbortException
Inherits System.Applicat ionException
End Class

End Module

=============== ======

""Jeffrey Tan[MSFT]"" <je***@online.m icrosoft.com> wrote in message
news:%2******** ********@TK2MSF TNGXA03.phx.gbl ...
Hi Bob ,

Thanks for your post!

Normally, if we return the execution path to the Main method, the main
thread will exit and the winform application will exit without any
problem.
This is the most recommanded way to exit an application. Can you show me
why you can not use this way?

Additionally, based on my test, Application.Exi t() should work in exiting
the application. If I have misunderstood you, please provide some more
information? Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Mar 13 '06 #3
Hi Bob ,

Thanks for your feedback.

Regarding this, why not just return from Main method to exit the
application? This is the graceful and recomanded way of exiting a
thread/application. (Actually, even in C/C++, exit() is not the recommanded
way to exit the thread, because:
"When you call the exit or _exit functions, the destructors for any
temporary or automatic objects that exist at the time of the call are not
called. An automatic object is an object that is defined in a function
where the object is not declared to be static. A temporary object is an
object created by the compiler.", this may cause some inconsistent behavior
or memory leak in the 24X7 server applications.)

Exception is not a recommanded way to jump the execution path; it has some
performance hit, because exception requires some context switch to the
kernel mode.

Yes, I can reproduce out the behavior that Application.Exi t() method does
not take effect in this scenario. This is because Application.Exi t
internally always check an internal ApplicationCont ext class, which is
created after invoking Application.Run (). Because your code invokes
Application.Exi t before Application.Run , this Application.Exi t() method
takes no effect. If you really want to exit the current thread, you may
p/invoke ExitThread Win32 API, however, just as I original stated, it is
not an elegant API, which we should avoid in application. Returning from
Main method in normal flow is the correct way.

Hope this helps!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Mar 14 '06 #4
Thanks for the info. That helps a lot!

- Bob

""Jeffrey Tan[MSFT]"" <je***@online.m icrosoft.com> wrote in message
news:5j******** ******@TK2MSFTN GXA03.phx.gbl.. .
Hi Bob ,

Thanks for your feedback.

Regarding this, why not just return from Main method to exit the
application? This is the graceful and recomanded way of exiting a
thread/application. (Actually, even in C/C++, exit() is not the
recommanded
way to exit the thread, because:
"When you call the exit or _exit functions, the destructors for any
temporary or automatic objects that exist at the time of the call are not
called. An automatic object is an object that is defined in a function
where the object is not declared to be static. A temporary object is an
object created by the compiler.", this may cause some inconsistent
behavior
or memory leak in the 24X7 server applications.)

Exception is not a recommanded way to jump the execution path; it has some
performance hit, because exception requires some context switch to the
kernel mode.

Yes, I can reproduce out the behavior that Application.Exi t() method does
not take effect in this scenario. This is because Application.Exi t
internally always check an internal ApplicationCont ext class, which is
created after invoking Application.Run (). Because your code invokes
Application.Exi t before Application.Run , this Application.Exi t() method
takes no effect. If you really want to exit the current thread, you may
p/invoke ExitThread Win32 API, however, just as I original stated, it is
not an elegant API, which we should avoid in application. Returning from
Main method in normal flow is the correct way.

Hope this helps!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Mar 16 '06 #5
You are welcome.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Mar 17 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5560
by: Drew Stoddard | last post by:
We are working in C#/Winforms and are using asynchronous client-side web service calls (the Begin... and End... methods supplied by the web reference creation). Many of these calls are contained in Windows forms that the user can close before the web service call has completed. This seems to be working fine, but I wanted to check to see if anyone has experienced any problem with this. Specifically this is what I need to know: Is it...
10
2416
by: BBFrost | last post by:
We just recently moved one of our major c# apps from VS Net 2002 to VS Net 2003. At first things were looking ok, now problems are starting to appear. So far ... (1) ComboBox.SelectedValue = db_value; If the db_value was not included in the ComboBox value list the ComboBox.SelectedIndex used to return -1, Now the very same code is
0
1458
by: sean | last post by:
From a parent process, I created a custom windows station and desktop. I then CreateProcess using that new station/desktop. There are no problems launching a plain windows app with this station/desktop. (I tried some simple apps and notepad.exe) However a Winforms app fails. If I use the default station (Winsta0) but a custom desktop, the Winforms app does work. I would prefer using a custom windows station, but I'm close to punting :(
4
3737
by: Elhanan | last post by:
hi.. all a client of ours is considering to move from a dos application to windows desktop application. the application is for traveling agency, the database is rather large. their current database for other application is SQL-SERVER. they have 12 users.
1
2072
by: Ennixo | last post by:
hi, i coded an application in which i can move a slider to define a radius and it computes a gaussian blur in a thread. because the Scroll event of the slider is often raised, i use one thread (declared in the form's class) that i abort when it is already existing and running. this works well but sometimes i've got this error (translated from
20
2904
by: Martin Rosén-Lidholm | last post by:
Although an impossible question to answer, I fell urged to raise it anyhow. Given a fairly complex ERP application scenario, what's your estimation for the X-ratio dev. time for WebForms app
3
8159
by: Robert Brinson | last post by:
I'm writing one of my first WinForm apps; everything else has been WebForms up to this point. However, having come from a background of Java, C++, and Perl, I'm having trouble understanding how VB.NET works as far as WinForms are concerned. So, please bear with me. I'm used to applications having a static main function that has args as one of its arguments to accept command line flags. I want to have an application that can either be...
5
3827
by: brian.wilson4 | last post by:
Our group is currently comparing winforms vs webforms.....app is Corp LAN based - we have control of desktops.....Below is pros and cons list we have come up with - if anything strikes you as untrue or you would like to add - please comment - thanks..... Rich Client PROS 1. User experience (* indicates feasible on web with AJAX) - a. Single, unified application experience b. Windows/Office-like look and feel - i.Tabs - drag and drop...
10
6728
by: Jules Winfield | last post by:
Guys, I've been designing applications for the financial services industry using MSFT technologies for many years (Win32 API, MFC, and now .NET WinForms). All applications are Internet-based, with a "thick client" which makes calls to my grid of servers via a socket or remoting connection. Customers are pleased with my work but it seems that over the past twelve months or so, those same customers have expressed a strong demand to...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10212
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10047
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9995
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5304
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.