Unload even on my form? - vb.net

I want to run a subroutine to clear some things up when the user exits the application. I tried looking for a Form_Unload event or anything similar, is there a way to do this?
I open a database connection on Form_Load, and would like to close it when the user exits the app.

Most of the time, you should try not to hang on a connection for the lifetime of an application. You should open and close it as needed. If you don't want to close and reopen it as a performance optimization, you don't need to worry about it. Connection pooling for the database driver should handle that.
Anyway, you don't really need to close the connection if the process is going to end. The acquired resources will get released automatically.
If you want to execute code as the form gets closed, you can handle its FormClosing event. If you want the code to execute after a form is closed, handle its FormClosed event.

Use the FormClosing event. The MSDN Library article is here. Poke around a bit more, these are the kind of events you need to know pat to do any kind of Windows Forms programming.

Related

ABAP do something on program exit

in my ABAP program I'm updating field X in table tab1 at the beginning and in the last step if everything goes OK, I'm reversing this update. It's important that during execution of program the field X has correct value.
However when I exit the transaction with close button not SAP cancel button (F12), program terminates and it doesn't go to the end of program, thus not reversing the update made at the beginning.
Is there a way that I can execute some code after closing the report?
The "close window" button cannot be controlled by program (this is true for the "external modes" i.e. the full-screen windows, which seems to be your question, but not for the popups, whose close button can be controlled).
Because of that, SAP programmed its Dynpro applications this way:
SAP update the database at the end of the whole application, when you save
and eventually, if some parts of the screens are handled by "external" applications SAP record intermediate updates via the "update task" (i.e. they are delayed until the COMMIT WORK is done at the end of the application). Note that SAP also frequently use the update task at the end only, but it's only for getting a better dialog response time.
Custom applications should follow the same principle.
I think you are trying to add lock mechanism. ABAP has own lock mechanism for objects. If user logout, close report or session terminated, system automatically unlocking it. I prefer use locking mechanism, example.
If you working different scenario; add new column for user and lock time to same table, and check user is online, otherwise remove lock.
If you want to not remove lock with user action, you can start new background job for 5 minutes with updating record. This job can check user and record, if user logout from report (t-code SM04), job removing record, otherwise reschedule it self.

Event raised when Access object saved?

This question references some events raised by the VBIDE. I'm looking for an event I can hook that is raised whenever an Access object is saved (form, querydef, module, class module, etc.).
If such an event is unavailable, I'm looking for workarounds. A project-wide save event or a code module change event would be acceptable alternatives. Perhaps there is some creative way to be notified when one of the "msys" system tables is updated and, ideally, which row.
Worst-case scenario, it looks like I can iterate through the CurrentDb.QueryDefs .LastUpdated or CurrentProject.AllForms/.AllModules/.AllReports .DateModified property and just poll it on some interval, but I would like to avoid that if possible.
There aren't any events that you can catch, but there is probably a better solution than polling the database objects.
The Database Window (that contains all of the tables, queries and other objects) will receive Windows messages when certain things happen in the User Interface. A quick look with Spy++ shows that the Database Window appears to receive a WM_ENABLE message when an object is saved. If you can trap that message using Win32, you might have the beginnings of a reliable "event".
Note that VBA UserForms can be used in Access Projects, but they don't appear in the Database Window, so that might be a problem.
Also, anything that programmatically changes/adds/deletes database objects might not trigger an automatic Database Window refresh or message.

Sendkeys On Disconnected RDP Session

I have a VB application which is scheduled. It focuses on some cmd windows and performs sendkey actions. This works fine when I have the RDP session open, it's only when I disconnect (not logoff) that the issue occurs. (This task is running on a virtualised server).
When I open the RDP session again after the task has ran, the application has thrown an error regarding the login permissions. I presume this is because the user is locked and therefore can't perform the actions?
I need to find a way around this, any help is much appreciated!
Don't use SendKeys.
Instead, if you have a program running in a command prompt, make sure the VB.NET program is the one to open it (or them) with Process.Start, and set the RedirectStandardInput property of the ProcessStartInfo object you pass to True. Then pass commands into the process's StandardInput property as though writing to a file.
This will avoid any focus-change problems, any problems to do with locked screen sessions, most if not all potential problems with integrity levels, most if not all problems with timing, and probably some other stuff I'm not thinking of.

Vb.net process close event

I'm asking a question which my mind goes blank on how it'll be coded... I'm trying to make a simple program that detects if a game closes, and when it does, it shuts down the computer.
Thanks!
Once you have the process, you can set EnableRaisingEvents to true, and the process will raise the Exited event.
As for shutting down the system - this is potentially more difficult. You can P/Invoke ExitWindowsEx, but it requires specific permissions (SE_SHUTDOWN_NAME) or it will fail.

How to allow users to quit out of long-running VBA tasks?

I have a routine that examines thousands of records looking for discrepancies. This can take upwards of 5 minutes to complete and although I provide a progress bar and elapsed time count, I'm not sure I want to encourage folk pressing ctrl-break to quit the report should it be taking longer than expected.
A button in the progress bar won't work as the form is non-modal, so is there any neat way of allowing users to quit in this situation?
You need DoEvents and a variable whose scope is greater than the scope of what you're running. That is, if it's just a procedure, you need a module level variable. If it's more than one module, you need a global variable. See here
Stopwatch at DDoE
Normally, the VB engine will tie up the processor until it's done. With DoEvents, however, VB allows the processor to work on whatever is next in the queue, then return to VB.
I don't think there is a way to do it like you would want it to work. VBA is a scripting language so when you start your procedure, it's gonna run until it's done. If you had another button somewhere that even WOULD let you click it while the original procedure was running, I'm not sure how you would reference that procedure and stop it.
You could do something like ask the user if they want to contine, but that would make it run even longer.
Also you could have your procedure check for a condition outside of Excel and keep running as long as it's true. Something easy might be check if a certain text file is in a folder. If you wanted the procedure to stop, open the folder and move the file. On your loop's next iteration, it wouldn't see the file and stop running. Cludgy, inefficient, and not elegant, but it would work. You could also have it check a cell, checkbox, radiobutton, basically any control in another Excel sheet running in another instance of Excel. Again cludgy.
CTRL+Break works. Accept it and move on. One neat trick about that though, is that if you password protect your code and they hit CTRL+Break, the debug option is unavailable and they will only get Continue or End.
If this is code that is run frequently, have you considered scripting something that runs it during times when a human is not using the computer? I used to run telnet screen scraping macros that would take hours to go through our widgets, but I always had them run either on a separate computer or when I wasn't there (nights/weekends).