Create a background proccess that terminates a program on KeyPress (VB 2013) - vb.net

I'm look for code which can help me accomplish what the title suggests.
I want this because I have an issue with a program which is quite buggy (it's a game), and if It freezes, and results in a black screen. Even after using Shift+Ctrl+Esc, Ctrl+Alt+Del, Alt+F4 and the rest of them, I acn't seem to end the program. So I thought that a KeyPress event would work, as it doesn't require me to use the UI in any way.
However, I've never used VB for anything other than Form's, so I have no idea how to start on something which runs in the background.
Thanks :)

A background app won't help in this case.
Ctrl+Alt+Del is specail key combo that is handled directly by Windows and can't be intercepted by a running program.
What this means is that if your game locks up and Ctrl+Alt+Del doesn't bring up the Windows menu, then the game has corrupted the system. The only fix at that point would be a restart.
That being said, you can kill a running process using VB.NET like this:
For Each program As Process In System.Diagnostics.Process.GetProcesses
If program.ProcessName = "ProcessName" Then
program.Kill()
End If
Next
Read more about it on the MSDN.
Okay, so Windows isn't actually locking up, but you just need some way to kill the process. Use the above code, replacing "ProcessName" with the name of your game process, and then either:
(basic) Turn the VB.NET code into an .exe file. Then set up a keyboard macro (using your keyboard software or some freeware) to launch the .exe you made which will kill all running instances of the given process. or...
(advanced) Import "user32.dll" into the VB.NET code, call SetWindowsHookEx with a hook id of 13, a pointer to a LowLevelKeyboardProc function, the handle to your running program, and a thread id of 0. Then, whenever a key is pressed in any program, your KeyboardProc function will be invoked. If the key(s) pressed match your desired kill-key combo, then run the above process killing code. This looks like it may give you more step-by-step instructions.

Related

Visual Basic: Stopping a background thread from the main thread possible?

I am writing a program (in Microsoft Visual Basic) to control a remote instrument (an XY axis microscope stage). As an example, the user must initially calibrate the stage by clicking a GUI button. It takes some time to accomplish the calibration process and I do not want to freeze the main GUI, so I execute the call on a background thread, as follows:
Call New Thread(AddressOf CALIBRATE_STAGE) With {.IsBackground = True}.Start()
So that works well, but we can imagine that for some reason, the user might need to halt the calibration before it is completed. The halt command must originate from a button on the GUI but it cannot be run from the main thread, or a HUGE instability results. (Basically, a program crash, probably because the sub thread was incorrectly managed. The only fix is to completely reboot the computer.)
I think what I need to do is something like this pseudo-code:
Press HALT button on GUI
Execute the STAGE HALT command on the same sub-thread where the CALIBRATE_STAGE command was launched
Once the stage is halted, properly abort the sub thread
Trouble is, I do not know how to do acquire the specific sub-thread on which CALIBRATE_STAGE is running. I do know its ThreadID, but given that integer, how do I launch a new process on that thread? Does anyone have some ideas?
Or maybe someone knows this idea of mine will not work and has a better solution?

Halt code at dialog (msgbox()) command in IDE

Title quite much says it all.
In VB6, and in VBA/Access it was possible to hit break key, and jump into debug mode when using the msgbox() command.
Is there a way to do this in vb.net (desktop/winforms) applications?
Often, some code will toss up a dialog box, and it is rather nice then to jump into debug mode as a result of that message box having been displayed.
Edit
Ok, hitting pause button in most applications work, but in this application, when I hit pause, then I get this:
Edit two:
Ok, I have discovered the reason for this behavior. I have the application Frame work box un-checked. The reason for this is I did't want to specify the main application form as startup form, and I desired additional control over what occurs if the main startup form (that I don't specify) is closed. Thus, my main application form is launched via application.Run(my form)
It thus seems that due to starting the main form as a new separate application thread (which is the result of using application.Run(), then you can't use ctrl-break, or more common use/hit the pause button in the IDE to halt the code. Hitting pause will thus display that the application is running a main app thread, which indeed is the case since I use applicaiton.Run() to launch the main form from the classic and traditional Sub Main().
Edit 3
A way to fix this, and enable the pause key to work is to un-check in tools->debugging the [ ] Enable Just My Code. This will thus allow debug mode of the other "main" application thread.
Hmm. [CTRL][BREAK] clears the dialog box. However, clicking the pause button in the IDE will do what you want.
Alternatively, select Debug > Break All from the menu.

ShowFileDialog1 Freezing

Okay, I have had the most aggravating problem with OpenFileDialog1. I have a program that I've been using for some 8 months, and in the past month, the program has begun to hang randomly when utilizing the OpenFileDialog1.ShowDialog() function. I have already read through all of the other posts about multi-threaded vs single threaded application. This did not fix it. Enabling the "Show Help" button did not fix it. I am mostly at a loss. here is a thorough walkthrough of the bug:
Run the application. I can always use the Open File button a few times with no problems. It freezes randomly after the program has been running for awhile.
The freeze happens after I push the ShowDialog button, and never displays the Open File Dialog window. The entire program locks up and hangs. If I pause it, Visual Studio doesn't show an error. It underlines the OpenFileDialog1.ShowDialog() in green, which is very odd.
I have found a way to break the freeze. Simply run a second instance of the program and use the OpenFileDialog function. As soon as it loads the file in the second instance, the first instance unfreezes. However, this is not a fix.
The only thing I can think of that may be causing this is the program also uses a WebBrowser1 control. It only seems to happen AFTER the WebBrowser control, which is on a seperate form, not the main form, has been initiated and utilized. Does this make any sense at all?
Thank you for anyone who can help me. I am about to tear my hair out.
Debug your program with dnspy, And when the software freezes, you will be able to see within the dnspy the actual code even if it is in a third party DLL.
I have solved this problem. It was quite unsolveable based on my description above, but hopefully I will help someone with this solution. The error is related to using the IE11 Emulation Control (11000) in the WebBrowser1 control. For some reason this interferes with OpenFileDialog and causes it to hang. I have no idea why. I changed my WebBrowser1 to use IE9 Emulation Control (9999) and the error has gone away. Thank you to those who looked into this. This is a registry entry in HKEY_CURRENT_USER.

Kill application not Process - VB

I have a program that out of my control will bring up a message box (I didn't write or have source code)
I am trying to find out if it is possible to write a program (visual studio) that can at a set time, say every 10 minutes close all open applications(msgbox) but keep the process going? The application/task name is different to the process name.
It runs on XP and 7.
I've tried google but haven't managed to find what I'm after, all i can find is process kill. Not 100% sure on correct terminology.
You might be better-off writing a program that finds a current open dialog (window, messagebox, etc) and closing it. Sending an Esc keystroke is a pretty easy approach. Here is another SO article that talks about doing this: How to send a mouse click event to a hidden window?

Detecting a message box opened in another application

I am developing a windows service, in VB.NET, that launches a legacy application that performs some work. The service acts as a wrapper around the legacy app allowing users to automate an otherwise manual operation.
Everything is working great, except occasionally the legacy app displays a messagebox. When it does this the process halts until the message box is closed.
As the service will be running on a server there will be no user to close the message box.
The service launches the legacy application in a System.Diagnostics.Process.
Is there way to detect that a message box has been displayed by a process that I have started using System.Diagnostics.Process? And is there a way to close the messagebox through code?
I also found that EnumChildWindows does not return the MessageBox. But I found a site that showed me how to do it. http://vbcity.com/forums/t/105842.aspx
You want to call GetWindow passing in GW_ENABLEDPOPUP. It worked like a charm. Thanks to Scott Waletzko!
Use FindWindow to find the app, the use EnumChildWindows to enumerate all it's childwindows until you find the messagebox (if the messagebox isn't a direct child of the main window of the app you might have to have recursive calls I think).
You might be able to skip the FindWindow call and instead use the MainWindowHandle property of the Process, but I haven't checked if that works.
A good tool for looking at all this is Spy++ which can help you see some information you can get hold of about a running process.