public static ThreadLoop Start(Action <ThreadLoop> action, int interval) { ThreadLoop threadLoop = new ThreadLoop { cancellationTokenSource = new CancellationTokenSource() }; threadLoop.task = Task.Run(action: () => { while (!threadLoop.cancellationTokenSource.IsCancellationRequested) { try { if (threadLoop.cancellationTokenSource.IsCancellationRequested) { break; } action(threadLoop); threadLoop.cancellationTokenSource.Token.WaitHandle.WaitOne(interval); } catch { threadLoop.cancellationTokenSource.Token.WaitHandle.WaitOne(interval); } } }, threadLoop.cancellationTokenSource.Token); return(threadLoop); }
public void Dispose() { if (this.WaitingForChatThread != null) { this.WaitingForChatThread.Stop(); this.WaitingForChatThread = null; } }
public void OnLogout(object sender, EventArgs args) { this.chatReady = false; if (this.WaitingForChatThread != null) { this.WaitingForChatThread.Stop(); this.WaitingForChatThread = null; } }
/// <summary> /// Executed upon successful character login. /// </summary> public unsafe void OnLogin(object sender, EventArgs args) { if (this.chatReady || this.WaitingForChatThread != null) { return; } this.WaitingForChatThread = ThreadLoop.Start( action: (threadLoop) => { if (Plugin.GameClient.GetChatVisible()) { this.chatReady = true; OnChatReady(); threadLoop.Stop(); this.WaitingForChatThread = null; } }, interval: 250); }