|
-
May 30th, 2007, 03:37 PM
#1
Thread Starter
Member
Trap user interrupts in Visual Basic .Net?
According to this link, ...
http://support.microsoft.com/kb/146864
... VBA programs can trap user interrupts by setting the EnableCancelKey property to xlErrorHandler.
Is there a way to trap user interrupts in Visual Basic .Net? If no, why not?
-
May 30th, 2007, 08:57 PM
#2
Re: Trap user interrupts in Visual Basic .Net?
You're not exactly comparing apples to apples. What kind of user interrupts do you want to trap and what kind of work will your code be doing when the interrupts may happen?
-
May 31st, 2007, 07:55 AM
#3
Thread Starter
Member
Re: Trap user interrupts in Visual Basic .Net?
I want to trap all user interrupts. In VBA, if you set the EnableCancelKey property to xlErrorHandler, "all interrupts will generate a run-time error number 18".
What kind of work will your code be doing when the interrupts may happen I'm not following you. Can't an interrupt happen at any point in the program?
Last edited by esharris; May 31st, 2007 at 07:58 AM.
-
May 31st, 2007, 08:07 AM
#4
Fanatic Member
Re: Trap user interrupts in Visual Basic .Net?
The way that vb.net handles errors is with a Try..Catch block:
Code:
Try
'......do code
Catch Sqlex as SqlClient.SqlException
'You can specify specific catches based on exception types OR
Catch ex as Exception
'This will catch ALL exceptions.
Finally
'This will trigger irregardless of error or no error (unless total application failure).
'You can use this to clean up database connections or objects, etc.
End Try
If you are worried about something being unhandled in your app then you can use the Application.Designer.vb. To get to this, make sure you are looking at ALL files (button is located at the top of the solution explorer).
Code:
Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
'You can put specific program related options in here
End Sub
If you want to just clean up any stray items in your program before closing then you can use this, also located in the Application.Designer.vb:
Code:
Private Sub MyApplication_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown
End Sub
I hope this helps to answer your question(s).
D
Platforms of choice: Visual Studio 2005/2008 Professional : Visual Studio 2010 Enterprise : PHP - Notepad++/WAMP
Please Rate If I helped you. 
Please remember to mark threads as closed if your issue has been resolved.
Reserved Words in Access | Connection Strings
-
May 31st, 2007, 08:15 AM
#5
Thread Starter
Member
Re: Trap user interrupts in Visual Basic .Net?
Thank you . I know about Try...Catch blocks. You use them to catch exceptions.
However, I want to catch user interrupts.
"A user can interrupt a Visual Basic for Applications procedure by pressing CTRL+BREAK or ESC (COMMAND+PERIOD on the Macintosh)."
Pressing these buttons has no effect on my Visual Basic .Net programs (I'm using 2005) .
-
May 31st, 2007, 08:25 AM
#6
Re: Trap user interrupts in Visual Basic .Net?
 Originally Posted by esharris
Pressing these buttons has no effect on my Visual Basic .Net programs (I'm using 2005)  .
That's why I asked the above questions. You will have to create the behaviors based on what you expect to happen.
-
May 31st, 2007, 08:27 AM
#7
Fanatic Member
-
May 31st, 2007, 08:44 AM
#8
Re: Trap user interrupts in Visual Basic .Net?
 Originally Posted by esharris
Thank you  . I know about Try...Catch blocks. You use them to catch exceptions.
However, I want to catch user interrupts.
"A user can interrupt a Visual Basic for Applications procedure by pressing CTRL+BREAK or ESC (COMMAND+PERIOD on the Macintosh)."
Pressing these buttons has no effect on my Visual Basic .Net programs (I'm using 2005)  .
VB.NET isn't a macro language designed to work with a specific application framework like Word or Excel like VBA is designed to do. It's a general purpose programming language. If you want these kind of user interrupts you have to code them into your application yourself. You would probably code it by either using standard form events or by hooking into Windows messages if you wanted a more generalized approach.
-
May 31st, 2007, 09:22 AM
#9
Thread Starter
Member
Re: Trap user interrupts in Visual Basic .Net?
Please forgive my ignorance .
Isn't the handling of form events serialized? Suppose I trigger a form event that kicks off a time consuming subroutine. Doesn't VB .Net wait for the current event to complete before handling a new event? If this is true, I can't write a keydown event to interrupt the processing of time-consuming event. Right?
-
May 31st, 2007, 09:24 AM
#10
Re: Trap user interrupts in Visual Basic .Net?
No, but you can use threading to handle the time-consuming event, and then interrupt the thread on user input. Look at the BackgroundWorker.
-
May 31st, 2007, 09:30 AM
#11
Thread Starter
Member
Re: Trap user interrupts in Visual Basic .Net?
The BackgroundWorker looks like a solution. Thanks!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|