Files icon indicating copy to clipboard operation
Files copied to clipboard

Code Quality: Convert into `await using` statement or declaration

Open gumbarros opened this issue 1 year ago • 10 comments

Description

using (var stream = File.OpenRead(_item.ItemPath))

to

await using (var stream = File.OpenRead(_item.ItemPath))					

In .NET, classes that own unmanaged resources usually implement the IDisposable interface to provide a mechanism for releasing unmanaged resources synchronously. However, in some cases they need to provide an asynchronous mechanism for releasing unmanaged resources in addition to (or instead of) the synchronous one. Providing such a mechanism enables the consumer to perform resource-intensive dispose operations without blocking the main thread of a GUI application for a long time.

The IAsyncDisposable.DisposeAsync method of this interface returns a ValueTask that represents the asynchronous dispose operation. Classes that own unmanaged resources implement this method, and the consumer of these classes calls this method on an object when it is no longer needed.

Source: https://stackoverflow.com/questions/58610350/what-is-the-difference-between-using-and-await-using-and-how-can-i-decide-which

Concerned code

Generated using Jetbrains Rider:

<platforms><Files.App>\Utils\Storage\StorageItems\VirtualStorageItem.cs (1 issue) <platforms><Files.App>\Utils\Storage\StorageBaseItems\BaseStorageFile.cs (1 issue) <platforms><Files.App>\Utils\Shell\LaunchHelper.cs (2 issues) <platforms><Files.App>\Utils\Storage\StorageItems\ZipStorageFile.cs (7 issues) <platforms><Files.App>\Utils\Storage\StorageItems\VirtualStorageFile.cs (4 issues) <platforms><Files.App>\Utils\Storage\Operations\ShellFilesystemOperations.cs (4 issues) <platforms><Files.App>\Utils\Storage\StorageItems\ZipStorageFolder.cs (9 issues) <platforms><Files.App>\Utils\Storage\StorageItems\NativeStorageFile.cs (4 issues) <platforms><Files.App>\Utils\Storage\StorageItems\SystemStorageFile.cs (9 issues) <platforms><Files.App>\ViewModels\Properties\HashesViewModel.cs (1 issue) <platforms><Files.App>\Utils\Storage\StorageItems\FtpStorageFile.cs (3 issues) <platforms><Files.App>\Utils\Cloud\Detector\SynologyDriveCloudDetector.cs (3 issues) <platforms><Files.App>\Utils\Cloud\Detector\GoogleDriveCloudDetector.cs (3 issues) <platforms><Files.App>\Services\SideloadUpdateService.cs (2 issues) <platforms><Files.App>\Services\UpdateService.cs (2 issues) <platforms><Files.App>\Extensions\ImageSourceExtensions.cs (1 issue)

Gains

  • UI will not freeze while disposing a resource

Requirements

  • Convert all asynchronous using to async using

Comments

image

It's possible to make a single PR using this button :)

gumbarros avatar Apr 01 '24 01:04 gumbarros

Out of curious, what IDE are you using? I also wanna generate this list lol.

0x5bfa avatar Apr 01 '24 12:04 0x5bfa

Out of curious, what IDE are you using? I also wanna generate this list lol.

Rider, but you can install ReSharper at VS to obtain the same result. You can get a free license if you are a FOSS contribuitor.

gumbarros avatar Apr 01 '24 13:04 gumbarros

Oh they are kind. Why do you think those warnings won't be shown in VS2022 by default and what should we do to should those issues that would improve performance.

0x5bfa avatar Apr 01 '24 13:04 0x5bfa

@yaira2 Can I use the name of Files Community for: image

0x5bfa avatar Apr 01 '24 13:04 0x5bfa

Oh they are kind. Why do you think those warnings won't be shown in VS2022 by default and what should we do to should those issues that would improve performance.

I guess this is why JetBrains makes money lol, if VS showed these problems ReSharper wouldn't exist. Maybe because of some limitation?

Yair said about benchmarking, I think this is valid. There is also dozens of issues types when I run the analyzer, but I opened just the most importants, some of them I think they don't make sense, like var instead of the explicit type, not worth a PR.

image

gumbarros avatar Apr 01 '24 13:04 gumbarros

I'm not sure if Files counts as a project, since users have to pay for the version in the Microsoft Store

From here https://www.jetbrains.com/community/opensource/#support

image

marcofranzen99 avatar Apr 01 '24 13:04 marcofranzen99

That's absolutely right lol I was wondering why VS doesn't show warnings that comes from the official analyzers(IDE????, C????) while Rider shows. But I seemed to have misunderstood. Thanks.

Really nice features.

0x5bfa avatar Apr 01 '24 13:04 0x5bfa

I'm not sure if Files counts as a project, since users have to pay for the version in the Microsoft Store

From here https://www.jetbrains.com/community/opensource/#support

image

image I saw some projects with "Thanks for JetBrains" at the end of the README file, maybe if you contact JetBrains sales they can make a exception for Files as this is a big project .

gumbarros avatar Apr 01 '24 13:04 gumbarros

But that's for donation.

0x5bfa avatar Apr 01 '24 14:04 0x5bfa

I'm not sure if Files counts as a project, since users have to pay for the version in the Microsoft Store

I wouldn't consider the store version as commercial, the sideload version has all the same features and users get the same level of support. In fact, the sideload version has many times the number of users as the store version.

yaira2 avatar Apr 01 '24 14:04 yaira2

Sorry for the unrelated comments, Yair. I don't mind you deleting them.

Besides that, I'm onboard where applicable. Basically classes that implement IAsyncDisposable need to release unmanaged objects asynchronously but using Disposable may cause hangs.

0x5bfa avatar Nov 14 '24 02:11 0x5bfa