mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-07-18 16:21:50 -05:00
TemporarySynchronizationContext
This commit is contained in:
parent
96c01397ce
commit
fb78dfbd9d
|
|
@ -0,0 +1,61 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace UniGLTF
|
||||
{
|
||||
/// <summary>
|
||||
/// await で post される先を1次的に乗っ取る
|
||||
/// </summary>
|
||||
public class TemporarySynchronizationContext : SynchronizationContext
|
||||
{
|
||||
Queue<Action> m_callbacks = new Queue<Action>();
|
||||
|
||||
public override void Post(SendOrPostCallback d, object state)
|
||||
{
|
||||
m_callbacks.Enqueue(() => d(state));
|
||||
}
|
||||
|
||||
public void ExecuteSync()
|
||||
{
|
||||
while (m_callbacks.Count > 0)
|
||||
{
|
||||
var callback = m_callbacks.Dequeue();
|
||||
callback();
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerator AsCoroutine()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task AsTask()
|
||||
{
|
||||
// repost to SynchronizationContext.Current
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
class Hijacker : IDisposable
|
||||
{
|
||||
SynchronizationContext m_backup;
|
||||
public Hijacker(SynchronizationContext context)
|
||||
{
|
||||
m_backup = SynchronizationContext.Current;
|
||||
SynchronizationContext.SetSynchronizationContext(context);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
SynchronizationContext.SetSynchronizationContext(m_backup);
|
||||
}
|
||||
}
|
||||
|
||||
public IDisposable Hijack()
|
||||
{
|
||||
return new Hijacker(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 62f8f380aadfc284f9bdc2d58f84bf48
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Loading…
Reference in New Issue
Block a user