Exemple #1
0
            public async Task Restart(CommandContext ctx)
            {
                if (await LockObj.WaitAsync(0).ConfigureAwait(false))
                {
                    DiscordMessage?msg = null;
                    try
                    {
                        msg = await ctx.RespondAsync("Saving state...").ConfigureAwait(false);

                        await StatsStorage.SaveAsync(true).ConfigureAwait(false);

                        msg = await msg.UpdateOrCreateMessageAsync(ctx.Channel, "Restarting...").ConfigureAwait(false);

                        Restart(ctx.Channel.Id, "Restarted due to command request");
                    }
                    catch (Exception e)
                    {
                        await msg.UpdateOrCreateMessageAsync(ctx.Channel, "Restarting failed: " + e.Message).ConfigureAwait(false);
                    }
                    finally
                    {
                        LockObj.Release();
                    }
                }
                else
                {
                    await ctx.RespondAsync("Update is in progress").ConfigureAwait(false);
                }
            }
 // Use this for initialization
 void Start()
 {
     myLock    = Lock.GetComponent("LockObj") as LockObj;
     rb2d      = GetComponent <Rigidbody2D>();
     animator  = GetComponent <Animator>();
     activated = false;
     timer     = -1;
 }
Exemple #3
0
        public static void Run(string key, Action action)
        {
            LockObj lockObj = null;

            lock (_dict)
            {
                if (!_dict.ContainsKey(key))
                {
                    _dict[key] = new LockObj();
                }
                lockObj      = _dict[key];
                lockObj.Time = DateTime.Now;
            }
            lock (lockObj)
            {
                action();
            }
        }
Exemple #4
0
            public async Task Update(CommandContext ctx)
            {
                if (await LockObj.WaitAsync(0).ConfigureAwait(false))
                {
                    DiscordMessage?msg = null;
                    try
                    {
                        Config.Log.Info("Checking for available bot updates...");
                        msg = await msg.UpdateOrCreateMessageAsync(ctx.Channel, "Checking for bot updates...").ConfigureAwait(false);

                        var(updated, stdout) = await UpdateAsync().ConfigureAwait(false);

                        if (!string.IsNullOrEmpty(stdout))
                        {
                            await ctx.SendAutosplitMessageAsync("```" + stdout + "```").ConfigureAwait(false);
                        }
                        if (!updated)
                        {
                            return;
                        }

                        msg = await ctx.RespondAsync("Saving state...").ConfigureAwait(false);

                        await StatsStorage.SaveAsync(true).ConfigureAwait(false);

                        msg = await msg.UpdateOrCreateMessageAsync(ctx.Channel, "Restarting...").ConfigureAwait(false);

                        Restart(ctx.Channel.Id, "Restarted after successful bot update");
                    }
                    catch (Exception e)
                    {
                        await msg.UpdateOrCreateMessageAsync(ctx.Channel, "Updating failed: " + e.Message).ConfigureAwait(false);
                    }
                    finally
                    {
                        LockObj.Release();
                    }
                }
                else
                {
                    await ctx.RespondAsync("Update is already in progress").ConfigureAwait(false);
                }
            }
Exemple #5
0
        public void Main()
        {
            LockObj obj = new LockObj();

            //注意,这里使用的是同一个资源对象obj
            Jack jack = new Jack(obj);
            John john = new John(obj);

            Thread t1 = new Thread(new ThreadStart(jack.Run));
            Thread t2 = new Thread(new ThreadStart(john.Run));

            t1.Start();
            t1.Name = "Jack";

            t2.Start();
            t2.Name = "John";

            Console.ReadLine();
        }
        public void Should_PreventRaceCondition_When_UseLockWithLog()
        {
            LockObj locker      = new LockObj("test locker");
            int     count       = 100;
            Action  plusOne     = () => { using (LockWithLog.Lock(locker)) { count++; } };
            Action  incrementer = () =>
            {
                for (int i = 0; i <= 100; i++)
                {
                    using (LockWithLog.Lock(locker))
                    {
                        count++;
                        Thread.Sleep(1);
                    }
                }
            };
            Action decrementer = () =>
            {
                for (int i = 0; i <= 100; i++)
                {
                    using (LockWithLog.Lock(locker))
                    {
                        count--;
                        Thread.Sleep(1);
                    }
                }
            };

            Thread thread1 = new Thread(new ThreadStart(incrementer));
            Thread thread2 = new Thread(new ThreadStart(decrementer));
            Thread thread3 = new Thread(new ThreadStart(plusOne));

            thread1.Start();
            thread2.Start();
            thread3.Start();

            Assert.IsTrue(thread1.Join(20000), "Thread is blocked.");
            Assert.IsTrue(thread2.Join(20000), "Thread is blocked.");
            Assert.IsTrue(thread3.Join(20000), "Thread is blocked.");
            Assert.AreEqual(101, count);
        }
