C
C#2y ago
annoyingb

✅ is there a way to get the method name from Task?

14 Replies
Kouhai
Kouhai2y ago
What do you mean?
annoyingb
annoyingbOP2y ago
if I create a thread that just runs a function, how do I get the name of that function?
Kouhai
Kouhai2y ago
You can't get it inside the the thread unless you pass it somehow. You can technically use reflection as well, but it's better to just pass it imo
annoyingb
annoyingbOP2y ago
could I do this with async or something?
Kouhai
Kouhai2y ago
No, async won't work with getting the name via reflection
annoyingb
annoyingbOP2y ago
oh
Kouhai
Kouhai2y ago
I'm not exactly sure what are you trying to achieve though 😅
annoyingb
annoyingbOP2y ago
I am trying to make a loading screen that runs on another thread asynchronously and it will tell you what is currently running.
Kouhai
Kouhai2y ago
Ah so basically, the main thread loads assets and you want your loading screen that runs on a different thread to show the user the current step i.e Loading textures Loading 3d objects... etc Right?
annoyingb
annoyingbOP2y ago
yeah like I have a list of actions
int incrementAmount = 1 / Actions.Count;
foreach (Action action in Actions)
{
Task loadTask = new Task(action);
currentlyRunning = action.Method.Name;
loadTask.Start();
await Task.WhenAll(loadTask);
progress += incrementAmount;
Debug.WriteLine(loadTask + " has finished");
}
int incrementAmount = 1 / Actions.Count;
foreach (Action action in Actions)
{
Task loadTask = new Task(action);
currentlyRunning = action.Method.Name;
loadTask.Start();
await Task.WhenAll(loadTask);
progress += incrementAmount;
Debug.WriteLine(loadTask + " has finished");
}
nvm i just found out
Kouhai
Kouhai2y ago
Okay, couple of notes here First Why are you newing a Task manually? You're awaiting it couple of lines after creating it Second, why are you using Task.WhenAll instead of await?
annoyingb
annoyingbOP2y ago
I was doing it with multithreading but I changed it and realised the answer
currentlyRunning = action.Method.Name;
await Task.Run(action);
progress += incrementAmount;
Debug.WriteLine(currentlyRunning + " has finished");
currentlyRunning = action.Method.Name;
await Task.Run(action);
progress += incrementAmount;
Debug.WriteLine(currentlyRunning + " has finished");
is this better?
Kouhai
Kouhai2y ago
Yeah, it's much better imo 😅
annoyingb
annoyingbOP2y ago
alr thanks
Want results from more Discord servers?
Add your server