Exemplo n.º 1
0
        /// <summary>
        /// در اینجا موارد قابل اجر به لیست منحصر به فرد فراخواننده اضافه می گردند و درصورتیکه این موارد قبلا وجود داشته باشند به عنوان مورد جدید پذیرفته نمی شود
        /// </summary>
        /// <param name="ExectablePrsCalcList"></param>
        /// <returns>مواردی که برای اجر پذیرفته شده اند</returns>
        public static IList <ExecutablePersonCalculation> AddThreads(String CallerIdentity, IList <ExecutablePersonCalculation> ExectablePrsCalcList, FinishedCallback Callback)
        {
            try
            {
                mut.WaitOne();
                IDictionary <decimal, ExecutableThread> ExecThreads = null;
                int ExecThreadsCount = 0;

                executableThreadsWithCaller.TryGetValue(CallerIdentity, out ExecThreads);
                executableThreadsCountWithCaller.TryGetValue(CallerIdentity, out ExecThreadsCount);

                if (ExecThreads == null)
                {
                    ExecThreads = new Dictionary <decimal, ExecutableThread>();
                    executableThreadsWithCaller.Add(CallerIdentity, ExecThreads);
                    executableThreadsCountWithCaller.Add(CallerIdentity, 0);
                }

                IList <ExecutablePersonCalculation> ItemAdded = new List <ExecutablePersonCalculation>();
                for (int i = 0; i <= ExectablePrsCalcList.Count - 1; i++)
                {
                    decimal PrsId = ExectablePrsCalcList[i].PersonId;

                    ///این پرسنل در لیستی با شناسه فراخواننده متفاوت قبلا درج شده است
                    if (ExsitThreadNotInCaller(CallerIdentity, PrsId))
                    {
                        continue;
                    }

                    if (!ExecThreads.ContainsKey(PrsId))
                    {
                        ExecutableThread executableThread = new ExecutableThread(CallerIdentity,
                                                                                 ExectablePrsCalcList[i].ID,
                                                                                 ExectablePrsCalcList[i].PersonId,
                                                                                 ExectablePrsCalcList[i].FromDate.Date,
                                                                                 ExectablePrsCalcList[i].ToDate.Date,
                                                                                 Callback);
                        ExecThreads.Add(PrsId, executableThread);
                        executableThreadsCountWithCaller[CallerIdentity]++;

                        ItemAdded.Add(ExectablePrsCalcList[i]);
                    }
                    //else if (!ExecThreads[PrsId].Executing)
                    //{
                    //    ItemAdded.Add(ExectablePrsCalcList[i]);
                    //}
                }
                return(ItemAdded);
            }
            finally
            {
                mut.ReleaseMutex();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// مورد ارسال شده را برای اجرا تست می کند، اگر قبلا برای اجرا ارسال نشده باشد یا اجرا نشده باشد آن را برای اجرا برمی گرداند
        /// </summary>
        /// <param name="Threads"></param>
        /// <returns>موردی که برای اجر پذیرفته شده است</returns>
        public static ExecutableThread.ThreadParam ForceToExecute(string CallerIdentity, ExecutablePersonCalculation MustExecuted, FinishedCallback Callback)
        {
            try
            {
                mut.WaitOne();

                //if (IsThreadExecute(MustExecuted.PersonId))
                //{
                //    ///نخی که محاسبات را در حالت اجرا قرار داده با فراخواننده ی این تابع متفاوت است
                //    if (executableThreadsWithCaller.ContainsKey(CallerIdentity) &&
                //        executableThreadsWithCaller[CallerIdentity].ContainsKey(MustExecuted.PersonId) &&
                //        !executableThreadsWithCaller[CallerIdentity][MustExecuted.PersonId].Executing)
                //    {
                //        executableThreadsWithCaller[CallerIdentity].Remove(MustExecuted.PersonId);
                //    }
                //    return null;
                //}
                //else
                //{
                if (!executableThreadsWithCaller.ContainsKey(CallerIdentity))
                {
                    executableThreadsWithCaller.Add(CallerIdentity, new Dictionary <decimal, ExecutableThread>());
                    executableThreadsCountWithCaller.Add(CallerIdentity, 0);
                }

                if (!executableThreadsWithCaller[CallerIdentity].ContainsKey(MustExecuted.PersonId))
                {
                    ExecutableThread executableThread = new ExecutableThread(CallerIdentity,
                                                                             MustExecuted.ID,
                                                                             MustExecuted.PersonId,
                                                                             MustExecuted.FromDate.Date,
                                                                             MustExecuted.ToDate.Date,
                                                                             Callback);
                    executableThreadsWithCaller[CallerIdentity].Add(MustExecuted.PersonId, executableThread);
                    executableThreadsCountWithCaller[CallerIdentity]++;
                }
                executableThreadsWithCaller[CallerIdentity][MustExecuted.PersonId].Executing = true;
                executableThreadsWithCaller[CallerIdentity][MustExecuted.PersonId].Duration.Start();
                executableThreadsWithCaller[CallerIdentity][MustExecuted.PersonId].Param.ToDate = MustExecuted.ToDate;
                return(executableThreadsWithCaller[CallerIdentity][MustExecuted.PersonId].Param);
                //}
            }
            finally
            {
                mut.ReleaseMutex();
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// اگر نخی بیش از 10 ثانیه در حالت اجرا مانده باشد آن را از لیست اجرا حذف می نماید
 /// </summary>
 /// <param name="ExectablePrsCalcList"></param>
 public static void CleanupThreads()
 {
     try
     {
         mut.WaitOne();
         IList <string> MustDeleteCallerIdentities = new List <string>();
         for (int n = 0; n < executableThreadsWithCaller.Count; n++)
         {
             IDictionary <decimal, ExecutableThread> ExecutableThreads =
                 executableThreadsWithCaller.ElementAt(n).Value;
             for (int i = 0; i < ExecutableThreads.Count; i++)
             {
                 ExecutableThread Thread = ExecutableThreads.ElementAt(i).Value;
                 if (Thread.Duration.Elapsed.TotalSeconds > 11160)
                 {
                     if (Thread.Param.ThreadContext != null)
                     {
                         Thread.Param.ThreadContext.Abort();
                     }
                     ExecutableThreads.Remove(Thread.Param.PersonId);
                     executableThreadsCountWithCaller[executableThreadsWithCaller.ElementAt(n).Key]--;
                 }
             }
             if (ExecutableThreads.Count == 0)
             {
                 MustDeleteCallerIdentities.Add(executableThreadsWithCaller.ElementAt(n).Key);
             }
         }
         foreach (string CallerIdentity in MustDeleteCallerIdentities)
         {
             executableThreadsWithCaller.Remove(CallerIdentity);
             executableThreadsCountWithCaller.Remove(CallerIdentity);
         }
     }
     finally
     {
         mut.ReleaseMutex();
     }
 }