Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix loop timings #62

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 28 additions & 14 deletions Listen/OSUListenerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,9 @@ static OsuListenerManager()
s_stop_flag = false;

//Listen Thread
s_listen_task = Task.Run(() =>
s_listen_task = Task.Factory.StartNew(() =>
{
var spinWait = new SpinWait();
Thread.CurrentThread.Name = "OsuRTDataProviderThread";
Thread.Sleep(2000);
while (!s_stop_flag)
Expand All @@ -222,9 +223,16 @@ static OsuListenerManager()
action.Item2();
}

Thread.Sleep(Setting.ListenInterval);
if (Setting.ListenInterval == 0)
{
spinWait.SpinOnce();
}
else
{
Thread.Sleep(Setting.ListenInterval);
}
}
});
}, TaskCreationOptions.LongRunning);
}
#endregion

Expand Down Expand Up @@ -395,43 +403,49 @@ public ProvideData GetCurrentData(ProvideDataMask mask)
#region Init Finder
private const long RETRY_INTERVAL = 3000;

private Dictionary<Type, long> finder_timer_dict = new Dictionary<Type, long>();
private Dictionary<Type, Stopwatch> finder_timer_dict = new Dictionary<Type, Stopwatch>();
private T InitFinder<T>(string success_fmt, string failed_fmt) where T : OsuFinderBase
{
bool firstInit = false;
if (!finder_timer_dict.ContainsKey(typeof(T)))
finder_timer_dict.Add(typeof(T), 0);
{
finder_timer_dict.Add(typeof(T), new Stopwatch());
firstInit = true;
}

T finder = null;
long timer = finder_timer_dict[typeof(T)];
Stopwatch timer = finder_timer_dict[typeof(T)];

if (timer % RETRY_INTERVAL == 0)
if (firstInit || timer.ElapsedMilliseconds >= RETRY_INTERVAL)
{
finder = typeof(T).GetConstructors()[0].Invoke(new object[] { m_osu_process }) as T;
if (finder.TryInit())
{
timer = 0;
timer.Reset();
Logger.Info(string.Format(success_fmt, m_osu_id));
return finder;
}

finder = null;
Logger.Error(string.Format(failed_fmt, m_osu_id, RETRY_INTERVAL / 1000));
}
timer += Setting.ListenInterval;
timer.Start();
finder_timer_dict[typeof(T)] = timer;
return finder;
}
#endregion

#region Find Osu Setting
private long find_osu_process_timer = 0;
private bool firstInit = true;
private Stopwatch find_osu_process_timer = new Stopwatch();
private const long FIND_OSU_RETRY_INTERVAL = 5000;

private void FindOsuProcess()
{
if (find_osu_process_timer > FIND_OSU_RETRY_INTERVAL)
if (firstInit || find_osu_process_timer.ElapsedMilliseconds > FIND_OSU_RETRY_INTERVAL)
{
find_osu_process_timer = 0;
firstInit = false;
find_osu_process_timer.Reset();
Process[] process_list;

process_list = Process.GetProcessesByName("osu!");
Expand Down Expand Up @@ -462,11 +476,11 @@ private void FindOsuProcess()
return;
}
}
find_osu_process_timer = 0;
find_osu_process_timer.Reset();
if (!Setting.DisableProcessNotFoundInformation)
Logger.Error(string.Format(LANG_OSU_NOT_FOUND, m_osu_id));
}
find_osu_process_timer += Setting.ListenInterval;
find_osu_process_timer.Start();
}

private void FindSongPathAndUsername()
Expand Down