1

I've developed a program (winforms application, not a service) in C# that runs on a windows server.

The program starts multiple times based on requests from outside the server.

From time to time I see that the program is "Suspended" for an unknown reason. I think it is related to a lack of resources, but not sure. enter image description here

How can I prevent windows from suspending my program?

Update To be clear, I know that the program crash and it is OK. What I'm asking is not how to improve performance \ prevent the crash, but how to remove the process from the process list \ prevent this suspended status?

Omri
  • 887
  • 8
  • 24
  • Can you be more specific about the problem? Is there something the program stops doing when it is suspended? Is it a background process or an interactive program? – HackSlash Mar 31 '21 at 20:00
  • NOTE: There is a Windows OS bug that leads to programs getting stuck in suspended state. Please run window update, install any missing major versions, and reboot. Let us know if the problem goes away. – HackSlash Mar 31 '21 at 20:07
  • The windows server (2019) is updated and the problem still occurs. Do you have a reference for that windows bug? – Omri Apr 02 '21 at 06:01

4 Answers4

0

Depends in your hardware/software configuration, it's hard to know where is your bottleneck.

I recommend instead to do Multi-thread/task app where you're able to control threads and asign priority, resources, stop, resume, abort, etc...

use on command console to start and check if happends the same but with the parameter high:

start /HIGH <ProgramPath>

Read more how to change priority on executables

Task Scheduler on windows servers MSDN -> Priority

(It's only an opinion, start digging about others solutions.)

Lulz
  • 32
  • 1
  • 1
    Thank you, however, i'm not looking to improve performance. just "signal" windows that this process should not be suspended. – Omri Mar 30 '21 at 06:50
0

You must set the ServiceBase.CanPauseAndContinue Property to False in the constructor of the service before it is started.

NOTE the side effect is:

If CanPauseAndContinue is false, the SCM will not pass Pause or Continue requests to the service, so the OnPause and OnContinue methods will not be called even if they are implemented. In the SCM, the Pause and Continue controls are disabled when CanPauseAndContinue is false.

For more information see this Microsoft Doc

HackSlash
  • 4,944
  • 2
  • 18
  • 44
  • The program is not a service, but a winforms application. I've edited the question to clarify it. – Omri Mar 31 '21 at 19:56
  • Maybe it should be.... Does it have an interface that the user clicks on? Doesn't that take it out of suspend? How could it be suspended if it's in use? – HackSlash Mar 31 '21 at 19:58
  • It can't, the program host a web browser that "leaks" memory based on the site that it hosts. Therefore, for each request, a new process is started and closed when done. – Omri Apr 02 '21 at 06:03
0

There are multiple methods of keeping an app awake.

One method would be to request a deferral and then only mark that deferral complete when you are done.

First you need a deferral object that will remain in scope of your process

SuspendingDeferral deferral

Then you need to override OnSuspending

async protected void OnSuspending(object sender, SuspendingEventArgs args)
{
    deferral = args.SuspendingOperation.GetDeferral();
    await SuspensionManager.SaveAsync();
}

Then you need to mark the deferral complete when your process is done doing whatever it was doing

    if (deferral is not null) { deferral.Complete(); }

Full details can be found in the Microsoft docs

For discussion of other methods see this thread: How to Disable UWP App Suspension?

HackSlash
  • 4,944
  • 2
  • 18
  • 44
  • This is not a UWP, but a winforms program as stated in the question. As far as I know there is no OnSuspending \ SuspensionManager for winforms. – Omri Apr 04 '21 at 09:43
0

Technically the process is suspended but if you look at the memory consumption of 32K you can tell it was not suspended. Your process did crash with an unhandled exception which in turn triggers Windows Error Reporting to take a memory dump.

This involves some kernel magic which keeps a process handle in the kernel (System process) alive. It is not a big deal or memory leak since the process did already terminate. Only the PEB (Process Environment Block) the 32K which includes mostly command line, loaded dlls and environment variables are still there.

I would not worry about suspended processes but why your process did crash with an unhandled exception. This could even be a feature to make programers aware that their process did crash by looking at a looong list of suspended processes in Task Manager ;-).

Since it is a .NET Application you will find a generic error messages that a process did crash in the Application event log and a .NET Runtime logged error message with more exception details. enter image description here

Perhaps that is already enough to keep you going to fix your issue. If not you can configure WER to create a full memory dump via a registry setting (https://learn.microsoft.com/en-us/windows/win32/wer/collecting-user-mode-dumps). Now you have for each "suspended" process a full memory dump you can load into Visual Studio to look further what was the issue.

To further check who holds your process handle still open you can use handle e.g. ProcessHacker (the better Process Explorer) https://processhacker.sourceforge.io/nightly.php

enter image description here

If something else is happening you can see with this tool who is holding any outstanding handles to your process open. But I strongly suspect it is the Windows Kernel.

Alois Kraus
  • 13,229
  • 1
  • 38
  • 64
  • Yes, this is definitely a crush in a third-party library in my program. and this is the reason I'm opening several processes. You wrote "I would not worry about suspended processes but why your process did crash with an unhandled exception." - but I'm not worried about the crash (I can handle it), but removing the process completely from the processes list. 32K not sound like a lot, but after some time I have hundreds of processes in a "suspended" state. So the question is - how can I remove the process or signal windows to remove the process, – Omri Apr 04 '21 at 09:48
  • Fix the bug or handle the exception or enable the in your app.config to continue running even for unhandled exceptions. – Alois Kraus Apr 04 '21 at 09:56
  • I've tried it already (legacyUnhandledExceptionPolicy) - with no success. I can't fix the bug or handle the exception since it is a third party code – Omri Apr 04 '21 at 12:15
  • What is the stacktrace of your crash? – Alois Kraus Apr 04 '21 at 17:42