简体   繁体   中英

Prevent thread from stopping execution when debugging

I have:

static void Main(string[] args)
{
    Task t = new Task(() =>
    {
        while (true)
        {
            Console.WriteLine("Test: " + DateTime.Now);
            Thread.Sleep(1000);
        }
    });
    t.Start();

    while (true)
    {
        Console.WriteLine("Main thread Test: " + DateTime.Now);
        Thread.Sleep(1000);
        Debugger.Break();
    }

    return;
}

why does task t stops execution when main thread stops? is there a way I can have task t continue executing regardless if I place a break point on the main thread?


edit

I don't have Debugger.Break(); in my real code. I send a ping to a computer every 4 seconds to let the other computer know I am connected. The problem is that when I start debugging my code, the ping is not send every 4 seconds. It will be nice if I can keep this program as one program and do not have to create a separate program responsible for sending that ping every 4 seconds.

No you can't. Thats simply how breakpoints work - all threads are breaked!

For more details: https://stackoverflow.com/a/21339186/2243584

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