/// <summary> /// 若从服务器启动则运行Loader,否则关闭 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Loader_Load(object sender, EventArgs e) { if (Application.StartupPath.StartsWith("\\\\10.")) { CopyThread.RunWorkerAsync(); } else { TestSet.Init(); this.Close(); } }
public virtual void TestCopyBytesWithThreads() { int datalen = TestUtil.NextInt(Random(), 101, 10000); byte[] data = new byte[datalen]; Random().NextBytes(data); Directory d = NewDirectory(); IndexOutput output = d.CreateOutput("data", IOContext.DEFAULT); output.WriteBytes(data, 0, datalen); output.Dispose(); IndexInput input = d.OpenInput("data", IOContext.DEFAULT); IndexOutput outputHeader = d.CreateOutput("header", IOContext.DEFAULT); // copy our 100-byte header outputHeader.CopyBytes(input, 100); outputHeader.Dispose(); // now make N copies of the remaining bytes CopyThread[] copies = new CopyThread[10]; for (int i = 0; i < copies.Length; i++) { copies[i] = new CopyThread((IndexInput)input.Clone(), d.CreateOutput("copy" + i, IOContext.DEFAULT)); } for (int i = 0; i < copies.Length; i++) { copies[i].Start(); } for (int i = 0; i < copies.Length; i++) { copies[i].Join(); } for (int i = 0; i < copies.Length; i++) { IndexInput copiedData = d.OpenInput("copy" + i, IOContext.DEFAULT); byte[] dataCopy = new byte[datalen]; System.Buffer.BlockCopy(data, 0, dataCopy, 0, 100); // copy the header for easy testing copiedData.ReadBytes(dataCopy, 100, datalen - 100); Assert.AreEqual(data, dataCopy); copiedData.Dispose(); } input.Dispose(); d.Dispose(); }
/// <summary> /// 递归复制文件 /// </summary> /// <param name="source"></param> /// <param name="dest"></param> private void DeepCopyDirectory(DirectoryInfo source, DirectoryInfo dest) { if (!source.Exists) { MessageBox.Show("目标文件夹不存在!"); return; } if (!dest.Exists) { dest.Create(); } foreach (FileInfo fl in source.GetFiles()) { try { CopyThread.ReportProgress(10, "Copy:" + fl.Name + "\n"); File.Copy(fl.FullName, Path.Combine(dest.FullName, fl.Name), true); } catch (Exception x) { MessageBox.Show(x.Message + "\r\n复制文件出错:" + fl.FullName); } } foreach (DirectoryInfo dir in source.GetDirectories()) { try { DirectoryInfo sub = new DirectoryInfo(Path.Combine(dest.FullName, dir.Name)); if (!sub.Exists) { sub.Create(); CopyThread.ReportProgress(11, "Create Dir:" + dir.Name + "\n"); } DeepCopyDirectory(dir, sub); } catch (Exception x) { MessageBox.Show(x.Message + "\r\n创建文件夹出错:" + dir.FullName); } } }