✅ System threading tasks
I am trying to accomplish a checker to make sure a task/function doesn't take to long to run. If it does it must end. Basically kill the task. This is the code I have:
But with this it is creating cross threading. I had this issue before but with accessing data, and I did this to solve it:
This worked, but now the panel (error on picture) that is trying to be accessed is to update it...I update a lot of the information on the form so the same workaround cannot be done. Please lemme know if you need more information.
11 Replies
Task.Run is most likely the culprit here for moving the task to the other thread, can you make that task return data that the main thread awaits and then uses to update the UI?
Also if you want, you can install Polly (https://www.nuget.org/packages/polly/) which has timeout as well as a bunch of other resilience strategies built in
Polly 8.3.1
Polly is a .NET resilience and transient-fault-handling library that allows developers to express resilience and transient fault handling policies such as Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback in a fluent and thread-safe manner.
I would be returning an array/object with like 15 items. Would that be ideal? To answer I can make the task return data
Will check this out
Shouldn't be an issue, technically it's returning a reference to the array anyway so it's just the size of a ref, not a copy of the full array
Do you think creating a class entities in this scenario would be good?
formsValues
for intance
And then just have the values for every tool thereNot sure I know what you mean, to wrap the array response from the task? Probably not
Yeah, or format it basically. Also t prevent my array from putting information in the wrong fields.
So the above is an example of what I speak about. If my function doesn't return the version for instance, it can put
""
in that field on the field...and if I want a specific value I can get that. kind of like a dictionary.Oh so you'll have an
Item[]
response?
Sounds reasonableYes
The only concern I have is
data races
where we now trying to access the same data from different threads
I've had the issue before and to this day not sure how to deal with itDon't share mutatable state and you're golden
Fair, so an await and return of a new entity based on the class should cover that right?
Then just write the returned values into the existing data...
That sounded so scuffed, do you understand my thought process here?
@🅱ladesfist Imma take this logic into my project, lemme know if you notice anything wrong here or potential issues please
I am using the class I sent earlier
Seemed to work. Thanks ^^