TechTalkz.com Logo Ask the Expert

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Programing Languages > VB.NET

Notices

StreamWriter Problem

VB.NET


Reply
 
Thread Tools Display Modes
Old 08-12-2007, 10:52 PM   #11
kimiraikkonen
Guest
 
Posts: n/a
Re: StreamWriter Problem

On Dec 8, 7:00 pm, Tom Shelton <tom_shel...@comcast.net> wrote:
> On Dec 7, 5:01 pm, kimiraikkonen <kimiraikkone...@gmail.com> wrote:
>
>
>
> > On Dec 8, 1:45 am, Tom Shelton <tom_shel...@comcast.net> wrote:

>
> > > On Dec 7, 4:23 pm, kimiraikkonen <kimiraikkone...@gmail.com> wrote:

>
> > > > Edit: However, i added a catch ex as exception handler and received
> > > > the exception as "the file is being used by another process" although
> > > > nothing is open except my program.

>
> > > > Is it a streamwriter bug? Because nothing is using a text file while
> > > > i'm trying to save through my project.

>
> > > no, it's not a bug in the streamwriter - it's a bug in your code. and
> > > if your code for loading the file is similar to the saving code, then
> > > i can see why. you are never closing the resource. you need to call
> > > close on the streamwriter (and streamreader) when you are done with
> > > them....

>
> > > using sr as new streamreader(myfile)
> > > ' do stuff
> > > end using

>
> > > using the using block will ensure that the streamreader is properly
> > > cleand up when the block ends. Basically, it will compile like you
> > > had done something like:

>
> > > dim sr as streamreader = nothing
> > > try
> > > sr = new streamreader(myfile)
> > > ' do stuff
> > > finally
> > > if not isnothing(sr) then
> > > sr.dispose()
> > > end finally

>
> > > you need to do the same with your streamreader.

>
> > > --
> > > Tom Shelton

>
> > I was using this for streamwriter also of streamreader before i posted
> > with no help:
> > Try
> > .....
> > Catch

>
> > Finally
> > If Not myStreamWriter Is Nothing Then
> > myStreamWriter.Close()
> > End If- Hide quoted text -

>
> > - Show quoted text -

>
> There are only two possibilties here... 1) You aren't closing one of
> your streams properly - that's why I suggest using a using block,
> since it will guarentee they are closed. 2) The file really is locked
> by another process.
>
> I would suggest for one, that you make sure you aren't "swallowing"
> exceptions. Exceptions that are causing you to bypass your close.
> The close in every case should be in a finally block, or you should
> use the stream in a using block.
>
> This is not a bug in the stream classes - there is a bug in your code
> somewhere, or we do not have sufficient information to give you a
> proper answer.
>
> --
> Tom Shelton


Teemu and Tom,
I'm currently using this code with getting this exception rarely but
not solved completely:
myStreamWriter = System.IO.File.CreateText(Save.FileName)
myStreamWriter.Write(TextBox1.Text)
myStreamWriter.Flush()
myStreamWriter.Close()
myStreamWriter.Dispose()

However, i added a yesnocancel msgbox question inside exception area
against the problem, when this exception occurs user can retry re-save
or cancel save process. At least this saves it temporarily but i wish
i didn't have to yesnocancel question prompt to retry save process.
  Reply With Quote
Old 08-12-2007, 10:52 PM   #12
kimiraikkonen
Guest
 
Posts: n/a
Re: StreamWriter Problem

On Dec 8, 7:00 pm, Tom Shelton <tom_shel...@comcast.net> wrote:
> On Dec 7, 5:01 pm, kimiraikkonen <kimiraikkone...@gmail.com> wrote:
>
>
>
> > On Dec 8, 1:45 am, Tom Shelton <tom_shel...@comcast.net> wrote:

>
> > > On Dec 7, 4:23 pm, kimiraikkonen <kimiraikkone...@gmail.com> wrote:

>
> > > > Edit: However, i added a catch ex as exception handler and received
> > > > the exception as "the file is being used by another process" although
> > > > nothing is open except my program.

>
> > > > Is it a streamwriter bug? Because nothing is using a text file while
> > > > i'm trying to save through my project.

