mirror of
https://github.com/BeyondDimension/SteamTools.git
synced 2026-10-25 19:49:43 +08:00
add lock
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// https://github.com/aspnet/AspNetIdentity/blob/master/src/Microsoft.AspNet.Identity.Core/AsyncHelper.cs
|
||||
// https://github.com/aspnet/AspNetIdentity/blob/master/src/Microsoft.AspNet.Identity.Core/AsyncHelper.cs
|
||||
using System.Globalization;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
@@ -8,7 +8,7 @@ namespace System
|
||||
{
|
||||
public static class TaskExtensions
|
||||
{
|
||||
static readonly TaskFactory _myTaskFactory = new TaskFactory(CancellationToken.None,
|
||||
static readonly TaskFactory _myTaskFactory = new(CancellationToken.None,
|
||||
TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Default);
|
||||
|
||||
public static TResult RunSync<TResult>(this Func<Task<TResult>> func)
|
||||
|
||||
@@ -78,6 +78,7 @@ namespace Microsoft.AppCenter.Utils
|
||||
{
|
||||
internal sealed class ApplicationSettings : IApplicationSettings
|
||||
{
|
||||
static readonly object configLock = new();
|
||||
readonly IStorage storage;
|
||||
|
||||
public ApplicationSettings(IStorage storage)
|
||||
@@ -87,34 +88,46 @@ namespace Microsoft.AppCenter.Utils
|
||||
|
||||
public bool ContainsKey(string key)
|
||||
{
|
||||
Func<Task<bool>> func = () => storage.ContainsKeyAsync(key);
|
||||
var r = func.RunSync();
|
||||
return r;
|
||||
lock (configLock)
|
||||
{
|
||||
Func<Task<bool>> func = () => storage.ContainsKeyAsync(key);
|
||||
var r = func.RunSync();
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
public T? GetValue<T>(string key, T? defaultValue = default) where T : notnull
|
||||
{
|
||||
Func<Task<string?>> func = () => storage.GetAsync(key);
|
||||
var r = func.RunSync();
|
||||
if (r != null)
|
||||
lock (configLock)
|
||||
{
|
||||
var r2 = (T)TypeDescriptor.GetConverter(typeof(T)).ConvertFromInvariantString(r);
|
||||
return r2;
|
||||
Func<Task<string?>> func = () => storage.GetAsync(key);
|
||||
var r = func.RunSync();
|
||||
if (r != null)
|
||||
{
|
||||
var r2 = (T)TypeDescriptor.GetConverter(typeof(T)).ConvertFromInvariantString(r);
|
||||
return r2;
|
||||
}
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public void Remove(string key)
|
||||
{
|
||||
Func<Task> func = () => storage.RemoveAsync(key);
|
||||
func.RunSync();
|
||||
lock (configLock)
|
||||
{
|
||||
Func<Task> func = () => storage.RemoveAsync(key);
|
||||
func.RunSync();
|
||||
}
|
||||
}
|
||||
|
||||
public void SetValue(string key, object value)
|
||||
{
|
||||
var invariant = value != null ? TypeDescriptor.GetConverter(value.GetType()).ConvertToInvariantString(value) : null;
|
||||
Func<Task> func = () => storage.SetAsync(key, invariant);
|
||||
func.RunSync();
|
||||
lock (configLock)
|
||||
{
|
||||
Func<Task> func = () => storage.SetAsync(key, invariant);
|
||||
func.RunSync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -44,12 +44,12 @@ namespace System.Application.UI.ViewModels
|
||||
|
||||
public void DebugButton_Click()
|
||||
{
|
||||
DebugButton_Click1();
|
||||
//Parallel.For(0, 10, (_, _) =>
|
||||
//{
|
||||
// DebugButton_Click1();
|
||||
// //Task.Run(DebugButton_Click1);
|
||||
//});
|
||||
//DebugButton_Click1();
|
||||
Parallel.For(0, 10, (_, _) =>
|
||||
{
|
||||
DebugButton_Click1();
|
||||
//Task.Run(DebugButton_Click1);
|
||||
});
|
||||
}
|
||||
|
||||
public async void DebugButton_Click1()
|
||||
|
||||
Reference in New Issue
Block a user