コード例 #1
0
        public void FSWSimultaneousCreationTest()
        {
            m_beginWriteEvent = new ManualResetEvent(false);

            // test setup
            filesCreated.Clear();
            if (!Directory.Exists(testDir))
            {
                Directory.CreateDirectory(testDir);
            }
            foreach (string fn in filesToCreate)
            {
                if (File.Exists(fn))
                {
                    File.Delete(fn);
                }
            }

            FileSystemWatcher fsw = new OpenNETCF.IO.FileSystemWatcher(testDir, "*.*");

            fsw.IncludeSubdirectories = false;
            fsw.NotifyFilter          = OpenNETCF.IO.NotifyFilters.CreationTime | OpenNETCF.IO.NotifyFilters.DirectoryName | OpenNETCF.IO.NotifyFilters.FileName | OpenNETCF.IO.NotifyFilters.LastAccess;
            fsw.EnableRaisingEvents   = true;
            fsw.Created += new OpenNETCF.IO.FileSystemEventHandler(delegate(object sender, OpenNETCF.IO.FileSystemEventArgs e)
            {
                filesCreated.Add(e.FullPath);
            });

            // start up two threads
            foreach (string fn in filesToCreate)
            {
                ThreadPool.QueueUserWorkItem(CreateFileThreadProc, fn);
            }

            // make the write at the same time
            m_beginWriteEvent.Set();

            // since we're holding things up, let the system dispatch messages
            for (int i = 0; i < 10; i++)
            {
                Application.DoEvents();
                Thread.Sleep(10);
            }

            Assert.AreEqual(filesToCreate.Length, filesCreated.Count, "Incorrect number of files created");

            foreach (string fn in filesCreated)
            {
                Assert.IsTrue(filesCreated.Contains(fn), "Missing file " + fn);
            }

            // reset state and clean up
            m_beginWriteEvent.Close();
            fsw.Dispose();
        }