I guess that $tray->runApplication starts the event loop for your program. That loop only returns once your program quits.
If you have what is basically a console program without an event loop and event architecture, you will have to post a Short Self-Contained Correct Example to demonstrate your problem. Then,
we can maybe suggest how to change your program logic so it works together with the Windows event loop.
As the documentation of Win32::SysTray says, it invokes Win32::GUI::Dialog, so basically it will involve rewriting your application in terms of Win32::GUI.