Lilly Zen
Lilly Zen

Reputation: 13

Can we have a while loop logic in Task.Run?

I want to do something like:

Task.Run(while(true)
{
doSomething()
}

Is there a proper way of doing this?

Upvotes: 0

Views: 1107

Answers (2)

Sameer Khan
Sameer Khan

Reputation: 49

You might want to add some other options.

  1. The task creation option should indicate that this is a long running thread.

  2. You may want to include a task cancellation token to allow this thread to terminate.

Upvotes: 1

John Kraft
John Kraft

Reputation: 6840

Task.Run(() => {
    while (true) {
        doSomething();
    };
});

Upvotes: 4

Related Questions