Execute task in an already existing thread

Michael Zdarsky 101 Reputation points
2022-01-19T17:19:45.25+00:00

Hello,

I would like to execute a task on an already existing thread.

How can I tell the task to use this specific thread?

I saw there is a task scheduler available in .net but if this would be the vehicle to implement
I have no glue how to do that.

Any hint?

Thanks
Michael

Developer technologies C#
{count} votes

16 answers

Sort by: Most helpful
  1. Michael Zdarsky 101 Reputation points
    2022-01-22T10:03:25.3+00:00

    Right,
    you know the best solution for us, interesting.

    This is exactly what we hat to avoid, multiple threads.
    Don't tell us what the best solution is.
    If you have a use case in your mind, regarding "BEST SOLUTION", do it for your implementation.

    As stated before, the solution can be provided with a task scheduler,
    I provided a task scheduler which is providing one thread as a sample.

    It is not much code to exchange the thread in my samle against a message loop.
    This is realy not the point.

    And even with your statement that the code could end up with dead locks you are wrong.

    Per se, dead locks cannot happen in single thread context.

    In addition if you mean, that a dead lock can happen between the calling thread and this single tread,
    than you are rigth, but it can also happen when using thread pool threads.

    This has NOTHING to do with thread design
    This has to do with locking, critical sections, mutexes etc.
    If used wrong -> deadlock

    And you are running in a dead lock, when the calling thread is doing a task.wait() AND the inner method is awaiting.
    Than the task continuation cannot proceed, because the caller thread is blocke with the wait call.

    But this is a wrong usage of wait and await and has NOTHING to do with single/multi threading.

    No deadlock occur in our situation.

    Instead of giving other people the best solution, just answer the question and do not provide your best solution you have in mind,
    just because of you think thats the way to do.

    1 person found this answer helpful.
    0 comments No comments

  2. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-01-20T14:52:52.017+00:00

    Check out several code samples in this project.

    Each code sample is the project are easy to follow but the code spans forms and classes such as in real life rather than have all code in a Windows Form, a WPF Window etc,

    Execution is in this form

    Classes used
    166750-classesused.png

    Screenshots

    166710-asynccode.png

    0 comments No comments

  3. Michael Zdarsky 101 Reputation points
    2022-01-20T15:17:00.807+00:00

    Hello Karen,

    thanks also to you, but also the question back.

    Where do you consider my requirement?
    I am not asking how to call methods asynchronuosly.

    This is NOT the question.

    Thanks Michael

    0 comments No comments

  4. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2022-01-20T16:57:54.43+00:00

    Tasks support synchronization of async operation, and managing callbacks.

    Whether a task runs on the current thread of a new thread depends on the code the task is wrapping. async operations supported by the O/S (file, network, timer, etc), use the calling thread. the the task code creates a new thread to get async behavior, then the code and callback happens on the new thread.

    also there is a difference between

    var result = SomeTask().Result;
    Console.WriteLine("still calling thread");

    and

    var result = await SomeTask();
    Console.WriteLine("now the thread SomeTask called back on");

    because await uses callbacks (continuation) rather than blocking the current thread. this because

    var result = await SomeTask();
    Console.WriteLine("now the thread SomeTask called back on");

    is translated to

    SomeTask().ContinueWith(() =>  Console.WriteLine("now the thread SomeTask called back on"));
    

    if you need to stay on the same thread, use threads and Thread.Join() rather than awaitable Tasks.

    0 comments No comments

  5. Michael Zdarsky 101 Reputation points
    2022-01-20T17:13:14.86+00:00

    Hello,

    and also thanks to
    But again you didn't take my requirment into account.

    How do I execute a task on a predefined thread?

    I have a thread and I want execute a task on exactly that thread.

    Why is thas so difficult to understand.
    I don't need any hints regardins other solutions.

    So either tell me how it is to implement or don't provide another solution.

    I have another solution and again i want do use awaitable tasks.

    It seems that it is possible to implement an own task scheduler to achieve that, but are there
    .net feature availabe to tell the task or an existing task scheduler which thread to use to execute the task.

    Thanks
    Michael

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.