Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //In WCF service
- FileStream fs = null;
- public void AppendBytes(string fileName, byte[] data, long position)
- {
- try
- {
- if (fs==null)//In first call, open the file handle
- fs = System.IO.File.Open(fileName, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.None);
- fs.Write(data, 0, data.Length);
- }
- catch (Exception ex)
- {
- //Close handle in case of error
- if (fs != null)
- fs.Close();
- }
- }
- public void CloseHandle()
- {
- //Close handle explicitly
- if (fs != null)
- fs.Close();
- }
- public void DoSomeOperation(string fileName)
- {
- using (FileStream fsO = System.IO.File.Open(fileName, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.None))
- {
- //Do something with file here, this is atomic operation so I am opening FileStream with 'using' to dispose at operation is over
- }
- }
- //In client
- public void CallerFunction()
- {
- //Read Data from sourceFile in chunk and copy to target file using WCF.AppendBytes on another machine
- WCF.AppendBytes(filename, data, pos);
- WCF.CloseHandle();
- WCF.DoSomeOperation(filename); //I get error here that file is in use with some other process. if I put a thread.sleep(1000) just before this statement then all works fine.
- }
- static void TestHandleClose()
- {
- int i = 0;
- try
- {
- if (File.Exists(@"d:destinationfile2.exe"))
- File.Delete(@"d:destinationfile2.exe");
- byte[] data = null;
- int blocksize = 10 * 1024 * 1024;
- for( i=0;i<100;i++)
- {
- using (FileStream fr = File.Open(@"d:destinationFile1.zip", FileMode.Open, FileAccess.Read, FileShare.None))
- {
- data = new byte[blocksize];
- fr.Read(data, 0, blocksize); //We are reading the file single time but appending same data to target file multiple time.
- using (FileStream f = File.Open(@"d:destinationfile2.exe", FileMode.Append, FileAccess.Write, FileShare.None))
- {
- f.Write(data, 0, data.Length); //We are writing same data multiple times.
- f.Flush();
- f.Close();
- }
- }
- }
- if (File.Exists(@"d:destinationfile2.exe"))
- File.Delete(@"d:destinationfile2.exe");
- }
- catch (Exception ex)
- {
- throw;
- }
- }
- using (FileStream fileStream = new FileStream(file, mode, access, FileShare.ReadWrite))
- {...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement