When switch back from background, program gets stuck at every second

The program has no issue in editor(30 FPS). Problem only occurs in packaged game.
The program first runs fine in foreground (focused state), but if I switch to another program, and switch back, the program will be stuck( feels like FPS = 1, only gets responsive for a very short period of time(<=0.1 second), and becomes unresponsive(the whole computer, even the mouse and keyboard and other basic system components) for >=0.9 second.
At the unresponsive time, cpu and gpu usage is very high, >=95%
If I switch to another program, my program immediately runs without issue (30 FPS).
And once the problem happens, I can switch back and forth a few times and the problem goes away.

It’s a UI program that has a few user widgets, uses OSC server(acts like a simple socket server) plugin in blueprint, creates another thread in C++ that runs an exe file as external server, has a custom game engine and uses PreExit() to end the external server process.

I am a newbie and learning UE5 for less than a month. If you have no clue what I’m describing I am sorry. But if you have any method to help me debug it would be appreciated. For example, debug tools and methods. Since it only happens outside the editor, I don’t know what to do.

Update:My friend’s computer runs it fine. It might be the problem with 13/14th gen i7 i9 cpu problem.