Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / August 2006

Tip: Looking for answers? Try searching our database.

File.Flush() - return instantly

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
David Thielen - 24 Aug 2006 17:41 GMT
Hi;

Is there a way, without creating a worker thread, to call Flush and the call
returns instantly and the file will be set to flush soon. I am ok if it does
not flush before returning - I just want it to flush in the next second or so.

Signature

thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm

"Peter Huang" [MSFT] - 25 Aug 2006 03:35 GMT
Hi Dave,

To make a call returned immediately is called asynchronous processing.
If we make a call the current thread will block until the current call
return, this is called synchronous processing.

So for your scenario, it seems that we need asynchronous processing.
Asynchronous processing in .NET part 1: fundamentals
http://www.simple-talk.com/dotnet/windows-forms/asynchronous-processing-in-.
net-part-1-fundamentals/

In .NET we provided a few approaches, start a new thread(which you did not
want), the thread pool, Asynchronous delegates, but the key point is to
create another thread, because we want to do two jobs in the same time. One
is the calling thread and the other is the method.

Including Asynchronous Calls
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconasynchronousprogramming.asp

Also based on my knowledge, the File.Flush should return soon, do you have
special scenario to run it asynchronously.

If I have any misunderstanding, please feel free to let me know.

Thanks!

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
David Thielen - 25 Aug 2006 05:37 GMT
Hi;

I figured there wasn't an option (I've written file systems and they never
have this). Flush will write the file to disk before returning. Without Flush
the most recent writes to a file can stay in memory forever.

I was hoping for a third approach - the contents will be written to disk
soon, but the call returns immediately.

Signature

thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm

> Hi Dave,
>
[quoted text clipped - 33 lines]
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
Ben Voigt - 25 Aug 2006 14:47 GMT
> Hi;
>
[quoted text clipped - 5 lines]
> I was hoping for a third approach - the contents will be written to disk
> soon, but the call returns immediately.

You might be able to do that with an overlapped IOCTL using NtFsControlFile.
But I think that the driver can choose to exhibit synchronous behavior even
for overlapped requests.

>> Hi Dave,
>>
[quoted text clipped - 37 lines]
>> This posting is provided "AS IS" with no warranties, and confers no
>> rights.
"Peter Huang" [MSFT] - 28 Aug 2006 04:22 GMT
Hi Dave,

Yes, you are correct. We have asynchronous version FileStream related
operation, e.g. BeginRead/BeginWrite. But we did NOT have such a BeginFlush
method.
So to make the Flush method(a synchronous method) to be asynchronous, we
have to do the flush in another thread.
The link I posted has a few method to run certain method on another thread.
Here I post the code snippet using asynchronous Delegate to implement an
asynchronous method call)

   class Program
   {
       public delegate bool FlushDelegate();
       public static FileStream fs = null;
       public static bool FlushDelegateFunc()
       {
           Console.WriteLine("Flush ThreadID: " +
Thread.CurrentThread.ManagedThreadId);
           fs.Flush();
           return true;
       }
       static void Main(string[] args)
       {
           fs = new FileStream(@"test.txt", FileMode.OpenOrCreate);
           fs.Write(new byte[] { 1, 2 }, 0, 2);
           FlushDelegate fDelegate = new FlushDelegate(FlushDelegateFunc);
           IAsyncResult ar = fDelegate.BeginInvoke(null, null); //This
call will return immediately.
           Console.WriteLine("Main ThreadID: " +
Thread.CurrentThread.ManagedThreadId);
           Console.WriteLine("Wait for flush thread return");
           fDelegate.EndInvoke(ar);
       }
   }

NOTE: Because here actually is a multiple threading programming, so we need
to guarantee that the object accessed from the two
threads(Main,FlushDelegateFunc) are thread safe.
From MSDN the public static version of FileStream is  thread safe.

If I misunderstood, or  you have any other concern, please feel free to let
me know.

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.