Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Commit

Permalink
MainThreadSchedule's schedule(dueTime) acquires time accuracy.
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Jul 1, 2014
1 parent 190bada commit 2936d26
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
10 changes: 10 additions & 0 deletions Assets/ObjectTest/NewBehaviourScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ public override void Awake()
//_____cancel = Observable.EveryUpdate()
// .Subscribe(_ => Debug.Log(DateTime.Now.ToString()));

var a = DateTime.Now.Ticks;
Debug.Log(a);
Scheduler.MainThread.Schedule(TimeSpan.FromMilliseconds(3200), () =>
{
var b = DateTime.Now.Ticks;
Debug.Log(b);

Debug.Log(new TimeSpan(b - a));
});

base.Awake();
}

Expand Down
30 changes: 26 additions & 4 deletions Assets/UniRx/Scripts/UnityEngineBridge/MainThreadScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,29 @@ public MainThreadScheduler()

IEnumerator DelayAction(TimeSpan dueTime, Action action)
{
// lack of accuracy, should be change impl?
yield return new WaitForSeconds((float)dueTime.TotalSeconds);
MainThreadDispatcher.Post(action);
if (dueTime == TimeSpan.Zero)
{
MainThreadDispatcher.Post(action);
}
else if (dueTime.TotalMilliseconds % 1000 == 0)
{
yield return new WaitForSeconds((float)dueTime.TotalSeconds);
MainThreadDispatcher.Post(action);
}
else
{
var startTime = DateTime.Now;
while (true)
{
yield return null;
var now = DateTime.Now;
if ((now - startTime) >= dueTime)
{
MainThreadDispatcher.Post(action);
break;
}
}
}
}

public DateTimeOffset Now
Expand Down Expand Up @@ -60,7 +80,9 @@ public IDisposable Schedule(DateTimeOffset dueTime, Action action)
public IDisposable Schedule(TimeSpan dueTime, Action action)
{
var d = new BooleanDisposable();
MainThreadDispatcher.StartCoroutine(DelayAction(dueTime, () =>
var time = Normalize(dueTime);

MainThreadDispatcher.StartCoroutine(DelayAction(time, () =>
{
if (!d.IsDisposed)
{
Expand Down
4 changes: 2 additions & 2 deletions UnityVS.UniRx.CSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
<OutputPath>Temp\UnityVS_bin\Debug\</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DefineConstants>DEBUG;TRACE;UNITY_4_5_1;UNITY_4_5;UNITY_STANDALONE_WIN;ENABLE_MICROPHONE;ENABLE_TEXTUREID_MAP;ENABLE_UNITYEVENTS;ENABLE_NEW_HIERARCHY ;ENABLE_AUDIO_FMOD;UNITY_STANDALONE;ENABLE_MONO;ENABLE_TERRAIN;ENABLE_SUBSTANCE;ENABLE_GENERICS;INCLUDE_WP8SUPPORT;ENABLE_MOVIES;ENABLE_WWW;ENABLE_IMAGEEFFECTS;ENABLE_WEBCAM;INCLUDE_METROSUPPORT;RENDER_SOFTWARE_CURSOR;ENABLE_NETWORK;ENABLE_PHYSICS;ENABLE_CACHING;ENABLE_CLOTH;ENABLE_2D_PHYSICS;ENABLE_SHADOWS;ENABLE_AUDIO;ENABLE_NAVMESH_CARVING;ENABLE_DUCK_TYPING;ENABLE_SINGLE_INSTANCE_BUILD_SETTING;ENABLE_PROFILER;UNITY_EDITOR;UNITY_EDITOR_WIN;UNITY_PRO_LICENSE</DefineConstants>
<DefineConstants>DEBUG;TRACE;UNITY_4_5_1;UNITY_4_5;UNITY_STANDALONE_WIN;ENABLE_MICROPHONE;ENABLE_TEXTUREID_MAP;ENABLE_UNITYEVENTS;ENABLE_NEW_HIERARCHY ;ENABLE_AUDIO_FMOD;UNITY_STANDALONE;ENABLE_MONO;ENABLE_TERRAIN;ENABLE_SUBSTANCE;ENABLE_GENERICS;INCLUDE_WP8SUPPORT;ENABLE_MOVIES;ENABLE_WWW;ENABLE_IMAGEEFFECTS;ENABLE_WEBCAM;INCLUDE_METROSUPPORT;RENDER_SOFTWARE_CURSOR;ENABLE_NETWORK;ENABLE_PHYSICS;ENABLE_CACHING;ENABLE_CLOTH;ENABLE_2D_PHYSICS;ENABLE_SHADOWS;ENABLE_AUDIO;ENABLE_NAVMESH_CARVING;ENABLE_DUCK_TYPING;ENABLE_SINGLE_INSTANCE_BUILD_SETTING;ENABLE_PROFILER;UNITY_EDITOR;UNITY_EDITOR_WIN</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>false</Optimize>
<OutputPath>Temp\UnityVS_bin\Release\</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DefineConstants>TRACE;UNITY_4_5_1;UNITY_4_5;UNITY_STANDALONE_WIN;ENABLE_MICROPHONE;ENABLE_TEXTUREID_MAP;ENABLE_UNITYEVENTS;ENABLE_NEW_HIERARCHY ;ENABLE_AUDIO_FMOD;UNITY_STANDALONE;ENABLE_MONO;ENABLE_TERRAIN;ENABLE_SUBSTANCE;ENABLE_GENERICS;INCLUDE_WP8SUPPORT;ENABLE_MOVIES;ENABLE_WWW;ENABLE_IMAGEEFFECTS;ENABLE_WEBCAM;INCLUDE_METROSUPPORT;RENDER_SOFTWARE_CURSOR;ENABLE_NETWORK;ENABLE_PHYSICS;ENABLE_CACHING;ENABLE_CLOTH;ENABLE_2D_PHYSICS;ENABLE_SHADOWS;ENABLE_AUDIO;ENABLE_NAVMESH_CARVING;ENABLE_DUCK_TYPING;ENABLE_SINGLE_INSTANCE_BUILD_SETTING;ENABLE_PROFILER;UNITY_EDITOR;UNITY_EDITOR_WIN;UNITY_PRO_LICENSE</DefineConstants>
<DefineConstants>TRACE;UNITY_4_5_1;UNITY_4_5;UNITY_STANDALONE_WIN;ENABLE_MICROPHONE;ENABLE_TEXTUREID_MAP;ENABLE_UNITYEVENTS;ENABLE_NEW_HIERARCHY ;ENABLE_AUDIO_FMOD;UNITY_STANDALONE;ENABLE_MONO;ENABLE_TERRAIN;ENABLE_SUBSTANCE;ENABLE_GENERICS;INCLUDE_WP8SUPPORT;ENABLE_MOVIES;ENABLE_WWW;ENABLE_IMAGEEFFECTS;ENABLE_WEBCAM;INCLUDE_METROSUPPORT;RENDER_SOFTWARE_CURSOR;ENABLE_NETWORK;ENABLE_PHYSICS;ENABLE_CACHING;ENABLE_CLOTH;ENABLE_2D_PHYSICS;ENABLE_SHADOWS;ENABLE_AUDIO;ENABLE_NAVMESH_CARVING;ENABLE_DUCK_TYPING;ENABLE_SINGLE_INSTANCE_BUILD_SETTING;ENABLE_PROFILER;UNITY_EDITOR;UNITY_EDITOR_WIN</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Reference Include="mscorlib" />
Expand Down

0 comments on commit 2936d26

Please sign in to comment.