Execute task in an already existing thread

Michael Zdarsky 96 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

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,164 questions
{count} votes

16 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,026 Reputation points
    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

  2. Michael Zdarsky 96 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

  3. Bruce (SqlWork.com) 54,621 Reputation points
    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

  4. Michael Zdarsky 96 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

  5. Bruce (SqlWork.com) 54,621 Reputation points
    2022-01-20T17:54:42.96+00:00

    Tasks run on the Task thread pool. You can write your own thread pool scheduler.

    https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.taskscheduler?view=net-6.0

    what is your real use case?

    if you want the task code to run on a particular thread, normally you would pass that thread the code (delegate) to execute and not use Tasks, you can use a synchronized queue of Actions that the thread monitors, and queue the Action from any thread.

    note: you can not call a method on another thread, you can only send messages via some agreed upon protocol. Tasks and the Task thread pool scheduler have defined a private protocol to pass the delegate to execute

    0 comments No comments