>
> > > no, it's not a bug in the streamwriter - it's a bug in your code. and
> > > if your code for loading the file is similar to the saving code, then
> > > i can see why. you are never closing the resource. you need to call
> > > close on the streamwriter (and streamreader) when you are done with
> > > them....

>
> > > using sr as new streamreader(myfile)
> > > ' do stuff
> > > end using

>
> > > using the using block will ensure that the streamreader is properly
> > > cleand up when the block ends. Basically, it will compile like you
> > > had done something like:

>
> > > dim sr as streamreader = nothing
> > > try
> > > sr = new streamreader(myfile)
> > > ' do stuff
> > > finally
> > > if not isnothing(sr) then
> > > sr.dispose()
> > > end finally

>
> > > you need to do the same with your streamreader.

>
> > > --
> > > Tom Shelton

>
> > I was using this for streamwriter also of streamreader before i posted
> > with no help:
> > Try
> > .....
> > Catch

>
> > Finally
> > If Not myStreamWriter Is Nothing Then
> > myStreamWriter.Close()
> > End If- Hide quoted text -

>
> > - Show quoted text -

>
> There are only two possibilties here... 1) You aren't closing one of
> your streams properly - that's why I suggest using a using block,
> since it will guarentee they are closed. 2) The file really is locked
> by another process.
>
> I would suggest for one, that you make sure you aren't "swallowing"
> exceptions. Exceptions that are causing you to bypass your close.
> The close in every case should be in a finally block, or you should
> use the stream in a using block.
>
> This is not a bug in the stream classes - there is a bug in your code
> somewhere, or we do not have sufficient information to give you a
> proper answer.
>
> --
> Tom Shelton



Teemu and Tom,
I'm currently using this code for "try" area with getting this
exception rarely but
not solved completely:
myStreamWriter = System.IO.File.CreateText(Save.FileName)
myStreamWriter.Write(TextBox1.Text)
myStreamWriter.Flush()
myStreamWriter.Close()
myStreamWriter.Dispose()


However, i added a "yesnocancel" msgbox question inside exception area
against the problem, when this exception occurs user can retry save
process
or cancel save process. At least this does it temporarily but i wish
i didn't have to yesnocancel question prompt to retry save process.
  Reply With Quote
Old 08-12-2007, 11:56 PM   #13
Teemu
Guest
 
Posts: n/a
Re: StreamWriter Problem


"kimiraikkonen" <kimiraikkonen85@gmail.com> kirjoitti viestissä
news:6b5e9f3a-f116-47d0-85ac-f5a089f4e445@a35g2000prf.googlegroups.com...

> Teemu and Tom,
> I'm currently using this code for "try" area with getting this
> exception rarely but
> not solved completely:
> myStreamWriter = System.IO.File.CreateText(Save.FileName)
> myStreamWriter.Write(TextBox1.Text)
> myStreamWriter.Flush()
> myStreamWriter.Close()
> myStreamWriter.Dispose()
>
>
> However, i added a "yesnocancel" msgbox question inside exception area
> against the problem, when this exception occurs user can retry save
> process
> or cancel save process. At least this does it temporarily but i wish
> i didn't have to yesnocancel question prompt to retry save process.


Your problem sounds so weird that I can't figure out any reason for this if
the file is really not used by another program.

Maybe you could use a tool like this
http://software.techrepublic.com.com...k&docid=211273
to check out which process has locked the file when the exception occurs.

-Teemu

  Reply With Quote
Old 09-12-2007, 01:00 AM   #14
kimiraikkonen
Guest
 
Posts: n/a
Re: StreamWriter Problem

On Dec 8, 8:35 pm, "Teemu" <tsir...@hotmail.com> wrote:
> "kimiraikkonen" <kimiraikkone...@gmail.com> kirjoitti viestissänews:6b5e9f3a-f116-47d0-85ac-f5a089f4e445@a35g2000prf.googlegroups.com...
>
>
>
> > Teemu and Tom,
> > I'm currently using this code for "try" area with getting this
> > exception rarely but
> > not solved completely:
> > myStreamWriter = System.IO.File.CreateText(Save.FileName)
> > myStreamWriter.Write(TextBox1.Text)
> > myStreamWriter.Flush()
> > myStreamWriter.Close()
> > myStreamWriter.Dispose()

