-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebView2EnvironmentManager.cs
More file actions
63 lines (54 loc) · 2.05 KB
/
WebView2EnvironmentManager.cs
File metadata and controls
63 lines (54 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using Microsoft.Web.WebView2.Core;
namespace PiPCrunchy;
/// <summary>
/// Singleton manager for shared WebView2 environment
/// Ensures all windows use the same environment instance
/// </summary>
public static class WebView2EnvironmentManager
{
private static CoreWebView2Environment? _sharedEnvironment;
private static readonly object _lock = new object();
/// <summary>
/// Gets or creates the shared WebView2 environment
/// </summary>
public static async Task<CoreWebView2Environment> GetEnvironmentAsync()
{
if (_sharedEnvironment != null)
{
return _sharedEnvironment;
}
lock (_lock)
{
// Double-check after acquiring lock
if (_sharedEnvironment != null)
{
return _sharedEnvironment;
}
}
// Create environment outside lock to avoid blocking
var userDataFolder = System.IO.Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"PiPStream",
"WebView2Data"
);
// Performance optimization: Configure environment with additional options
var environmentOptions = new CoreWebView2EnvironmentOptions
{
AdditionalBrowserArguments =
"--disable-background-timer-throttling " + // Prevent throttling of background tabs
"--disable-renderer-backgrounding " + // Keep renderer active
"--disable-backgrounding-occluded-windows " + // Don't throttle hidden windows
"--disable-features=msSmartScreenProtection " + // Reduce overhead
"--disable-extensions " + // No extensions needed
"--disable-background-networking" // Reduce network overhead
};
var environment = await CoreWebView2Environment.CreateAsync(
null, userDataFolder, environmentOptions
);
lock (_lock)
{
_sharedEnvironment = environment;
}
return environment;
}
}