Exemple #7
0
        /// <inheritdoc />
        public async Task HandleMessage(IProxiedMessageContext <PSOBBGamePacketPayloadClient, PSOBBGamePacketPayloadServer> context, PSOBBGamePacketPayloadServer payload)
        {
            //TODO: Kinda hacky to do it here, but keeping packets ordered this way. Mostly.
            using (await LockObj.LockAsync().ConfigureAwait(false))
            {
                if (Logger.IsDebugEnabled)
                {
                    Logger.Debug($"Recieved unhandled server payload Name: {payload.GetType().Name} with OpCode: {(GameNetworkOperationCode)payload.OperationCode} - {payload.OperationCode:X}");
                }

                LogPayloadBytes(payload);

                await context.ProxyConnection.SendMessage(payload)
                .ConfigureAwait(false);
            }

            if (payload is IUnknownPayloadType u)
            {
                await PacketLogger.WritePacketAsync((GameNetworkOperationCode)payload.OperationCode, u, BinaryPacketWriter.PacketType.Server)
                .ConfigureAwait(false);
            }
        }
Exemple #8
0
        public void StartLockThread(Action callback)
        {
            if (_lockThread != null && _lockThread.IsAlive)
            {
                Form parentForm = GetTopForm(LockObj);
                if (parentForm != null)
                {
                    parentForm.BeginInvoke((Action) delegate
                    {
                        ToolTip t = new ToolTip();
                        //t.IsBalloon = true;
                        t.ToolTipIcon  = ToolTipIcon.Info;
                        t.ToolTipTitle = "提示";
                        t.UseAnimation = true;
                        t.UseFading    = true;

                        Point p = parentForm.PointToClient(new Point(Form.MousePosition.X, Form.MousePosition.Y + 20));
                        t.Show("您的操作太快了,请休息一下。", parentForm, p.X, p.Y, 2000);
                    });
                }

                _runTimes = 0;
                return;
            }

            _lockThread = new Thread(() =>
            {
                if (_tickCount == 0)
                {
                    _tickCount = Environment.TickCount;
                }
                else
                {
                    _span      = Environment.TickCount - _tickCount;
                    _tickCount = Environment.TickCount;

                    if (_span <= _spanInterval)
                    {
                        Interlocked.Increment(ref _runTimes);
                    }
                    else
                    {
                        _runTimes = 0;
                    }
                }

                if (_runTimes >= _maxRunTimes)
                {
                    LockObj.IsAccessible = false;
                    Application.DoEvents();
                    Thread.Sleep(_releaseLockDuration);
                    _runTimes = 0;


                    return;
                }

                if (callback != null)
                {
                    LockObj.IsAccessible = true;
                    LockObj.Invoke(callback);
                }
            });
            _lockThread.Start();
        }
 public override void ReleaseLock()
 {
     LockObj.ExitWriteLock();
 }
 public override void AddLock()
 {
     LockObj.EnterWriteLock();
 }
