void HandleEvent(GameObject player, Thread thread) { Thread current = player.GetComponent <Player>().currentThread; // Запрет срабатывания события во время выполнения его обработчика if (thread.Name == current.Name) { return; } // Сравнить приоритеты if (thread.Priority <= current.Priority) // проверить после текущего { current.OnFinish += () => HandleEvent(player, thread); } else // вытеснить текущий { current.Pause(true); player.GetComponent <Player>().currentThread = thread; thread.OnFinish += () => { player.GetComponent <Player>().currentThread = current; current.Resume(); }; thread.Run(); } }
public void LogObserverThreadSafeTest() { ConfirmNotListening(); Log.RegisterListener(listenTest, Log.MessageType.ALL); int loopCount = 3; Thread[] threads = new Thread[5]; string message = "123."; for(int i = 0; i < threads.Length; i++) { threads[i] = new Thread(() => { int p = 0; p++; for(int j = 0; j < loopCount; j++) { Log.info(message); } }); } threads.Run(); string repeatedMessage = ""; for(int i = 0; i < loopCount * threads.Length; i++) { repeatedMessage += message; } Assert.AreEqual(repeatedMessage, listenTestMessages); listenTestMessages = ""; Log.DeregisterListener(listenTest); ConfirmNotListening(); }
public void Repeat(string name, Action action, Action <Exception> onException, int interval = 100, ThreadPriority priority = ThreadPriority.Normal, CancellationToken?cancellationToken = null) { var thread = new Thread( () => { try { while (true) { if (cancellationToken.HasValue && cancellationToken.Value.IsCancellationRequested) { break; } Sleep(interval); action(); } } catch (Exception e) { onException(e); } } ); thread.Run(priority); Register(thread, cancellationToken, name); }
public void ThreadWaitTillAllCompleteTest() { counter = 0; Thread[] threads = new Thread[10]; for(int i = 0; i < threads.Length; i++) { threads[i] = new Thread(() => { Thread.SleepInSeconds(Random.Uint.LessThan(10)); counter++; }); } threads.Run(); Assert.IsTrue(counter == 10); }
public void ThreadWaitTillAllCompleteTest() { counter = 0; Thread[] threads = new Thread[100]; for (int i = 0; i < threads.Length; i++) { threads[i] = new Thread(() => { Thread.SleepInSeconds(Random.Uint.LessThan(3)); Interlocked.Increment(ref counter); }); } threads.Run(); Assert.IsTrue(counter == threads.Length); }
public void ThrottleIdTest() { DateTime start = DateTime.Now; Thread[] threads = new Thread[10]; for(int i = 0; i < 10; i++) { threads[i] = new Thread(() => { throttleTest(counter++); }); } threads.Run(); TimeSpan span = DateTime.Now - start; Assert.IsTrue(span > new TimeSpan(0, 0, 9)); Assert.IsTrue(span < new TimeSpan(0, 0, 90)); }
public void ThrottleIdTest() { DateTime start = DateTime.Now; Thread[] threads = new Thread[10]; for (int i = 0; i < 10; i++) { threads[i] = new Thread(() => { throttleTest(counter++); }); } threads.Run(); TimeSpan span = DateTime.Now - start; Assert.IsTrue(span > TimeSpan.FromSeconds(9)); Assert.IsTrue(span < TimeSpan.FromSeconds(90)); }
protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.Main); var list = FindViewById <ListView>(Resource.Id.TestsList); var selectButton = FindViewById <Button>(Resource.Id.SelectAllButton); var deselectButton = FindViewById <Button>(Resource.Id.DeselectAllButton); var runTestsButton = FindViewById <Button>(Resource.Id.RunTests); var tests = GetAllTests(); MarkAllAs(tests, true); list.Adapter = new TestListAdapter(this, tests); selectButton.Click += (sender, args) => { MarkAllAs(tests, true); list.Adapter = new TestListAdapter(this, tests); }; deselectButton.Click += (sender, args) => { MarkAllAs(tests, false); list.Adapter = new TestListAdapter(this, tests); }; runTestsButton.Click += (sender, args) => { SetContentView(Resource.Layout.Testing); var selectedTests = tests.Where(item => item.Selected).ToList(); var thread = new Thread(() => RunTests(selectedTests)); thread.Run(); }; }
protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.Main); var list = FindViewById<ListView>(Resource.Id.TestsList); var selectButton = FindViewById<Button>(Resource.Id.SelectAllButton); var deselectButton = FindViewById<Button>(Resource.Id.DeselectAllButton); var runTestsButton = FindViewById<Button>(Resource.Id.RunTests); var tests = GetAllTests(); MarkAllAs(tests, true); list.Adapter = new TestListAdapter(this, tests); selectButton.Click += (sender, args) => { MarkAllAs(tests, true); list.Adapter = new TestListAdapter(this, tests); }; deselectButton.Click += (sender, args) => { MarkAllAs(tests, false); list.Adapter = new TestListAdapter(this, tests); }; runTestsButton.Click += (sender, args) => { SetContentView(Resource.Layout.Testing); var selectedTests = tests.Where(item => item.Selected).ToList(); var thread = new Thread(() => RunTests(selectedTests)); thread.Run(); }; }
public event EventHandler OnChangeHP; // Вызывается при любом изменении кол-ва HP void Start() { canvas = SceneManager.GetSceneAt(0).GetRootGameObjects().Where(x => x.name == "Canvas").ToArray()[0].transform; botName = canvas.GetComponent <Setup>().titles[this.name == "Player" ? 0 : 1]; Arena arena = SceneManager.GetSceneAt(1).GetRootGameObjects().Where(x => x.name == "GameLogic").ToArray()[0].GetComponent <Arena>(); // На старте игры присваиваем значения по умолчанию bulletDamage = arena.bulletDamage; playerSpeed = arena.playerSpeed; rotationSpeed = arena.rotationSpeed; bulletSpeed = arena.bulletSpeed; reloadTime = arena.reloadTime; HP = arena.HP; if (!currentThread.IsRunning) { currentThread.Run(); // процесс OnChangeHP(Clone) запускается 2 раза. Возможное решение: при запуске проверять isRunning } // Вариант реализации с событием OnStart (currentThread.Run() не нужен, сразу запускается процесс OnStart(Clone), который, по завершению, запустит процесс default) //OnStart?.Invoke(); InvokeRepeating("Timer", 0, 1); }
public void Run(string name, Action action, Action <Exception> onException, ThreadPriority priority = ThreadPriority.Normal, CancellationToken?cancellationToken = null) { var thread = new Thread( () => { try { if (cancellationToken.HasValue && cancellationToken.Value.IsCancellationRequested) { return; } action(); } catch (Exception e) { onException(e); } } ); thread.Run(priority); Register(thread, cancellationToken, name); }