Exemplo n.º 1
0
    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();
        }
    }
Exemplo n.º 2
0
		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();
		}
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
0
        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));
        }
Exemplo n.º 7
0
        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));
        }
Exemplo n.º 8
0
        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();
            };
        }
Exemplo n.º 9
0
		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();
			};
		}
Exemplo n.º 10
0
    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);
    }
Exemplo n.º 11
0
        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);
        }
Exemplo n.º 12
0
        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();
        }