/// ------------------------------------------------------------------------------------
        public static bool Generate(TierCollection tierCollection, Control parentControlForDialog,
                                    bool justClearFileContents)
        {
            var timeTier = tierCollection.GetTimeTier();

            if (!CanGenerate(timeTier))
            {
                return(false);
            }

            try
            {
                Program.SuspendBackgroundProcesses();

                if (justClearFileContents)
                {
                    var oralAnnotationFile = timeTier.MediaFileName +
                                             Settings.Default.OralAnnotationGeneratedFileSuffix;

                    // SP-699: The process cannot access the file because it is being used by another process
                    if (File.Exists(oralAnnotationFile))
                    {
                        FileSystemUtils.WaitForFileRelease(oralAnnotationFile);
                    }

                    File.Create(oralAnnotationFile).Dispose();
                    return(false);
                }

                Analytics.Track("Generating Oral Annotation File");

                using (var generator = new OralAnnotationFileGenerator(timeTier,
                                                                       tierCollection.GetIsSegmentIgnored, parentControlForDialog))
                    using (var dlg = new LoadingDlg(GetGeneratingMsg()))
                    {
                        dlg.GenericErrorMessage = GetGenericErrorMsg();
                        var worker = new BackgroundWorker();
                        worker.DoWork += generator.CreateInterleavedAudioFile;
                        worker.WorkerSupportsCancellation = true;
                        dlg.BackgroundWorker = worker;
                        dlg.Show(parentControlForDialog);
                        return(dlg.DialogResult == DialogResult.OK);
                    }
            }
            catch (Exception error)
            {
                ErrorReport.NotifyUserOfProblem(error, GetGenericErrorMsg());
                return(false);
            }
            finally
            {
                Program.ResumeBackgroundProcesses(true);
            }
        }
Esempio n. 2
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// First, we try to get the mutex quickly and quietly. If that fails and this request
        /// was an explicit user choice (as opposed to merely opening the last project), we put
        /// up a dialog and wait 10 seconds, while we wait for the mutex to come free.
        /// </summary>
        /// ------------------------------------------------------------------------------------
        private static bool ObtainTokenForThisProject(string pathToSayMoreProjectFile)
        {
            ReleaseMutexForThisProject();             // Just to be sure.
            Guard.AgainstNull(pathToSayMoreProjectFile, "pathToSayMoreProjectFile");
            var mutexIdBldr = new StringBuilder(pathToSayMoreProjectFile);

            for (int i = 0; i < mutexIdBldr.Length; i++)
            {
                if (mutexIdBldr[i] == Path.DirectorySeparatorChar || mutexIdBldr[i] == Path.VolumeSeparatorChar)
                {
                    mutexIdBldr[i] = '-';
                }
            }
            _mutexId = mutexIdBldr.ToString();
            try
            {
                _oneInstancePerProjectMutex = Mutex.OpenExisting(_mutexId);
                if (_oneInstancePerProjectMutex.WaitOne(1500, false))
                {
                    return(true);
                }
            }
            catch (Exception e)
            {
                if (e is WaitHandleCannotBeOpenedException || e is AbandonedMutexException)
                {
                    bool thisThreadGrantedOwnership;
                    _oneInstancePerProjectMutex = new Mutex(true, _mutexId, out thisThreadGrantedOwnership);
                    if (thisThreadGrantedOwnership)
                    {
                        return(true);
                    }
                }
                else
                {
                    throw;
                }
            }

            if (Application.MessageLoop)
            {
                using (var dlg = new LoadingDlg(LocalizationManager.GetString("MainWindow.WaitingForOtherSayMoreInstance",
                                                                              "Waiting for other instance of SayMore to finish...")))
                {
                    var worker = new BackgroundWorker();
                    worker.DoWork += WaitForProjectMutex;
                    worker.WorkerSupportsCancellation = true;
                    dlg.BackgroundWorker = worker;
                    dlg.Show(null);

                    if (dlg.DialogResult == DialogResult.OK)
                    {
                        return(true);
                    }
                }

                ErrorReport.NotifyUserOfProblem(Format(
                                                    LocalizationManager.GetString("MainWindow.ProjectOpenInOtherSayMore",
                                                                                  "Another instance of SayMore is already open with this project:\r\n{0}\r\n\r\nIf you cannot find that instance of SayMore, restart your computer."),
                                                    pathToSayMoreProjectFile));
            }

            _oneInstancePerProjectMutex = null;
            return(false);
        }