Exemple #11
0
    public void RefreshSmallButton()
    {
        if (GM == null)
        {
            GM = GameManager.getInstance();
        }
        UILabel    monsterLabel = null;
        GameObject smallButton, LockObj;
        UISprite   monsterButtonSprite;
        UIButton   monsterButton;
        string     spriteName;
        int        itemLevel    = 0;
        int        monsterCount = 0;


        int childCount = Buttons.childCount;

        StageController _currentStageCtrl = GM.currenetStageController;

        for (int i = 0; i < childCount; i++)
        {
            smallButton = Buttons.GetChild(i).gameObject;

            if (smallButton.tag.Equals("Alias"))
            {
                monsterButton     = smallButton.GetComponent <UIButton>();
                LockObj           = smallButton.transform.FindChild("InstallLock").gameObject;
                monsterLabel      = smallButton.transform.FindChild("monsterPrice").GetComponent <UILabel>();
                monsterLabel.text = "";

                itemLevel    = GM.LoadLevelData(smallButton.name);
                monsterCount = GM.getCharacterSize(smallButton.name);

                if ((itemLevel == 0) ||
                    installPos != -1 && _currentStageCtrl.isOccupyPos(installPos - 1) && _currentStageCtrl.isOccupyPos(installPos + 1) && monsterCount == 2)
                {
                    LockObj.SetActive(true);
                    UIEventListener.Get(Buttons.GetChild(i).gameObject).onClick -= new UIEventListener.VoidDelegate(ButtonProcess);
                }

                else
                {
                    spriteName = GM.getMonsterName(smallButton.name, itemLevel - 1, true) + "_head";
                    LockObj.SetActive(false);
                    monsterLabel.text              = GM.getPrice(smallButton.name, "install", smallButton.tag).ToString("##") + " G";
                    monsterButtonSprite            = smallButton.transform.FindChild("Background").GetComponent <UISprite>();
                    monsterButtonSprite.spriteName = spriteName;
                    monsterButton.normalSprite     = spriteName;
                    monsterButtonSprite.MakePixelPerfect();
                    monsterButtonSprite            = smallButton.transform.FindChild("InstallLock").GetComponent <UISprite>();
                    monsterButtonSprite.spriteName = spriteName;
                    monsterButtonSprite.MakePixelPerfect();
                    UIEventListener.Get(Buttons.GetChild(i).gameObject).onClick -= new UIEventListener.VoidDelegate(ButtonProcess);
                    UIEventListener.Get(Buttons.GetChild(i).gameObject).onClick += new UIEventListener.VoidDelegate(ButtonProcess);
                }
            }
            else if (smallButton.tag.Equals("Obstacle"))
            {
                LockObj           = smallButton.transform.FindChild("InstallLock").gameObject;
                monsterLabel      = smallButton.transform.FindChild("monsterPrice").GetComponent <UILabel>();
                monsterLabel.text = "";
                //itemLevel = GM.LoadTrapLevelData(i - 6);
                itemLevel = GM.LoadLevelData(smallButton.name);

                if (itemLevel == 0 || !_currentStageCtrl.isTrapInstall(smallButton.name))
                {
                    LockObj.SetActive(true);
                    UIEventListener.Get(Buttons.GetChild(i).gameObject).onClick -= new UIEventListener.VoidDelegate(ButtonProcess);
                }
                else
                {
                    LockObj.SetActive(false);
                    monsterLabel.text = GM.getPrice(smallButton.name, "install", smallButton.tag).ToString("00") + " G";
                    UIEventListener.Get(Buttons.GetChild(i).gameObject).onClick -= new UIEventListener.VoidDelegate(ButtonProcess);
                    UIEventListener.Get(Buttons.GetChild(i).gameObject).onClick += new UIEventListener.VoidDelegate(ButtonProcess);
                }
            }
            else
            {
                UIEventListener.Get(Buttons.GetChild(i).gameObject).onClick -= new UIEventListener.VoidDelegate(ButtonProcess);
                UIEventListener.Get(Buttons.GetChild(i).gameObject).onClick += new UIEventListener.VoidDelegate(ButtonProcess);
            }
        }
    }
Exemple #12
0
 public override void ReleaseLock()
 {
     LockObj.ExitReadLock();
 }
Exemple #13
0
 public override void AddLock()
 {
     LockObj.EnterReadLock();
 }
Exemple #14
0
 public John(LockObj obj)
 {
     this.obj = obj;
 }
Exemple #15
0
 public Jack(LockObj obj)
 {
     this.obj = obj;
 }