Re: is there any way to clear the buffer of a System.IO.StreamWriter so that it does not do a flush when it is closed in the finaly block?
- From: "William Stacey [MVP]" <staceyw@xxxxxxxx>
- Date: Wed, 28 Sep 2005 21:09:50 -0400
I have not tested this for network failure, but something like this might
work. This will try three times to write the data and then throw exception.
Even if you get an exception in Flush, it should try again. You may to add
a Thread.Sleep(500) or something in the while loop after exception to allow
network stuff to settle down, before you try again in a tight loop. The
normal path will just write the data and return.
public void DoWrite()
{
string path = @"\\8.3.35.139\netdistest\mb30file.txt";
// Create request.
System.Text.StringBuilder sb = new
System.Text.StringBuilder(1000);
for ( int i = 0; i < 100; i++ )
{
sb.Append("0");
}
string request = sb.ToString();
int failCount = 0;
while ( true )
{
try
{
WriteFile(path, request);
return;
}
catch ( Exception ex )
{
failCount++;
if ( failCount >= 2 ) // Try 3 times to write the
data, then give up.
throw ex;
}
}
}
private void WriteFile(string path, string data)
{
using ( StreamWriter sw = new StreamWriter(path) )
{
sw.Write(data);
}
}
--
William Stacey [MVP]
"Daniel" <softwareengineer98037@xxxxxxxxx> wrote in message
news:%23qJkfVHxFHA.3312@xxxxxxxxxxxxxxxxxxxxxxx
> when i disconnect the network drive after the file is open an exception is
> thrown but i can not close the file because the close will try to flush
> and
> close it self will throws "The specified network name is no longer
> available." then if i renable the network drive on the target machien and
> let it try to send the file again it throws "{"The process cannot access
> the
> file "\\11.11.11.11\netdistest\mb30file.txt" because it is being used by
> another process." }" i dont konw how to free the file up without
> restarting
> my .net app seems to fix that but i want to not have to restart my app
> every
> time one of my thousands of target computers has a momentary netowork
> failiure. using the using() clause has the same issue because it tries to
> close the file and that it self throws the error because close does a
> flush:
>
> public void tProc()
>
> {
>
> string strTempPath = @"\\8.3.35.139\netdistest\mb30file.txt";
>
> System.Text.StringBuilder sb = new System.Text.StringBuilder(1000);
>
> for(int i=0;i<100;i++)
>
> {
>
> sb.Append("0");
>
> }
>
> string request = sb.ToString();
>
> System.IntPtr lasthandle = new System.IntPtr(-1);
>
> while(true)
>
> {
>
> int i2dd3 = 23 + 23;
>
> System.IO.TextWriter streamWriter = null;
>
> try
>
> {
>
> streamWriter = new System.IO.StreamWriter(strTempPath);
>
> streamWriter.Write(request);
>
> }
>
> catch(Exception eed3)
>
> {
>
> int i22322 = 23 + 23;
>
> }
>
> finally
>
> {
>
> try
>
> {
>
> if(streamWriter != null)
>
> {
>
> streamWriter.Close();
>
> }
>
> }
>
> catch(Exception ee3)
>
> {
>
> int i23243 = 23 + 23;
>
> }
>
> }
>
> int i23 = 23 + 23;
>
> }
>
> }
>
>
> "William Stacey [MVP]" <staceyw@xxxxxxxx> wrote in message
> news:eiqw0JHxFHA.1028@xxxxxxxxxxxxxxxxxxxxxxx
>> Dispose calls Flush, so don't think there is a way to do that. However,
>> there is probably a way to refactor your code to get the behavior you
>> desire. What are you trying to do?
>>
>> --
>> William Stacey [MVP]
>>
>> "Daniel" <softwareengineer98037@xxxxxxxxx> wrote in message
>> news:%23e$Ij6GxFHA.612@xxxxxxxxxxxxxxxxxxxxxxx
>> > is there any way to clear the buffer of a System.IO.StreamWriter so
>> > that
>> > it
>> > does not do a flush when it is closed in the finaly block?
>> >
>> >
>>
>>
>
>
.
- Follow-Ups:
- References:
- Re: is there any way to clear the buffer of a System.IO.StreamWriter so that it does not do a flush when it is closed in the finaly block?
- From: William Stacey [MVP]
- Re: is there any way to clear the buffer of a System.IO.StreamWriter so that it does not do a flush when it is closed in the finaly block?
- From: Daniel
- Re: is there any way to clear the buffer of a System.IO.StreamWriter so that it does not do a flush when it is closed in the finaly block?
- Prev by Date: Re: .NET framework and Terminal Server
- Next by Date: RE: Antivirus Software
- Previous by thread: Re: is there any way to clear the buffer of a System.IO.StreamWriter so that it does not do a flush when it is closed in the finaly block?
- Next by thread: Re: is there any way to clear the buffer of a System.IO.StreamWriter so that it does not do a flush when it is closed in the finaly block?
- Index(es):
Relevant Pages
|