Facepunch / sbox-issues

176 stars 12 forks source link

Whitelist Task.IsCanceled, Task.IsFaulted, Task.Exception #6536

Open GavrilovNI opened 1 month ago

GavrilovNI commented 1 month ago

What it is?

the following code

using System.Threading.Tasks;

public sealed class Test
{
    private void TestWhitelist()
    {
        var task = Task.CompletedTask;

        var isCanceled = task.IsCanceled;
        var isFaulted = task.IsFaulted;
        var exception = task.Exception;
    }
}

will not ne able to compile:

 09:40:21   PackageLoader/GameMenu Whitelist Error: System.Private.CoreLib/System.Threading.Tasks.Task.get_Exception()
 09:40:21   PackageLoader/GameMenu Whitelist Error: System.Private.CoreLib/System.Threading.Tasks.Task.get_IsCanceled()
 09:40:21   PackageLoader/GameMenu Whitelist Error: System.Private.CoreLib/System.Threading.Tasks.Task.get_IsFaulted()
 09:40:21   PackageLoader/GameMenu Couldn't load package.local.threadingtest - access control error

Why do you need to use this?

I need it to do more complicated things with Tasks

P.S. Couldn't reopen the issue, but here is the old one #4806 No, it doesn't look whitelisted, sorry garry

yuberee commented 1 month ago

You can use CancellationTokens and check for Token.IsCancellationRequested, or if you want to be super safe you can also wrap it in a try() and catch OperationCanceledException, or even dispose the token and catch ObjectDisposedException.

Still these options would be nice to have in GameTask rather than Task.