I'd like to know if there is a possible way to handle a closing application in Visual Studio 2008 without using the dispose event handler.
If my application crashes or if I close it while it is running:
Private Sub Foo_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
Is not called.
This result in a serious problem, because I'm currently working on multiple Excel files and they remain open after the application crashes or I close it while it's running.
Is there a way to handle this kind of closing application event?
In a normal situation when your application is simply closing you can subscribe to the MyApplication.Shutdown event and close your excel documents in there.
Subscribing to the event can be done through these steps:
Right-click your project in the Solution Explorer and press Properties.
Go to the Application pane and press View Application Events.
In the file that was opened, either write the event handler on your own or let VS do it by first selecting (MyApplication Events) in the left combo box above the text editor, then selecting Shutdown in the right combo box.
Now you should have an event handler that looks something like the one below. Just go ahead and do your cleanup in there:
Private Sub MyApplication_Shutdown(sender As Object, e As System.EventArgs) Handles Me.Shutdown
'Do your cleanup here...
End Sub
For application crashes caused by CLR exceptions you can use the AppDomain.UnhandledException event, but for more serious crashes there isn't very much you can do.
A workaround would be to create another application which monitors your main app. When the other app senses that your main application's process has been terminated, it will close the excel documents. The tricky part with this solution is passing the information necessary for the other app to close the documents.
Related
I'm working on a single instance application in VB.NET that goes hidden when we minimize the form:
Private Sub Form_Resize(sender As Object, e As EventArgs) Handles Me.Resize
If Me.WindowState = FormWindowState.Minimized Then
Me.Visible = False
End if
End Sub
Then, if I try to launch it again, it doesn't open because it is already running (so far, so good).
Is it possible to make it just visible on this stage? I mean, can I just make the application visible if I try to launch it when it's already running?
I suppose this can be done because there are some applications with this exact behavior.
In your WinForms project properties select "View Application Events" (My Project -> Application -> View Application Events). From there you can see the events that get fired at the start and end of your application. One of which is the StartupNextInstance event handler. In here you should be able to make your form visible again.
Per docs:
A single-instance application raises the StartupNextInstance event when you attempt to restart the application when it is already active. When a single-instance application starts for the first time, it raises the Startup event. For more information, see Startup.
StartupNextInstance Documentation
I was going to build my project when i noticed i didn't put an icon,
and since i couldn't access the icon value of the original form because i used a theme i C+X the container and access it from the grey form, change it, C+V the container, built the project.
Nothing changed, all the name of the buttons and stuff are the same, but i feel like nothing is connected to the code anymore, i don't know what happened, i just recently got re-interested into coding, and i have no idea what to do, i tried some things but nothing worked, so here i am, desperate (i spent 3 days on this, i'm REALLY starting from bottom)
link to the project: http://www.mediafire.com/file/2zrbe32lzpx2qhz/SchedulerProjectVBNET.rar
Thanks in advance
It sounds like you have lost all the Handles clauses off your event handlers. As an example, if you add a Button to your form and double-click it, you will get a Click event handler like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Note the Handles clause at the end, which is what connects the code to the event of the control. If you cut that Button from your form to the Clipboard then it no longer exists as part of the form, so that Handles clause will be automatically removed. If you paste the Button back onto the form, the Handles clause is not restored, so the method no longer handles the event.
If that's what has happened - which you can easily check just by looking at the code - then you need to restore all those Handles clauses. You can do that manually, i.e. write them all yourself in the code window, or you can have the designer regenerate them for you. To do the latter, select a control in the designer, open the Properties window, click the Events button, select the desired event and then select the appropriate method from the drop-down list.
Note that you can also double-click an event there to generate a new event handler, which can be useful for handling events other than the default event. You can generate a handler for the default event simply by double-clicking the control.
I have a splash-screen for a desktop app that I am making. The way the startup works is that it checks for certain files, if it finds them it closes immediately and loads up the main form.
However by doing so I get a threading error. I'm guessing its because the startup form closes before fully loading the Main form.
Here's the error message:
Cross-thread operation not valid: Control 'frm_SplashScreen' accessed from a thread other than the thread it was created on.
I've tried putting the thread to sleep, and even using timers to give the form time to load. But so far unsuccessfully.
Private Sub frm_Load_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If Dir("System.dll") = "" then
Createfile()
Frm_Main.show()
Me.close()
Else
Frm_Main.show()
Me.close()
End IF
Everything else regarding the splash-screen is done in the project properties under application. Otherwise it is just an empty form.
How can I fix this?
In WinForms, the splash screen is not responsible for creating the main form and should not do so. If you have code that needs to run before the main form is created, while the splash screen is visible, place that code in the application's Startup event. Here's how to do it:
Go to the application tab of your project's properties (same place you set the startup form and splash screen).
Click on "View Application Events"
In the event dropdown near the top of the window, select "Startup". This should create an event handler method for the Startup event, if it doesn't already exist (Private Sub MyApplication_Startup(sender, e) Handles Me.Startup).
Put your code (the If Dir(...) Then Createfile() stuff in your example code) in the event handler.
The code in the Startup event handler will be called before the main form is created. The splash screen will automatically be created, displayed, and destroyed in another thread. You don't need to worry about creating or closing the splash screen, as that is done automatically.
If your startup code determines that you don't want the main form to be displayed at all, set e.Cancel = true in the startup event handler method and the application will quit instead of bringing up the main form.
Well, according to my previous experience, this is a bug in visual studio, and it comes as a result of setting your startup form to window state.maximized that overrides the default launch event. So in order to fix this, you need the start up for to laugh at the startup then set the event on the splash screen's form closing event, instead of it running directly in the startup form.worked on over 5 projects. They all had the same issues till I moved the maximized state to the splash closing event e.g
Private sub splash_closed(....)
Startup form.window state.maximized...
End sub
So, the issue was actually event collision.
I have a quick question.
I want to run some come when my application exits, wherever in the winform application it may be. The reason is that the user adds/changes data in the application in different forms, and I have it set to save different data to My.Settings. When the application exits, I want it all to be saved to a text file, to be loaded when the application is next started up. I cannot save to text file whenever the data is changed because it is changing too many times, so for ease of access, I save it My.Settings.
I know how to save the data to a text file, I just want to know how can I have it run the code to save data to the text file WHEN the application is exiting.
VB allows you to add code to perform tasks when your application Starts or Shuts down. To access these, go to Project Properties -> Application Click "View Application Events".
This will open a file very much like a form code window. From the left/Declarations menu select MyApplication Events, then select ShutDown from the event list. This will bring up this:
Private Sub MyApplication_Shutdown(sender As Object,
e As EventArgs) Handles Me.Shutdown
' your code
' to executes when the application is shutting down
End Sub
VB.NET program.
While developing on Visual Studio Express 2010, there are two buttons: Start Debugging, and Stop Debugging.
Well, while debugging, I close my application (the red X button). However, when I look back at Visual Studio, it seems to still be in debugging mode (the Start button is disabled, and the Stop button is enabled). So I have to manually press the Stop button.
I remember it was not like this before.
Perhaps it is because my application uses multiple forms or something? Then I am probably missing something rather important... Any ideas?
You've got something open somewhere. You can force the entire app to quit by adding
End
in the form's FormClosed event:
Private Sub Form1_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
End
End Sub
Are you doing any threading? Shutting down your primary app leaving threads running will cause what you are seeing. Also app design can cause what you are seeing. You can start your vb.net app running a "main" procedure, but if you don't provide an exit for it, the app will continue to run.
Ensure that all hidden forms (if any) are closed, properly dispose all objects.
If you are hiding any form and forgot to close it in your application this scenario may happen. Or if you are using threading in your application and they are not terminating properly then also it happens.