Example #1
0
        public void ChangePath(string newPath, string newExtension)
        {
            IncubationEnded?.Invoke();

            lock (Incubator)
            {
                if (DiscoveryThread != null && DiscoveryThread.IsBusy)
                {
                    DiscoveryThread.RunWorkerCompleted += (sender, args) =>
                    {
                        FileWatcher.EnableRaisingEvents = false;
                        FileWatcherRaised = false;
                        ShouldAbort       = false;

                        FolderPath    = newPath;
                        FileExtension = newExtension;

                        if (FolderPath == "" || !Directory.Exists(FolderPath) || !IOHelper.CheckFolderPermission(FolderPath))
                        {
                            return;
                        }

                        Incubator.Clear();
                        lock (Ripe)
                            Ripe.Clear();

                        FilesChanged?.Invoke();

                        FileWatcher.Path   = FolderPath;
                        FileWatcher.Filter = FileExtension;
                        FileWatcher.EnableRaisingEvents = true;

                        DiscoveryThread         = new BackgroundWorker();
                        DiscoveryThread.DoWork += WorkLoop;
                        DiscoveryThread.RunWorkerAsync();
                    };

                    ShouldAbort = true;
                }
                else
                {
                    FolderPath    = newPath;
                    FileExtension = newExtension;

                    if (FolderPath == "" || !Directory.Exists(FolderPath) || !IOHelper.CheckFolderPermission(FolderPath))
                    {
                        return;
                    }

                    Incubator.Clear();
                    lock (Ripe)
                        Ripe.Clear();

                    FilesChanged?.Invoke();

                    FileWatcher.Path   = FolderPath;
                    FileWatcher.Filter = FileExtension;
                    FileWatcher.EnableRaisingEvents = true;

                    DiscoveryThread         = new BackgroundWorker();
                    DiscoveryThread.DoWork += WorkLoop;
                    DiscoveryThread.RunWorkerAsync();
                }
            }
        }
Example #2
0
        void WorkLoop(object sender, EventArgs e)
        {
            while (true)
            {
                if (ShouldAbort)
                {
                    return;
                }

                Stopwatch Watch            = new Stopwatch();
                bool      EventNeedsFiring = false;
                FileWatcherRaised = false;

                try
                {
                    foreach (var fileName in Directory.EnumerateFiles(FolderPath, FileExtension, SearchOption.TopDirectoryOnly))
                    {
                        if (ShouldAbort)
                        {
                            return;
                        }

                        if (GetMovie(fileName) != null)
                        {
                            continue;
                        }

                        string NameXML = fileName.Substring(0, fileName.LastIndexOf(".")) + ".xml";

                        if (!File.Exists(NameXML))
                        {
                            FileInfo Info = new FileInfo(fileName);
                            Tuple <string, Stopwatch, long> CurrentState = GetIncubating(fileName);
                            if (CurrentState == null)
                            {
                                Stopwatch Timer = new Stopwatch();
                                Timer.Start();
                                lock (Incubator)
                                {
                                    Incubator.Add(new Tuple <string, Stopwatch, long>(fileName, Timer, Info.Length));
                                    if (Incubator.Count == 1)
                                    {
                                        IncubationStarted?.Invoke();
                                    }
                                }
                            }
                            else
                            {
                                // Check if
                                bool CanRead = false;
                                try
                                {
                                    File.OpenRead(fileName).Close();
                                    CanRead = true;
                                }
                                catch
                                {
                                }

                                if (Info.Length != CurrentState.Item3 || !CanRead)
                                {
                                    lock (Incubator)
                                        Incubator.Remove(CurrentState);
                                    Stopwatch Timer = new Stopwatch();
                                    Timer.Start();
                                    lock (Incubator)
                                        Incubator.Add(new Tuple <string, Stopwatch, long>(fileName, Timer, Info.Length));
                                }
                                else if (CurrentState.Item2.ElapsedMilliseconds > 1000)
                                {
                                    lock (Ripe)
                                    {
                                        if (!Ripe.Exists(m => m.Path == fileName))
                                        {
                                            // Make sure the list is sorted
                                            int InsertAt = 0;
                                            while (InsertAt < Ripe.Count && Ripe[InsertAt].Path.CompareTo(fileName) < 0)
                                            {
                                                InsertAt++;
                                            }

                                            if (!fileName.Substring(fileName.Length - 8).ToLower().Contains("tomostar"))
                                            {
                                                Ripe.Insert(InsertAt, new Movie(fileName));
                                            }
                                            else
                                            {
                                                Ripe.Insert(InsertAt, new TiltSeries(fileName));
                                            }
                                        }
                                    }

                                    EventNeedsFiring = true;
                                    if (!Watch.IsRunning)
                                    {
                                        Watch.Start();
                                    }

                                    lock (Incubator)
                                    {
                                        Incubator.Remove(CurrentState);
                                        if (Incubator.Count == 0)
                                        {
                                            IncubationEnded?.Invoke();
                                        }
                                    }
                                }
                            }

                            if (EventNeedsFiring && Watch.ElapsedMilliseconds > 500)
                            {
                                Watch.Stop();
                                Watch.Reset();
                                EventNeedsFiring = false;

                                FilesChanged?.Invoke();
                            }
                        }
                        else
                        {
                            lock (Ripe)
                            {
                                if (!Ripe.Exists(m => m.Path == fileName))
                                {
                                    // Make sure the list is sorted
                                    int InsertAt = 0;
                                    while (InsertAt < Ripe.Count && Ripe[InsertAt].Path.CompareTo(fileName) < 0)
                                    {
                                        InsertAt++;
                                    }

                                    if (!fileName.Substring(fileName.Length - 8).ToLower().Contains("tomostar"))
                                    {
                                        Ripe.Insert(InsertAt, new Movie(fileName));
                                    }
                                    else
                                    {
                                        Ripe.Insert(InsertAt, new TiltSeries(fileName));
                                    }
                                }
                            }

                            EventNeedsFiring = true;
                            if (!Watch.IsRunning)
                            {
                                Watch.Start();
                            }

                            if (EventNeedsFiring && Watch.ElapsedMilliseconds > 500)
                            {
                                Watch.Stop();
                                Watch.Reset();
                                EventNeedsFiring = false;

                                FilesChanged?.Invoke();
                            }
                        }
                    }
                }
                catch (Exception exc)
                {
                    Debug.WriteLine("FileDiscoverer crashed:");
                    Debug.WriteLine(exc);
                }

                if (EventNeedsFiring)
                {
                    FilesChanged?.Invoke();
                }

                while (!IsIncubating() && !FileWatcherRaised)
                {
                    if (ShouldAbort)
                    {
                        return;
                    }
                    Thread.Sleep(50);
                }
            }
        }
Example #3
0
        void WorkLoop(object sender, EventArgs e)
        {
            while (true)
            {
                if (ShouldAbort)
                {
                    return;
                }

                Stopwatch Watch            = new Stopwatch();
                bool      EventNeedsFiring = false;
                FileWatcherRaised = false;

                try
                {
                    foreach (var fileName in Directory.EnumerateFiles(FolderPath, FileExtension, SearchOption.TopDirectoryOnly).ToArray())
                    {
                        if (ShouldAbort)
                        {
                            return;
                        }

                        if (GetMdoc(fileName) != null)
                        {
                            continue;
                        }

                        FileInfo           Info         = new FileInfo(fileName);
                        MdocIncubatorEntry CurrentState = GetIncubating(fileName);
                        Mdoc ParsedEntry    = null;
                        int  AvailableTilts = 0;
                        try
                        {
                            ParsedEntry    = Mdoc.FromFile(new[] { fileName }, DefaultTiltDose);
                            AvailableTilts = ParsedEntry.Entries.Count;
                        } catch { }

                        if (CurrentState == null)
                        {
                            Stopwatch Timer = new Stopwatch();
                            Timer.Start();
                            lock (Incubator)
                            {
                                Incubator.Add(new MdocIncubatorEntry(fileName, Timer, Info.Length, AvailableTilts));
                                if (Incubator.Count == 1)
                                {
                                    IncubationStarted?.Invoke();
                                }
                            }
                        }
                        else
                        {
                            // Check if
                            bool CanRead = false;
                            try
                            {
                                File.OpenRead(fileName).Close();
                                CanRead = true;
                            }
                            catch
                            {
                            }

                            if (ParsedEntry == null || Info.Length != CurrentState.Size || CurrentState.NTilts != AvailableTilts || !CanRead)
                            {
                                lock (Incubator)
                                    Incubator.Remove(CurrentState);
                                Stopwatch Timer = new Stopwatch();
                                Timer.Start();
                                lock (Incubator)
                                    Incubator.Add(new MdocIncubatorEntry(fileName, Timer, Info.Length, AvailableTilts));
                            }
                            else if ((CurrentState.Lifetime.ElapsedMilliseconds > 1000 && AvailableTilts >= ExpectedNTilts) ||
                                     (AcceptAnywayAfter > 0 && CurrentState.Lifetime.ElapsedMilliseconds > AcceptAnywayAfter))
                            {
                                //lock (Ripe)
                                {
                                    if (!Ripe.Exists(m => m.Path == fileName))
                                    {
                                        while (CreationTasks.Count > 15)
                                        {
                                            Thread.Sleep(1);
                                        }

                                        Task CreationTask = new Task(() =>
                                        {
                                            Mdoc Created = ParsedEntry;

                                            lock (Ripe)
                                            {
                                                // Make sure the list is sorted
                                                int InsertAt = 0;
                                                while (InsertAt < Ripe.Count && Ripe[InsertAt].Path.CompareTo(fileName) < 0)
                                                {
                                                    InsertAt++;
                                                }

                                                Ripe.Insert(InsertAt, Created);
                                            }

                                            lock (CreationTasks)
                                                CreationTasks.Remove(fileName);
                                        });

                                        lock (CreationTasks)
                                            CreationTasks.Add(fileName, CreationTask);

                                        CreationTask.Start();
                                    }
                                }

                                EventNeedsFiring = true;
                                if (!Watch.IsRunning)
                                {
                                    Watch.Start();
                                }

                                lock (Incubator)
                                {
                                    Incubator.Remove(CurrentState);
                                    if (Incubator.Count == 0)
                                    {
                                        IncubationEnded?.Invoke();
                                    }
                                }
                            }
                        }

                        if (EventNeedsFiring && Watch.ElapsedMilliseconds > 500)
                        {
                            //Task.WaitAll(CreationTasks.ToArray());
                            //CreationTasks.Clear();

                            Watch.Stop();
                            Watch.Reset();
                            EventNeedsFiring = false;

                            FilesChanged?.Invoke();
                        }
                    }
                }
                catch (Exception exc)
                {
                    Debug.WriteLine("FileDiscoverer crashed:");
                    Debug.WriteLine(exc);

                    if (ExceptionsLogged < 100)
                    {
                        using (TextWriter Writer = File.AppendText("d_filediscoverer.txt"))
                        {
                            Writer.WriteLine(DateTime.Now + ":");
                            Writer.WriteLine(exc.ToString());
                            Writer.WriteLine("");
                            ExceptionsLogged++;
                        }
                    }
                }

                while (CreationTasks.Count > 0)
                {
                    Thread.Sleep(1);
                }

                if (EventNeedsFiring)
                {
                    FilesChanged?.Invoke();
                }

                while (!IsIncubating() && !FileWatcherRaised)
                {
                    if (ShouldAbort)
                    {
                        return;
                    }
                    Thread.Sleep(50);
                }
            }
        }
Example #4
0
        public void ChangePath(string newPath)
        {
            IncubationEnded?.Invoke();

            lock (Incubator)
            {
                if (DiscoveryThread != null && DiscoveryThread.IsBusy)
                {
                    DiscoveryThread.RunWorkerCompleted += (sender, args) =>
                    {
                        FileWatcher.EnableRaisingEvents = false;
                        FileWatcherRaised = false;
                        ShouldAbort       = false;

                        FolderPath = newPath;

                        if (FolderPath == "" || !Directory.Exists(FolderPath) || !IOHelper.CheckFolderPermission(FolderPath))
                        {
                            return;
                        }

                        Task.WaitAll(CreationTasks.Values.ToArray());
                        //Thread.Sleep(500);  // There might still be item creation tasks running asynchro

                        Incubator.Clear();
                        lock (Ripe)
                            Ripe.Clear();

                        FilesChanged?.Invoke();

                        FileWatcher.Path   = FolderPath;
                        FileWatcher.Filter = FileExtension;
                        FileWatcher.EnableRaisingEvents = true;

                        DiscoveryThread         = new BackgroundWorker();
                        DiscoveryThread.DoWork += WorkLoop;
                        DiscoveryThread.RunWorkerAsync();
                    };

                    ShouldAbort = true;
                }
                else
                {
                    FolderPath = newPath;

                    if (FolderPath == "" || !Directory.Exists(FolderPath) || !IOHelper.CheckFolderPermission(FolderPath))
                    {
                        return;
                    }

                    Incubator.Clear();
                    lock (Ripe)
                        Ripe.Clear();

                    FilesChanged?.Invoke();

                    FileWatcher.Path   = FolderPath;
                    FileWatcher.Filter = FileExtension;
                    FileWatcher.EnableRaisingEvents = true;

                    DiscoveryThread         = new BackgroundWorker();
                    DiscoveryThread.DoWork += WorkLoop;
                    DiscoveryThread.RunWorkerAsync();
                }
            }
        }
Example #5
0
        void WorkLoop(object sender, EventArgs e)
        {
            string[] ParticleFileNames = null;
            if (Directory.Exists(Path.Combine(FolderPath, "matching")))
            {
                ParticleFileNames = Directory.EnumerateFiles(Path.Combine(FolderPath, "matching"), "*.star", SearchOption.TopDirectoryOnly).Select(p => Helper.PathToNameWithExtension(p)).ToArray();
            }

            while (true)
            {
                if (ShouldAbort)
                {
                    return;
                }

                Stopwatch Watch            = new Stopwatch();
                bool      EventNeedsFiring = false;
                FileWatcherRaised = false;

                try
                {
                    foreach (var fileName in Directory.EnumerateFiles(FolderPath, FileExtension, SearchOption.TopDirectoryOnly).ToArray())
                    {
                        if (ShouldAbort)
                        {
                            return;
                        }

                        if (GetMovie(fileName) != null)
                        {
                            continue;
                        }

                        string NameXML = fileName.Substring(0, fileName.LastIndexOf(".")) + ".xml";

                        if (!File.Exists(NameXML))
                        {
                            FileInfo Info = new FileInfo(fileName);
                            Tuple <string, Stopwatch, long> CurrentState = GetIncubating(fileName);
                            if (CurrentState == null)
                            {
                                Stopwatch Timer = new Stopwatch();
                                Timer.Start();
                                lock (Incubator)
                                {
                                    Incubator.Add(new Tuple <string, Stopwatch, long>(fileName, Timer, Info.Length));
                                    if (Incubator.Count == 1)
                                    {
                                        IncubationStarted?.Invoke();
                                    }
                                }
                            }
                            else
                            {
                                // Check if
                                bool CanRead = false;
                                try
                                {
                                    File.OpenRead(fileName).Close();
                                    CanRead = true;
                                }
                                catch
                                {
                                }

                                if (Info.Length != CurrentState.Item3 || !CanRead)
                                {
                                    lock (Incubator)
                                        Incubator.Remove(CurrentState);
                                    Stopwatch Timer = new Stopwatch();
                                    Timer.Start();
                                    lock (Incubator)
                                        Incubator.Add(new Tuple <string, Stopwatch, long>(fileName, Timer, Info.Length));
                                }
                                else if (CurrentState.Item2.ElapsedMilliseconds > 1000)
                                {
                                    //lock (Ripe)
                                    {
                                        if (!Ripe.Exists(m => m.Path == fileName))
                                        {
                                            while (CreationTasks.Count > 15)
                                            {
                                                Thread.Sleep(1);
                                            }

                                            Task CreationTask = new Task(() =>
                                            {
                                                Movie Created = null;
                                                if (!fileName.Substring(fileName.Length - 8).ToLower().Contains("tomostar"))
                                                {
                                                    Created = new Movie(fileName, ParticleFileNames);
                                                }
                                                else
                                                {
                                                    Created = new TiltSeries(fileName);
                                                }

                                                lock (Ripe)
                                                {
                                                    // Make sure the list is sorted
                                                    int InsertAt = 0;
                                                    while (InsertAt < Ripe.Count && Ripe[InsertAt].Path.CompareTo(fileName) < 0)
                                                    {
                                                        InsertAt++;
                                                    }

                                                    Ripe.Insert(InsertAt, Created);
                                                }

                                                lock (CreationTasks)
                                                    CreationTasks.Remove(fileName);
                                            });

                                            lock (CreationTasks)
                                                CreationTasks.Add(fileName, CreationTask);

                                            CreationTask.Start();
                                        }
                                    }

                                    EventNeedsFiring = true;
                                    if (!Watch.IsRunning)
                                    {
                                        Watch.Start();
                                    }

                                    lock (Incubator)
                                    {
                                        Incubator.Remove(CurrentState);
                                        if (Incubator.Count == 0)
                                        {
                                            IncubationEnded?.Invoke();
                                        }
                                    }
                                }
                            }

                            if (EventNeedsFiring && Watch.ElapsedMilliseconds > 500)
                            {
                                //Task.WaitAll(CreationTasks.ToArray());
                                //CreationTasks.Clear();

                                Watch.Stop();
                                Watch.Reset();
                                EventNeedsFiring = false;

                                FilesChanged?.Invoke();
                            }
                        }
                        else
                        {
                            //lock (Ripe)
                            {
                                if (!Ripe.Exists(m => m.Path == fileName))
                                {
                                    while (CreationTasks.Count > 15)
                                    {
                                        Thread.Sleep(1);
                                    }

                                    Task CreationTask = new Task(() =>
                                    {
                                        Movie Created = null;
                                        if (!fileName.Substring(fileName.Length - 8).ToLower().Contains("tomostar"))
                                        {
                                            Created = new Movie(fileName, ParticleFileNames);
                                        }
                                        else
                                        {
                                            Created = new TiltSeries(fileName);
                                        }

                                        lock (Ripe)
                                        {
                                            // Make sure the list is sorted
                                            int InsertAt = 0;
                                            while (InsertAt < Ripe.Count && Ripe[InsertAt].Path.CompareTo(fileName) < 0)
                                            {
                                                InsertAt++;
                                            }

                                            Ripe.Insert(InsertAt, Created);
                                        }

                                        lock (CreationTasks)
                                            CreationTasks.Remove(fileName);
                                    });

                                    lock (CreationTasks)
                                        CreationTasks.Add(fileName, CreationTask);

                                    CreationTask.Start();
                                }
                            }

                            EventNeedsFiring = true;
                            if (!Watch.IsRunning)
                            {
                                Watch.Start();
                            }

                            if (EventNeedsFiring && Watch.ElapsedMilliseconds > 500)
                            {
                                //Task.WaitAll(CreationTasks.ToArray());
                                //CreationTasks.Clear();

                                Watch.Stop();
                                Watch.Reset();
                                EventNeedsFiring = false;

                                FilesChanged?.Invoke();
                            }
                        }
                    }
                }
                catch (Exception exc)
                {
                    Debug.WriteLine("FileDiscoverer crashed:");
                    Debug.WriteLine(exc);

                    if (ExceptionsLogged < 100)
                    {
                        using (TextWriter Writer = File.AppendText("d_filediscoverer.txt"))
                        {
                            Writer.WriteLine(DateTime.Now + ":");
                            Writer.WriteLine(exc.ToString());
                            Writer.WriteLine("");
                            ExceptionsLogged++;
                        }
                    }
                }

                while (CreationTasks.Count > 0)
                {
                    Thread.Sleep(1);
                }

                if (EventNeedsFiring)
                {
                    FilesChanged?.Invoke();
                }

                while (!IsIncubating() && !FileWatcherRaised)
                {
                    if (ShouldAbort)
                    {
                        return;
                    }
                    Thread.Sleep(50);
                }
            }
        }