Check whether an app or thread is already running when restarting qpython script - python-multithreading

For me the the behaviour of Android regarding the lifetime of an application is something mysterious. Specifically:
I start qPython and run a script. This script launches a thread that runs in the background.
If I exit the qPython interpreter by clicking the "back" arrow several times, the qPython window disappears. However, this does not seem to mean that qPython is stopped, as when I display the active windows by clicking the "square" key, the qPython window is still there, and if I select it it shows qPython in the last state I saw it before hitting "back".
Apparently, when the active windows are shown, if I drag the qPython thumbnail out of the screen, the thread running in the background is killed.
Question 1: should I infer that dragging the thumbnail of qPython off screen kills an app, including its background threads?
It happens to me to restart qPython and launch the script while a previous instance is already running. I then get errors because of conflicting access to the same resources.
Question 2: is there a way to know that a previous instance is running, so that my code can show an explicit message and prevent a resource conflict?

Related

How to detect when an app is launched on macOS?

I have a background process running on the user’s macOS machine. Its job is to detect whenever any app is launched on the machine. Currently, I am detecting the NSNotificationCenter’s NSWorkspaceDidLaunchApplicationNotification event. This works perfectly for detecting when an app is freshly launched (i.e. the app had no instance already running at that time).
But, on macOS, if we click the red cross button at the top-left corner, it generally closes the app window and the app continues to run in the background. This is also evident by the app icon visible on the dock with the dot indicator below it. If I click the app icon on the dock and then launch it, the NSWorkspaceDidLaunchApplicationNotification event won’t be triggered.
To track such events, I tried using the NSWorkspaceDidActivateApplicationNotification event. Using this event, I was able to detect all the app launch scenarios. The problem is that this event gets triggered whenever the app comes into focus such as switching windows using command+tab, clicking on its dock icon, changing between two apps, …
Is there a way to filter out these triggers or identify which action led to the trigger? Or is there some other event/ method I can listen to which gives the required filtered triggers? I only want to detect scenarios where a new window of the app is created.
What you seems to want is two different things, as was mentioned in comments, which should be processed separately to be reached.
To detect app launch, when the new process is started. You could use the NSWorkspaceDidLaunchApplicationNotification if it is enough (usually for visual user apps), or kqueue if it is not, or even EndpointSecurity framework to rule them all.
To track the window(s) in the already launched app. Visually, if the white dot under app dock icon is there, the app is still launched.
There is an Accessibility framework for this task, you could track the event of window creation, window destruction, get count of windows from target process id, visibility state and etc.
It is a bit abandoned and has no updates since maybe release, but it will work for you in most cases.

How to catch Delphi App shutting down when run in IDE?

Scenario
Using Delphi 2009 on Windows 10 I have an app that performs shut down operations such as logging and database manipulation.
Anywhere the user is able to exit the app I call Form.Close on the main form which calls a procedure to do the required operations. This works ok regardless of whether the user exits via the menu or by using the Windows task bar icon to close the window (as some of my users do).
Problem
When debugging the app in the IDE however, clicking the red square in the Delphi menu bar to stop the app running does not call Form.Close and hence doesn't call my shut down procedure.
Question
Is there a way to make the app correctly call form.close when shut down via the IDE so that I can ensure all parts get debugged?
Other info
I've looked here which describes a method using WMQueryEndSession to catch windows shut down messages but when I use that code in my app it is not called when the IDE shuts down the application, so maybe the IDE is using a different method.
Code used in my WMQueryEndSession procedure
procedure TFrmMain.WMQueryEndSession(var Msg: TWMQueryEndSession);
{intercept the shut down message and call our proc first
SafeFormClose is very simple and quick so no issues.
Also called first in case the inherited procedure shuts us down
before SafeFormClose has a chance to run}
begin
SafeFormClose; //Do our shut down and logging stuff
Msg.Result := 1; //Signal that it is OK for Windows to shut us down
inherited; //Do whatever the original handler would have done.
end;
If you are running the program in debugger, clicking the red square (reset button) terminates the program immediately. The debugger has full control of the program, and no more code is run after you click the reset button.
This is different than for example terminating the program in the task manager (in which case Windows would send a close signal to the program, which could reject it if it doesn't want to close yet).
You should design the program so that if it stops at any point, the next time it is started it fixes any problems caused by the reset (for example fixes corrupted database files). This is a real situation that can occur when power is lost or the operating system crashes. You cannot trust that your shutdown routine can be run every time.

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.

Keep my app as responder while calling activateWithOptions:NSApplicationActivateIgnoringOtherApps

I am making a vim-style "window manager" that takes text input, much like Alfred or Spotlight in Mavericks (in a floating panel).
The problem I'm having is when I call activateWithOptions: on a running application it steals focus from my window. I was hoping the problem would be solved by simply bring my app to the foreground again, however it seems the activation is running on a separate thread, and I end up activating my app before the original app gets activated.
I have tried reactivating when I receive NSWorkspaceDidActivateApplicationNotification, but that doesn't work either.
Ideally I'd like to pause execution until the application is focused for multiple reasons, since that would be the window I manipulate further.
Does anyone have any suggestions?

Is there a way of restarting an app when coming back from background?

I want to be able to restart the app when coming back from background. So if the user selects the app again it should start as if it were the first time it's open. I've been googling but couldn't find a way of doing this.
I was thinking in just add the main view of the app in applicationWillEnterForeground, but It would be great if I can deallocate resources.
You can't restart an app. What you can do is disable background support, so your app always completely terminates when closing.
"...you can explicitly opt out of the
background execution model by adding
the UIApplicationExitsOnSuspend key to
your application’s Info.plist file and
setting its value to YES."
Source: Opting Out of Background Execution.