44 changed files with 579 additions and 205 deletions
@ -0,0 +1,58 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Text; |
||||
|
using System.Threading; |
||||
|
using System.Threading.Tasks; |
||||
|
using Perspex.Platform; |
||||
|
|
||||
|
namespace Perspex.Threading |
||||
|
{ |
||||
|
public class SingleThreadDispatcher : Dispatcher |
||||
|
{ |
||||
|
class ThreadingInterface : IPlatformThreadingInterface |
||||
|
{ |
||||
|
private AutoResetEvent _evnt = new AutoResetEvent(false); |
||||
|
private JobRunner _timerJobRunner; |
||||
|
|
||||
|
public ThreadingInterface() |
||||
|
{ |
||||
|
_timerJobRunner = new JobRunner(this); |
||||
|
} |
||||
|
|
||||
|
public void RunLoop(CancellationToken cancellationToken) |
||||
|
{ |
||||
|
while (!cancellationToken.IsCancellationRequested) |
||||
|
{ |
||||
|
_evnt.WaitOne(); |
||||
|
if (cancellationToken.IsCancellationRequested) |
||||
|
return; |
||||
|
Signaled?.Invoke(); |
||||
|
_timerJobRunner.RunJobs(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public IDisposable StartTimer(TimeSpan interval, Action tick) |
||||
|
=> PerspexLocator.Current.GetService<IPclPlatformWrapper>().StartSystemTimer(interval, |
||||
|
() => _timerJobRunner.Post(tick, DispatcherPriority.Normal)); |
||||
|
|
||||
|
public void Signal() => _evnt.Set(); |
||||
|
|
||||
|
public event Action Signaled; |
||||
|
} |
||||
|
|
||||
|
public SingleThreadDispatcher() : base(new ThreadingInterface()) |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
public static Dispatcher StartNew(CancellationToken token) |
||||
|
{ |
||||
|
var dispatcher = new SingleThreadDispatcher(); |
||||
|
PerspexLocator.Current.GetService<IPclPlatformWrapper>().PostThreadPoolItem(() => |
||||
|
{ |
||||
|
dispatcher.MainLoop(token); |
||||
|
}); |
||||
|
return dispatcher; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,35 @@ |
|||||
|
// Copyright (c) The Perspex Project. All rights reserved.
|
||||
|
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
||||
|
|
||||
|
using System; |
||||
|
|
||||
|
namespace Perspex.Controls.Templates |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Selects a member of an object using a <see cref="Func{TObject, TMember}"/>.
|
||||
|
/// </summary>
|
||||
|
public class FuncMemberSelector<TObject, TMember> : IMemberSelector |
||||
|
{ |
||||
|
private Func<TObject, TMember> _selector; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="FuncMemberSelector{TObject, TMember}"/>
|
||||
|
/// class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="selector">The selector.</param>
|
||||
|
public FuncMemberSelector(Func<TObject, TMember> selector) |
||||
|
{ |
||||
|
this._selector = selector; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Selects a member of an object.
|
||||
|
/// </summary>
|
||||
|
/// <param name="o">The obeject.</param>
|
||||
|
/// <returns>The selected member.</returns>
|
||||
|
public object Select(object o) |
||||
|
{ |
||||
|
return (o is TObject) ? _selector((TObject)o) : default(TMember); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,18 @@ |
|||||
|
// Copyright (c) The Perspex Project. All rights reserved.
|
||||
|
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
||||
|
|
||||
|
namespace Perspex.Controls.Templates |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Selects a member of an object.
|
||||
|
/// </summary>
|
||||
|
public interface IMemberSelector |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Selects a member of an object.
|
||||
|
/// </summary>
|
||||
|
/// <param name="o">The obeject.</param>
|
||||
|
/// <returns>The selected member.</returns>
|
||||
|
object Select(object o); |
||||
|
} |
||||
|
} |
||||
Binary file not shown.
Loading…
Reference in new issue