Converting an app that uses DoEvents?

I’m trying to convert a plain C# app into a MonoBehaviour but I’ve run into a snag when stopping it.

The code spawns a thread to communicate with some hardware and it needs to wait until it is properly shut down before it can exit.

Is there any way to delay an OnApplicationQuit so that the update function will keep being called until it is finished? The example code I have is this, but the application line is conflicting with the unity Application library.

		mUpdateStatusThread.Abort();
		while(mUpdateStatusThread.IsAlive)
		{
			Application.DoEvents();
			Thread.Sleep(1000);
			Debug.Log("waiting for disconnect...");
		}
		
		mUpdateStatusThread = null;

You can use CancelQuit to prevent the shutdown in a standalone application, at least unless it was force closed.

What I would not recommend to do is using any threading that touches / impacts the UnityEngine namespace and the main thread

CancelQuit only works in the standalone, right? Is there any way I can do anything in the editor? I would like to still be able to press the play button to stop things without unity crashing.

no in the editor you can’t do anything actually.
The editor insta kills the whole thread the client is running in without giving it an option to prevent it.

For editor testing you optimally add an #if UNITY_EDITOR based ongui or ongui part that shows a button which allows you to terminate the plugin code like you would do with wait and then kill and after that stop the player