MechWarrior99
MechWarrior99
CC#
Created by MechWarrior99 on 1/21/2024 in #help
How to safely access parameters from Task (Porting from Unity IJob)
Ahh I see, thank you. I will keep .ConfigureAwait() in mind, wasn't aware of it! I was aware of LongRunning, but I don't think it applies in this case as I want the the generation to happen in 'real time' (less than ~100ms) as the user changes properties in the UI. So if it is taking longer than that, I need to do some more optimization haha.
36 replies
CC#
Created by MechWarrior99 on 1/21/2024 in #help
How to safely access parameters from Task (Porting from Unity IJob)
Appreciate the info! And yeah, as Burrito said, this is not in Unity, just familiar with the Unity Jobs system and way of doing things. And trying to understand how you would do the same in a plain C# app.
36 replies
CC#
Created by MechWarrior99 on 1/21/2024 in #help
How to safely access parameters from Task (Porting from Unity IJob)
Alrighty, well this definitely gives me clarity on it all. Thank you for the help and insight as always! 😄
36 replies
CC#
Created by MechWarrior99 on 1/21/2024 in #help
How to safely access parameters from Task (Porting from Unity IJob)
What would be the way you would recommend doing it? Variable capture? Record? Struct?
36 replies
CC#
Created by MechWarrior99 on 1/21/2024 in #help
How to safely access parameters from Task (Porting from Unity IJob)
Ooh yeah I totally forgot you could define records like that!
36 replies
CC#
Created by MechWarrior99 on 1/21/2024 in #help
How to safely access parameters from Task (Porting from Unity IJob)
Huh, didn't realize they were immutable. Interesting!
36 replies
CC#
Created by MechWarrior99 on 1/21/2024 in #help
How to safely access parameters from Task (Porting from Unity IJob)
Oh it does?
36 replies
CC#
Created by MechWarrior99 on 1/21/2024 in #help
How to safely access parameters from Task (Porting from Unity IJob)
I guess I could forgo the 'IJob' structs and just capture local variables as you showed before. Would you say that capturing the variables is the more 'C# standard' way?
36 replies
CC#
Created by MechWarrior99 on 1/21/2024 in #help
How to safely access parameters from Task (Porting from Unity IJob)
I guess that is possible, but feels like worse UX. Will probably just go the Unity Jobs approach and limit it to structs, or clones in some cases.
36 replies
CC#
Created by MechWarrior99 on 1/21/2024 in #help
How to safely access parameters from Task (Porting from Unity IJob)
That feels weird to do to me. I mean, makes sense, but feels weird. And I guess Foo would need to be cloned or something to be safe.
36 replies
CC#
Created by MechWarrior99 on 1/21/2024 in #help
How to safely access parameters from Task (Porting from Unity IJob)
Jobs gets around it by only allowing structs, and putting it all in the struct doing the executing. Which is what I was thinking of doing (or making a clone of SomeClass and passing it)
36 replies
CC#
Created by MechWarrior99 on 1/21/2024 in #help
How to safely access parameters from Task (Porting from Unity IJob)
How do I do that safely
36 replies
CC#
Created by MechWarrior99 on 1/21/2024 in #help
How to safely access parameters from Task (Porting from Unity IJob)
Right, that is the question I was asking.
36 replies
CC#
Created by MechWarrior99 on 1/21/2024 in #help
How to safely access parameters from Task (Porting from Unity IJob)
Well lets say I have something like this
class Generator
{
public SomeClass Foo {get; set;}
public int Bar {get; set;}

public async void Generate()
{
await Task.Run() =>
{
for (int i = 0; i < Bar; i++)
{
Foo.DoThing(i);
}
}
}
}
class Generator
{
public SomeClass Foo {get; set;}
public int Bar {get; set;}

public async void Generate()
{
await Task.Run() =>
{
for (int i = 0; i < Bar; i++)
{
Foo.DoThing(i);
}
}
}
}
The values in Foo could be changed while the task is running which would be not great, right? Or am I misunderstanding something?
36 replies
CC#
Created by MechWarrior99 on 1/21/2024 in #help
How to safely access parameters from Task (Porting from Unity IJob)
Yeah I figured that is the case. But I am not sure what is the 'right' way then since the values can change from the UI during generation. Accessing them directly would be a no go, right?
36 replies
CC#
Created by yatta on 8/18/2022 in #help
How to write function in .net6? [Answered]
You need to add a return at the very end where it is not based on any conditions or anything. Sort of like fallback if everything else fails/doesn't run at least that will return.
9 replies
CC#
Created by yatta on 8/18/2022 in #help
How to write function in .net6? [Answered]
No, because now the for loop will only run ones.
static int Search(int[] nums, int target)
{
for(int i = 0; i < nums.Length; i++)
{
if (nums[i] == target)
return i;
}

return -1;
}
static int Search(int[] nums, int target)
{
for(int i = 0; i < nums.Length; i++)
{
if (nums[i] == target)
return i;
}

return -1;
}
9 replies