How to use Dispose method of PuppeteerSharp.Helpers.TempDirectory class

Best Puppeteer-sharp code snippet using PuppeteerSharp.Helpers.TempDirectory.Dispose

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

TempDirectory.cs

Source: TempDirectory.cs Github

copy
1using System;
2using System.IO;
3using System.Threading;
4using System.Threading.Tasks;
5using PathHelper = System.IO.Path;
6
7namespace PuppeteerSharp.Helpers
8{
9    /// <summary>
10    /// Represents a directory that is deleted on disposal.
11    /// </summary>
12    internal class TempDirectory : IDisposable
13    {
14        private Task _deleteTask;
15
16        public TempDirectory()
17            : this(PathHelper.Combine(PathHelper.GetTempPath(), PathHelper.GetRandomFileName()))
18        { }
19
20        public TempDirectory(string path)
21        {
22            if (string.IsNullOrEmpty(path))
23            {
24                throw new ArgumentException("Path must be specified", nameof(path));
25            }
26
27            Directory.CreateDirectory(path);
28            this.Path = path;
29        }
30
31        ~TempDirectory()
32        {
33            Dispose(false);
34        }
35
36        public string Path { get; }
37
38        public void Dispose()
39        {
40            GC.SuppressFinalize(this);
41            Dispose(true);
42        }
43
44        protected void Dispose(bool disposing)
45        {
46            if (_deleteTask == null)
47            {
48                _ = DeleteAsync();
49            }
50        }
51
52        public override string ToString() => Path;
53
54        public Task DeleteAsync(CancellationToken cancellationToken = default)
55            => _deleteTask ?? (_deleteTask = DeleteAsync(Path, CancellationToken.None));
56
57        private static async Task DeleteAsync(string path, CancellationToken cancellationToken = default)
58        {
59            const int minDelayInMsec = 200;
60            const int maxDelayInMsec = 8000;
61
62            var retryDelay = minDelayInMsec;
63            while (true)
64            {
65                if (!Directory.Exists(path))
66                {
67                    return;
68                }
69
70                cancellationToken.ThrowIfCancellationRequested();
71                try
72                {
73                    Directory.Delete(path, true);
74                    return;
75                }
76                catch
77                {
78                    await Task.Delay(retryDelay, cancellationToken).ConfigureAwait(false);
79                    if (retryDelay < maxDelayInMsec)
80                    {
81                        retryDelay = Math.Min(2 * retryDelay, maxDelayInMsec);
82                    }
83                }
84            }
85        }
86    }
87}
88
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used method in TempDirectory

Trigger Dispose code on LambdaTest Cloud Grid

Execute automation tests with Dispose on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)