public IResult CreateDirectory(string name) { string tempPath = Path.GetTempPath(); IResult result = new Result { Success = true }; string pathArchiv = tempPath + name; using (Stream stream = File.OpenRead(GameEntityHolder.PathToArchive)) { IReader reader = ReaderFactory.Open(stream); while (reader.MoveToNextEntry()) { if (reader.Entry.IsDirectory) { Directory.CreateDirectory(pathArchiv + reader.Entry.FilePath); } } } using (Stream stream = File.OpenRead(GameEntityHolder.PathToArchive)) { IReader reader = ReaderFactory.Open(stream); while (reader.MoveToNextEntry()) { if (!reader.Entry.IsDirectory) { Mydelegate del = CreateFile; IAsyncResult asyncResult; string path = pathArchiv + reader.Entry.FilePath; asyncResult = del.BeginInvoke(path, null, null); del.EndInvoke(asyncResult); } } } using (Stream stream = File.OpenRead(GameEntityHolder.PathToArchive)) { IReader reader = ReaderFactory.Open(stream); while (reader.MoveToNextEntry()) { if (!reader.Entry.IsDirectory) { string path = pathArchiv + reader.Entry.FilePath; // MemoryStream streamFile = new MemoryStream(); reader.WriteEntryTo(path); } } } return(result); }
public static void CallMe() { DelegateExample Classobj = new DelegateExample(); Mydelegate delobj = new Mydelegate(Classobj.CalledViaDelegate); //delobj += Classobj.CalledViaDelegate; delobj.BeginInvoke(new AsyncCallback(CallbackMethod), null); delobj(); }
static void Main(string[] args) { //3.实例化委托 Mydelegate m1 = new Mydelegate(MyFangFa); //异步执行结果 IAsyncResult result = m1.BeginInvoke("Hans ", null, null); while (!result.AsyncWaitHandle.WaitOne(2000)) { Console.WriteLine("异步线程未完成,先执行这里"); } string data = m1.EndInvoke(result); Console.WriteLine(data); Console.ReadLine(); }
static void Main(string[] args) { //实例化一个委托 Mydelegate m1 = new Mydelegate(MyFangfa); //异步调用两个方法 //方法一: //string result= m1("hans"); // Console.WriteLine(result); //方法二: // IAsyncResult result = m1.BeginInvoke("hans", null, null); //第一个null 是回调函数名称,第二个null 是回调函数的 object参数 string data = m1.EndInvoke(result); //endinvoke是会线程阻塞的,保证其他线程执行完,才会执行后面 Console.WriteLine(data); Console.ReadLine(); }