Esempio n. 1
0
        private void updatePatrols()
        {
            if (!this.Visible)
            {
                return;
            }

            Trace.WriteLine("FPatrols.updatePatrols() started on thread " +
                            Thread.CurrentThread.Name + "\" ( " +
                            Thread.CurrentThread.ManagedThreadId.ToString() + " )");

            if (this.updatePatrolThreadsRunning > 1)
            {
                return;
            }

            Interlocked.Increment(ref updatePatrolThreadsRunning);

            Trace.WriteLine("FPatrols.updatePatrols() locking \"" +
                            "UpdatingPatrols\" on thread \"" +
                            Thread.CurrentThread.Name + "\" ( " +
                            Thread.CurrentThread.ManagedThreadId.ToString() + " )");

            bool result = Monitor.TryEnter(UpdatingPatrols);

            if (result == false)
            {
                return;
            }

            lock (UpdatingPatrols)
            {
                Trace.WriteLine("FPatrols.updatePatrols() locked \"" +
                                "UpdatingPatrols\" on thread \"" +
                                Thread.CurrentThread.Name + "\" ( " +
                                Thread.CurrentThread.ManagedThreadId.ToString() + " )");
                try
                {
                    this.patrolsDs = new Allberg.Shooter.Windows.DatasetPatrols();

                    this.patrolsDs.Clear();

                    foreach (Structs.Patrol patrol in CommonCode.GetPatrols())
                    {
                        DatasetPatrols.PatrolsRow row =
                            patrolsDs.Patrols.NewPatrolsRow();
                        row.Class = patrol.PClass.ToString();
                        if (patrol.LockedForAutomatic)
                        {
                            row.Class += "-S";
                        }
                        row.Id = patrol.PatrolId;
                        row.NumberOfCompetitors =
                            CommonCode.GetCompetitorsCountPatrol(patrol);
                        row.NumberOfCompetitorsWithResult =
                            CommonCode.GetCompetitorsWithResultCountPatrol(patrol);
                        row.Starttime        = patrol.StartDateTime;
                        row.StartTimeDisplay = patrol.StartDateTimeDisplay;
                        row.NrOfArrived      = CommonCode.GetCompetitorsArrivedCountPatrol(patrol);
                        patrolsDs.Patrols.AddPatrolsRow(row);
                    }
                    if (this.InvokeRequired)
                    {
                        this.Invoke(UpdateNrOfPatrols, new object[] { this.patrolsDs.Patrols.Count });
                        this.Invoke(DatasetBindToDataGrid);
                    }
                    else
                    {
                        UpdateNrOfPatrols(this.patrolsDs.Patrols.Count);
                        datasetBindToDataGrid();
                    }
                }
                catch (ApplicationException)
                {
                    // Occurs where there is no patrol. Do nothing
                }
                catch (Exception exc)
                {
                    Console.WriteLine(exc.ToString());
                }
                finally
                {
                    Interlocked.Decrement(ref updatePatrolThreadsRunning);
                }
                Trace.WriteLine("FPatrols.updatePatrols() unlocking \"" +
                                "UpdatingPatrols\" on thread \"" +
                                Thread.CurrentThread.Name + "\" ( " +
                                Thread.CurrentThread.ManagedThreadId.ToString() + " )");
                Trace.WriteLine("FPatrols.updatePatrols() ended.");
            }
            Monitor.Exit(UpdatingPatrols);
        }