简体   繁体   中英

How to keep the C# command line program still running when it loses focus

My command line C# program (running on windows VM) is connected to Redis, which accepts messages from channels subscribed to by Redis. If I don't touch my computer, it can still run fine once my finger After crossing the trackpad (I use a macbook) or minimize windows, he seems to have lost connection with Redis. At this point I have to type a few characters on the keyboard to keep the program running. Why? How can I solve this problem?

Anyway, thank you for giving me a suggestion, I wish you a good day.

this is code(The source code is really too big, I only took out part of it.):

static void Main(string[] args)
    {
        RedisConnectionString = $"{redisIp}:{redisPort}";
        Console.WriteLine($"Connecting to {RedisConnectionString}...");
        connection = ConnectionMultiplexer.Connect(RedisConnectionString);
        db = connection.GetDatabase();

        db.KeyDelete(SmcStreamDataList);

        smcSession = new Session();

        // Create pub/sub
        pubsub = connection.GetSubscriber();
        pubsub.Subscribe(testApiChannel).OnMessage((cmqMsg) => { SmcApiMessageAction(cmqMsg.Message); });

        Console.WriteLine("Starting now...");

        while (true)
        {
            Thread.Sleep(100);
        }
    }

Doesn't seems to be a problem of your program itself. I think it is more a problem of your machine setup. Unfortunately I don't have a Apple system, but I would guess that your virtual machine gets suspended when focus is lost or the window gets minimized.

Just for testing you could take a simple console application that tries to ping a machine on the net every 3 seconds and you write the result (including timestamp) as output. After starting the program make the same tests as with your faulty program and check afterwards if you see any bigger gaps then the estimated 3 seconds. If that works, then maybe cause every ping opens and closes a new network connection. So the next test would be to download a big file in your console application (just to simulate a steady network connection) and retry your procedure.

If any of these tests fails, than it is a problem of your Host OS or virtualization system and not of your program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM