Hi @zequion , Welcome to Microsoft Q&A,
The behavior you observe is due to the way value types (such as structures) and reference types (such as classes) are handled in C#. When you pass a structure by value, you are passing a copy of the structure. However, when you pass a structure by reference (using ref), you are passing a reference to the original structure, allowing modifications to affect the original structure.
private void Fcn_Task_Async(ref Name_Task_Async.St_Task_Async StTask_Async)
{
// Create a local copy of the original struct
Name_Task_Async.St_Task_Async StTask_Async_ = StTask_Async;
// Start a new Task to modify the local copy
StTask_Async.StTask_Async_Tasks[Task_index_].Task = System.Threading.Tasks.Task.Factory.StartNew(() =>
{
Name_Task_AsyncAdd.Cls_Task_AsyncAdd.Fcn_Task_Async_Step2(ref StTask_Async_);
});
// Wait for the Task to complete
System.Threading.Tasks.Task.WaitAll(StTask_Async.StTask_Async_Tasks[Task_index_].Task);
// After the Task completes, assign the modified local copy back to the original struct
StTask_Async = StTask_Async_;
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.