Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Task StartNew Action in C++/CLI

In C++/CLI is there any way to do the following? (I think answer is no because of no Action support?)

public ref class MyClass {
public:
    void TaskMethod();
    void Start();
}

void MyClass::Start() {
    Task^ myTask = Task::Factory->StartNew(??TaskMethod??);
}
like image 379
Ricibob Avatar asked Jun 05 '13 16:06

Ricibob


People also ask

How do I call startnew from a task?

Calling StartNew is functionally equivalent to creating a Task using one of its constructors and then calling Start to schedule it for execution. Starting with the .NET Framework 4.5, you can use the Task.Run (Action, CancellationToken) method as a quick way to call StartNew (Action, CancellationToken) with default parameters.

Is action supported in C++/CLI?

(I think answer is no because of no Action support?) public ref class MyClass { public: void TaskMethod (); void Start (); } void MyClass::Start () { Task^ myTask = Task::Factory->StartNew (??TaskMethod??); } Show activity on this post. Action is just a delegate, which is fully supported in C++/CLI.

What is the best way to initiate a task asynchronously?

If we ever engage in a discussion about task-based asynchronous programming in C#, almost certainly we are going to see some examples using either Task.Run or Task.Factory.StartNew. They are the most widely used ways for initiating a task asynchronously, in most cases in a similar fashion.

How to call startnew with default parameters in the action object?

Starting with the .NET Framework 4.5, you can use the Run method with an Action object as a quick way to call StartNew with default parameters. For more information and code examples, see Task.Run vs Task.Factory.StartNew in the Parallel Programming with .NET blog. Creates and starts a task for the specified action delegate and cancellation token.


Video Answer


1 Answers

Action is just a delegate, which is fully supported in C++/CLI. (You might be getting it confused with lambdas, which do not have support in C++/CLI.)

Here's the syntax to create a delegate in C++/CLI.

Task^ myTask = Task::Factory->StartNew(gcnew Action(this, &MyClass::TaskMethod));
// For non-static methods, specify the object.      ^^^^ 
// Use the C++-style reference to a class method.         ^^^^^^^^^^^^^^^^^^^^
like image 181
David Yaw Avatar answered Jan 01 '23 08:01

David Yaw