>
> > However, i added a "yesnocancel" msgbox question inside exception area
> > against the problem, when this exception occurs user can retry save
> > process
> > or cancel save process. At least this does it temporarily but i wish
> > i didn't have to yesnocancel question prompt to retry save process.

>
> Your problem sounds so weird that I can't figure out any reason for this if
> the file is really not used by another program.
>
> Maybe you could use a tool like thishttp://software.techrepublic.com.com/download.aspx?&kw=File+lock&doci...
> to check out which process has locked the file when the exception occurs.
>
> -Teemu


Hi,
Thanks for the tool but it did nothing, however this problem happens
very rarely for the latest code that i've edited, but and happened
again. With exception window is stayed open, i used the tool you
recommended, but it didn't report any process that locks my text file,
only says: "You do not have sufficient access to 1 processes". I have
all the rights as i'm admin, and i saw some right-related problem
about that exception, but i couldn't find an exact solution.
  Reply With Quote
Old 09-12-2007, 01:00 AM   #15
kimiraikkonen
Guest
 
Posts: n/a
Re: StreamWriter Problem

On Dec 8, 8:35 pm, "Teemu" <tsir...@hotmail.com> wrote:
> "kimiraikkonen" <kimiraikkone...@gmail.com> kirjoitti viestissänews:6b5e9f3a-f116-47d0-85ac-f5a089f4e445@a35g2000prf.googlegroups.com...
>
>
>
> > Teemu and Tom,
> > I'm currently using this code for "try" area with getting this
> > exception rarely but
> > not solved completely:
> > myStreamWriter = System.IO.File.CreateText(Save.FileName)
> > myStreamWriter.Write(TextBox1.Text)
> > myStreamWriter.Flush()
> > myStreamWriter.Close()
> > myStreamWriter.Dispose()

>
> > However, i added a "yesnocancel" msgbox question inside exception area
> > against the problem, when this exception occurs user can retry save
> > process
> > or cancel save process. At least this does it temporarily but i wish
> > i didn't have to yesnocancel question prompt to retry save process.

>
> Your problem sounds so weird that I can't figure out any reason for this if
> the file is really not used by another program.
>
> Maybe you could use a tool like thishttp://software.techrepublic.com.com/download.aspx?&kw=File+lock&doci...
> to check out which process has locked the file when the exception occurs.
>
> -Teemu


Hi,
Thanks for the tool but it did nothing, however this problem happens
very rarely for the latest code that i've edited, but and happened
again. With exception window is stayed open, i used the tool you
recommended, but it didn't report any process that locks my text file,
only says: "You do not have sufficient access to 1 processes". I have
all the rights as i'm admin, and i saw some right-related problem
about that exception, but i couldn't find an exact solution.

Additionaly, to test the tool which you pointed out, while an mp3
audio file is being played by a player, i used that tool to report if
it reports correct process(player) as it's used by, but nothing is
reported. This tool is not reliable.
  Reply With Quote
Old 09-12-2007, 02:20 AM   #16
Teemu
Guest
 
Posts: n/a
Re: StreamWriter Problem


"kimiraikkonen" <kimiraikkonen85@gmail.com> kirjoitti viestissä
news:be3c7297-ef81-47cb-ab84-1c164354b93e@y43g2000hsy.googlegroups.com...

> Additionaly, to test the tool which you pointed out, while an mp3
> audio file is being played by a player, i used that tool to report if
> it reports correct process(player) as it's used by, but nothing is
> reported. This tool is not reliable.


OK. I didn't try that tool myself so I don't know if it works or not. Maybe
you find another tool similar to this which works better. That kind of tool
might be the only way to find out what happens.

Is there any difference between running with debugger or only the
executable? Could it be possible that debugger won't release the file
sometimes and keeps it locked? Or does it matter how often you try to save
the file?

-Teemu

  Reply With Quote
Old 09-12-2007, 02:21 AM   #17
tom_shelton@comcast.net
Guest
 
Posts: n/a
Re: StreamWriter Problem

kimiraikkonen wrote:

> On Dec 8, 7:00 pm, Tom Shelton <tom_shel...@comcast.net> wrote:
>> On Dec 7, 5:01 pm, kimiraikkonen <kimiraikkone...@gmail.com> wrote:
>>
>>
>>
>> > On Dec 8, 1:45 am, Tom Shelton <tom_shel...@comcast.net> wrote:

>>
>> > > On Dec 7, 4:23 pm, kimiraikkonen <kimiraikkone...@gmail.com> wrote:

>>
>> > > > Edit: However, i added a catch ex as exception handler and received
>> > > > the exception as "the file is being used by another process"
>> > > > although nothing is open except my program.

>>
>> > > > Is it a streamwriter bug? Because nothing is using a text file
>> > > > while i'm trying to save through my project.

>>
>> > > no, it's not a bug in the streamwriter - it's a bug in your code.
>> > > and if your code for loading the file is similar to the saving code,
>> > > then
>> > > i can see why. you are never closing the resource. you need to call
>> > > close on the streamwriter (and streamreader) when you are done with
>> > > them....

>>
>> > > using sr as new streamreader(myfile)
>> > > ' do stuff
>> > > end using

>>
>> > > using the using block will ensure that the streamreader is properly
>> > > cleand up when the block ends. Basically, it will compile like you
>> > > had done something like:

>>
>> > > dim sr as streamreader = nothing
>> > > try
>> > > sr = new streamreader(myfile)
>> > > ' do stuff
>> > > finally
>> > > if not isnothing(sr) then
>> > > sr.dispose()
>> > > end finally

>>
>> > > you need to do the same with your streamreader.

>>
>> > > --
>> > > Tom Shelton

>>
>> > I was using this for streamwriter also of streamreader before i posted
>> > with no help:
>> > Try
>> > .....
>> > Catch

>>
>> > Finally
>> > If Not myStreamWriter Is Nothing Then
>> > myStreamWriter.Close()
>> > End If- Hide quoted text -

>>
>> > - Show quoted text -

>>
>> There are only two possibilties here... 1) You aren't closing one of
>> your streams properly - that's why I suggest using a using block,
>> since it will guarentee they are closed. 2) The file really is locked
>> by another process.
>>
>> I would suggest for one, that you make sure you aren't "swallowing"
>> exceptions. Exceptions that are causing you to bypass your close.
>> The close in every case should be in a finally block, or you should
>> use the stream in a using block.
>>
>> This is not a bug in the stream classes - there is a bug in your code
>> somewhere, or we do not have sufficient information to give you a
>> proper answer.
>>
>> --
>> Tom Shelton

>
>
> Teemu and Tom,
> I'm currently using this code for "try" area with getting this
> exception rarely but
> not solved completely:
> myStreamWriter = System.IO.File.CreateText(Save.FileName)
> myStreamWriter.Write(TextBox1.Text)
> myStreamWriter.Flush()
> myStreamWriter.Close()
> myStreamWriter.Dispose()
>


This is all you need - your working to hard.
Try

Using myStreamWriter As StreamWriter = File.CreateText (Save.FileName)
myStreamWriter.Write (TextBox1.Text)
myStreamWriter.Flush()
End Using

Catch ex As Exception
MessageBox.Show (ex.Message)
End Try


Put all of your reader's writers in a using block. It will call dispose
(which will close the file), even if you get an exception in the block.

There is no threading going on in this app?

--
Tom Shelton
  Reply With Quote
Old 09-12-2007, 02:21 AM   #18
Jack Jackson
Guest
 
Posts: n/a
Re: StreamWriter Problem

Do you realize all of your posts to this thread are posted twice?
  Reply With Quote
Old 09-12-2007, 02:22 AM   #19
tom_shelton@comcast.net
Guest
 
Posts: n/a
Re: StreamWriter Problem

Jack Jackson wrote:

> Do you realize all of your posts to this thread are posted twice?


I was going to point that out myself, but I decided not to bother

  Reply With Quote
Old 09-12-2007, 03:05 AM   #20
kimiraikkonen
Guest
 
Posts: n/a
Re: StreamWriter Problem

On Dec 8, 11:25 pm, Jack Jackson <jacknos...@pebbleridge.com> wrote:
> Do you realize all of your posts to this thread are posted twice?


If you're asking my posts, none of them was sent twice, but if you see
total message number higher than actual messages before clicking on
thread, that's because i needed to remove and re-post the message due
to need of modiying, not message duplication.
  Reply With Quote
Reply

Thread Tools
Display Modes



< Home - Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT +5.5. The time now is 06:11 PM.


vBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2010, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional