コード例 #1
0
        static void CreateXMLWriter(MainForm mainForm, DirectoryScanner scanner)
        {
            // to use it in onScanFinish bellow
            DirectoryXMLWriter directoryWriter = null;
            AsyncEventSubscriber <ItemFoundEventArgs> subscriber = null;

            // todo: move it to separate class
            mainForm.onScanStart += (DirectoryScanEventArgs e) =>
            {
                // todo: check second start
                try
                {
                    // todo: move to the lock
                    // so we could be sure that previous file is closed
                    XmlWriter xmlWriter = XmlWriter.Create(e.XMLFilePath);
                    directoryWriter = new DirectoryXMLWriter(xmlWriter);
                }
                catch
                {
                    MessageBox.Show("File is not writable");

                    return;
                }
                // write first lines
                directoryWriter.Start();

                subscriber           = new AsyncEventSubscriber <ItemFoundEventArgs>();
                scanner.onItemFound += subscriber.Handler;
                subscriber.OnEvent  += (object sender, ItemFoundEventArgs ev) =>
                {
                    lock (directoryWriter)
                    {
                        if (ev.Info is DirectoryInfo)
                        {
                            directoryWriter.StartDirectory();
                            directoryWriter.WriteDirectory(ev.Info as DirectoryInfo);
                            directoryWriter.EndDirectory();
                        }
                        else if (ev.Info is FileInfo)
                        {
                            directoryWriter.StartFile();
                            directoryWriter.WriteFile(ev.Info as FileInfo);
                            directoryWriter.EndFile();
                        }
                    }
                };

                // start subscriber in new thread
                subscriber.Thread.Start();
            };

            mainForm.onScanFinish += (DirectoryScanEventArgs e) =>
            {
                // last one failed?
                if (directoryWriter == null)
                {
                    return;
                }
                // to be sure close file after writing
                // are we sure?
                lock (directoryWriter)
                {
                    try
                    {
                        subscriber.Thread.Abort();
                        directoryWriter.End();
                    }
                    catch
                    {
                        // can be failed on second run
                    }
                }
            };
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: THROYAN/VTree
        static void CreateSubscribers(DirectoryScanner scanner)
        {
            scanner.onItemFound += (object sender, ItemFoundEventArgs e) =>
            {
                lock (q1)
                {
                    q1.Enqueue(e);
                    Monitor.Pulse(q1);
                }
            };
            scanner.onItemFound += (object sender, ItemFoundEventArgs e) =>
            {
                lock (q2)
                {
                    q2.Enqueue(e);
                    Monitor.Pulse(q2);
                }
            };

            var thread = new Thread(() =>
            {
                log("subscribe");

                while (true)
                {
                    lock (q1)
                    {
                        while (q1.Count == 0)
                        {
                            Monitor.Wait(q1);
                        }
                        ItemFoundEventArgs i = q1.Dequeue();
                        log(i.Info.FullName);
                    }
                }
            });

            thread.Name = "Console output";

            var thread2 = new Thread(() =>
            {
                log("subscribe");

                while (true)
                {
                    lock (q2)
                    {
                        while (q2.Count == 0)
                        {
                            Monitor.Wait(q2);
                        }
                        ItemFoundEventArgs i = q2.Dequeue();
                        log(i.Info.FullName);
                    }
                }
            });

            thread2.Name = "Console output!!!!";

            thread.Start();
            thread2.Start();

            AsyncEventSubscriber <ItemFoundEventArgs> outputToConsole = new AsyncEventSubscriber <ItemFoundEventArgs>();

            outputToConsole.OnEvent += (object sender, ItemFoundEventArgs e) => {
                log(e.Info.FullName);
            };
            scanner.onItemFound += outputToConsole.Handler;
            outputToConsole.Thread.Start();

            // xml
            XmlWriter xmlWriter = XmlWriter.Create("1.xml");

            directoryWriter = new DirectoryXMLWriter(xmlWriter);
            AsyncEventSubscriber <ItemFoundEventArgs> writeToFile = new AsyncEventSubscriber <ItemFoundEventArgs>();
            bool first = true;

            writeToFile.OnEvent += (object sender, ItemFoundEventArgs e) => {
                lock (directoryWriter)
                {
                    if (first)
                    {
                        first = false;
                        directoryWriter.Start();
                    }

                    if (e.Info is DirectoryInfo)
                    {
                        directoryWriter.WriteDirectory(e.Info as DirectoryInfo);
                    }
                    else
                    {
                        directoryWriter.WriteFile(e.Info as FileInfo);
                    }
                }
            };
            scanner.onItemFound += writeToFile.Handler;
            writeToFile.Thread.Start();
        }
コード例 #3
0
        static void CreateTreeXMLWriter(MainForm mainForm, DirectoryScanner scanner)
        {
            DirectoryTree tree = new DirectoryTree();
            AsyncEventSubscriber <ItemFoundEventArgs> subscriber = null;

            // todo: move it to separate class
            mainForm.onScanStart += (DirectoryScanEventArgs e) =>
            {
                tree.Clear();
                // todo: check second start
                subscriber           = new AsyncEventSubscriber <ItemFoundEventArgs>();
                scanner.onItemFound += subscriber.Handler;
                subscriber.OnEvent  += (object sender, ItemFoundEventArgs ev) =>
                {
                    lock (tree)
                    {
                        if (ev.Info is DirectoryInfo)
                        {
                            tree.AddDirectory(ev.Info as DirectoryInfo);
                        }
                        else if (ev.Info is FileInfo)
                        {
                            tree.AddFile(ev.Info as FileInfo);
                        }
                    }
                };

                subscriber.Thread.Start();
            };

            mainForm.onScanFinish += (DirectoryScanEventArgs e) =>
            {
                lock (tree)
                {
                    // it hope it's not null
                    subscriber.Thread.Abort();

                    // write XML in brand new thread
                    new Thread(() =>
                    {
                        // big one
                        try
                        {
                            XmlWriter xmlWriter = XmlWriter.Create(e.XMLFilePath + "-deep.xml");
                            DirectoryXMLWriter directoryWriter = new DirectoryXMLWriter(xmlWriter);

                            Action <DirectoryModel> writeDirFunc = null;
                            writeDirFunc = (DirectoryModel directory) =>
                            {
                                directoryWriter.StartDirectory();
                                directoryWriter.WriteDirectory(directory.Info);
                                // am I lazy?
                                // I cannot understand the domain
                                // I cannot really come up with some place I can move this shit to
                                // should I create one more "DirectoryXMLWriter" or maybe replace that one with this?
                                xmlWriter.WriteElementString("size", directory.Size + " bytes");
                                xmlWriter.WriteStartElement("directories");
                                directory.Directories.ForEach(writeDirFunc);
                                xmlWriter.WriteEndElement();
                                xmlWriter.WriteStartElement("files");
                                directory.Files.ForEach((FileInfo file) =>
                                {
                                    directoryWriter.StartFile();
                                    directoryWriter.WriteFile(file);
                                    directoryWriter.EndFile();
                                });
                                xmlWriter.WriteEndElement();
                                directoryWriter.EndDirectory();
                            };

                            directoryWriter.Start();
                            writeDirFunc(tree.GetRoot());
                            directoryWriter.End();
                        }
                        catch
                        {
                            MessageBox.Show("File is not writable");

                            return;
                        }
                    }).Start();
                }
            };
        }