コード例 #1
0
        public void DoJob()
        {
            _log.WriteLine(LoggingEvent.Debug, _packingParameter.ToString() + ":started");
            bool succesfull = false;

            using (Process compressionProcess = createCompressProcess(_packingParameter.Arguments))
            {
                try
                {
                    compressionProcess.Start();
                    Thread thread = new Thread(new ParameterizedThreadStart(readOutput));
                    thread.Start(compressionProcess.StandardOutput);

                    notify(new PackingNotificationEventArgs(_packingParameter.ItemToCompress, ProcessingState.InProgress));
                    setPriorityHelper(compressionProcess);

                    // FIXME: This is a workaround of .Net 2 bugs with processing ThreadAbortException and ThreadInterruptException
                    // both bugs were posted to MS
                    // this code is subject to be removed when those bugs will be fixed

                    while (!compressionProcess.HasExited)
                    {
                        Thread.Sleep(100);
                    }

                    while (thread.IsAlive)
                    {
                        Thread.Sleep(1000);
                    }

                    succesfull = isSuccessfull7ZipPacking(compressionProcess.ExitCode);
                    _log.ProcessPackerMessage(_compressionOutput, succesfull);
                    notify(new PackingNotificationEventArgs(_packingParameter.ItemToCompress, succesfull ? ProcessingState.FinishedSuccesfully: ProcessingState.FinishedWithErrors));
                }
                catch (ThreadInterruptedException)
                {
                    succesfull = false;
                    _finished  = null;
                    _log.WriteLine(LoggingEvent.Debug, _packingParameter.ItemToCompress.Target + ": Packing task is aborting...");

                    try
                    {
                        compressionProcess.Kill();
                        _log.WriteLine(LoggingEvent.Debug, _packingParameter.ItemToCompress.Target + ": Packing task is aborted");
                    }
                    catch (InvalidOperationException e)
                    {
                        _log.WriteLine(LoggingEvent.Error,
                                       string.Format(CultureInfo.CurrentCulture, _packingParameter.ItemToCompress.Target + ":" + Translation.Current[467], e.Message));
                    }
                    catch (Win32Exception e)
                    {
                        _log.WriteLine(LoggingEvent.Error,
                                       string.Format(CultureInfo.CurrentCulture, _packingParameter.ItemToCompress.Target + ":" + Translation.Current[467], e.Message));
                    }
                }
                catch (ObjectDisposedException e)
                {
                    _log.WriteLine(LoggingEvent.Error,
                                   string.Format(CultureInfo.CurrentCulture,
                                                 _packingParameter.ItemToCompress.Target + ": " + Translation.Current[469],
                                                 e.Message));
                }
                catch (Win32Exception e)
                {
                    _log.WriteLine(LoggingEvent.Error,
                                   string.Format(CultureInfo.CurrentCulture,
                                                 _packingParameter.ItemToCompress.Target + ": " + Translation.Current[469],
                                                 e.Message));
                }
                finally
                {
                    if (_finished != null)
                    {
                        _finished.Invoke(this, new JobThreadEventArgs(Thread.CurrentThread));
                    }
                }
            }

            _log.WriteLine(LoggingEvent.Debug, _packingParameter.ToString() + ":finished");
        }