maui-linux/Xamarin.Forms.Core/Performance.cs

79 строки
2.0 KiB
C#
Исходник Обычный вид История

using System.ComponentModel;
2016-03-22 23:02:25 +03:00
using System.Runtime.CompilerServices;
using System;
using System.Threading;
2016-03-22 23:02:25 +03:00
namespace Xamarin.Forms.Internals
2016-03-22 23:02:25 +03:00
{
[EditorBrowsable(EditorBrowsableState.Never)]
public interface IPerformanceProvider
2016-03-22 23:02:25 +03:00
{
void Stop(string reference, string tag, string path, string member);
2016-03-22 23:02:25 +03:00
void Start(string reference, string tag, string path, string member);
}
2016-03-22 23:02:25 +03:00
[EditorBrowsable(EditorBrowsableState.Never)]
public class Performance
{
static long Reference;
public static IPerformanceProvider Provider { get; private set; }
2016-03-22 23:02:25 +03:00
public static void SetProvider(IPerformanceProvider instance)
2016-03-22 23:02:25 +03:00
{
Provider = instance;
2016-03-22 23:02:25 +03:00
}
public static void Start(out string reference, string tag = null, [CallerFilePath] string path = null, [CallerMemberName] string member = null)
{
if (Provider == null)
{
reference = String.Empty;
return;
}
reference = Interlocked.Increment(ref Reference).ToString();
Provider.Start(reference, tag, path, member);
}
public static void Start(string reference, string tag = null, [CallerFilePath] string path = null,
[CallerMemberName] string member = null)
2016-03-22 23:02:25 +03:00
{
Provider?.Start(reference, tag, path, member);
2016-03-22 23:02:25 +03:00
}
public static void Stop(string reference, string tag = null, [CallerFilePath] string path = null, [CallerMemberName] string member = null)
2016-03-22 23:02:25 +03:00
{
Provider?.Stop(reference, tag, path, member);
2016-03-22 23:02:25 +03:00
}
internal static IDisposable StartNew(string tag = null, [CallerFilePath] string path = null, [CallerMemberName] string member = null)
{
return new DisposablePerformanceReference(tag, path, member);
}
class DisposablePerformanceReference : IDisposable
{
string _reference;
string _tag;
string _path;
string _member;
public DisposablePerformanceReference(string tag, string path, string member)
{
_tag = tag;
_path = path;
_member = member;
Start(out string reference, _tag, _path, _member);
_reference = reference;
}
public void Dispose()
{
Stop(_reference, _tag, _path, _member);
}
}
2016-03-22 23:02:25 +03:00
}
}