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?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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?
>> >
>> >
>>
>>
>
>


.



Relevant Pages

  • Re: URL exception settings
    ... GUID of the "Work" network. ... "Yaroslav Goncharov" wrote: ... I try to setup an URL exception programmatically. ... I want to be able to connect to internet from my PPC. ...
    (microsoft.public.pocketpc.developer.networking)
  • Re: Meaning of exception 0xC0000006 (EXCEPTION_IN_PAGE_ERROR)
    ... was run from a network share that was later disconnected. ... share then disconnecting it and forcing the system to page ... First chance exceptions are reported before any exception handling. ... which reports no bad memory accesses - for ...
    (microsoft.public.vc.debugger)
  • Re: is there any way to clear the buffer of a System.IO.StreamWriter so that it does not do a flush
    ... > Even if you get an exception in Flush, ... > allow network stuff to settle down, before you try again in a tight loop. ... >> my .net app seems to fix that but i want to not have to restart my app ...
    (microsoft.public.dotnet.framework)
  • O2 Installation Problems?
    ... Im trying to install my r5k O2 via CDROM or Network. ... Network is set up and should be ok, also the Installation CD dont has errors. ... Exception PC: 0x8103b48c, Exception RA: 0x8103b76c ... controller display SGI-CRM, Rev C key 0 ...
    (comp.sys.sgi.misc)
  • Re: Odd IOException using StreamWriter
    ... the exception was generated when Closewas called. ... I added a Flush() before the Close, ... If I set AutoFlush to true, ... inheriting StreamWriter. ...
    (microsoft.public.dotnet.framework.compactframework)