private void RaiseInfoChanged(FileCalculationInfo info)
 {
     if (InfoChanged != null)
         InfoChanged(this, info);
 }
        private void SetInfo(FileCalculationInfo info)
        {
            lock(infoLogLock)
            {
                if (!infoLog.Contains(info) && info != null)
                    infoLog.Add(info);

                while (infoLog.Count > 100)
                {
                    var item = infoLog.FirstOrDefault();
                    if(item != null)
                    {
                        if (System.IO.File.Exists(item.LogFilePath))
                        try
                        {
                            System.IO.File.Delete(item.LogFilePath);
                        }
                        catch { }
                        infoLog.Remove(item);
                    }
                }

                var statisticLog = System.IO.Path.Combine(Parameters.GetParameter("CalculationLogs"), "log.xml");
                System.IO.File.WriteAllText(statisticLog, infoLog.SerializeToXML(true));
            }
            RaiseInfoChanged(info);
        }
        private void CalculationThread(object param)
        {
            try
            {
                while (true)
                {
                    DateTime dt;
                    if (!DateTime.TryParse(Parameters.GetParameter("StartFromDate"), out dt))
                        dt = DateTime.Now.AddDays(-30);

                    int timeOut;
                    if (!int.TryParse(Parameters.GetParameter("ProcessTimeout"), out timeOut))
                        timeOut = 180;

                    var el = Queue.Pop();
                    if (el != null)
                    {
                        if (el.Changed >= dt)
                            try
                            {
                                LogMessage(string.Format("Файл '{0}' (дата изменения: {1}) принят в обработку", el.File, el.Changed.ToString("yyyy.MM.dd hh:mm:ss")));
                                dt = el.Changed;
                                var info = new FileCalculationInfo(Parameters.GetParameter("CalculationLogs"))
                                {
                                    Status = FileCalculationStatus.InAction,
                                    File = el.File,
                                    LogEncoding = el.LogEncoding
                                };
                                try
                                {
                                    SetInfo(info);

                                    string command = el.Command;
                                    string arguments = " >" + info.LogFilePath;

                                    if (el.Command.IndexOf(" ") > 0)
                                    {
                                        if (el.Command.FirstOrDefault() == '\"')
                                        {
                                            var ind = el.Command.Substring(1).IndexOf("\"") + 2;
                                            command = el.Command.Substring(0, ind);
                                            arguments = el.Command.Substring(ind + 1) + arguments;
                                        }
                                        else
                                        {
                                            var ind = el.Command.IndexOf(" ");
                                            command = el.Command.Substring(0, ind);
                                            arguments = el.Command.Substring(ind) + arguments;
                                        }
                                    }

                                    LogMessage(string.Format("For file '{0}' start command '{1}' with arguments '{2}'", el.File, command, arguments));

                                    var psi = new ProcessStartInfo(command, arguments);
                                    psi.WindowStyle = ProcessWindowStyle.Hidden;
                                    using(var process = Process.Start(psi))
                                    try
                                    {
                                        if (process.WaitForExit(timeOut * 1000))
                                        {
                                            info.DateEnd = DateTime.UtcNow;
                                            info.Status = FileCalculationStatus.Done;
                                            SetInfo(info);
                                        }
                                        else
                                            throw new Exception("Процесс выполняется слишком долго");
                                    }
                                    catch (ThreadAbortException ex)
                                    {
                                        if (process != null)
                                            try
                                            {
                                                process.Kill();
                                            }
                                            catch { }
                                        throw ex;
                                    }

                                    Parameters.SetParameter("StartFromDate", dt.ToString());
                                }
                                catch (Exception ex)
                                {
                                    info.Error = ex.GetFullText(false);
                                    info.DateEnd = DateTime.UtcNow;
                                    info.Status = FileCalculationStatus.Error;
                                    SetInfo(info);
                                    throw ex;
                                }
                            }
                            catch (ThreadAbortException ex)
                            {
                                Queue.Push(el);
                                throw ex;
                            }
                        else
                            LogMessage(string.Format("Файл '{0}' (дата изменения: {1}) пропущен", el.File, el.Changed.ToString("yyyy.MM.dd hh:mm:ss")));
                    }
                    else
                        Thread.Sleep(1000);
                }
            }
            catch (ThreadAbortException)
            {
                LogMessage("Поток остановлен пользователем");
            }
            catch(Exception ex)
            {
                LogMessage(ex.Message, ex);
            }
            finally
            {
                SetInfo(null);
            }
        }