9 Replies - 609 Views - Last Post: 11 May 2013 - 03:58 PM Rate Topic: -----

#1 amholmes222  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-May 13

Error Closing streamwriter file

Posted 11 May 2013 - 02:47 PM

I need to check if the streamwriter file is open and if so close it before opening a streamreader of the same file name to calculate ending totals. I am getting an exception at the [swPedroPizza.Close()] command in my form closing event telling me to "use the 'new' keyword to create an object instance" . I have gotten past that once only to get to an exception at the streamreader open command telling me the file is in use already. I have tried all options I can come up with.

Here is some of my form closing code

[If MessageBox.Show("Are you sure you want to Exit?", "Confirm Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) [= DialogResult.Yes Then
If Which = "Write" Then
swPedroPizza.Close()
Exit Sub
End If
' Open file as a streamreader
strFileName = ("pp-" & intYear.ToString & "-" & intMonth.ToString & "-" & intDay.ToString & ".txn")
srPedroPizza = File.OpenText(strFileName)]

My exception now comes at the last line. This is some of the error; The process cannot access the file 'S:\Debug\pp-2013-5-11.txn' because it is being used by another process.

I have a feeling it is something easy, but I can't seem to figure it out myself. Any advice or recommendations would be greatly appreciated.

Thanks in advance,

AH

Is This A Good Question/Topic? 0
  • +

Replies To: Error Closing streamwriter file

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3615
  • View blog
  • Posts: 12,442
  • Joined: 12-December 12

Re: Error Closing streamwriter file

Posted 11 May 2013 - 02:58 PM

You are only closing the stream if the user chooses to exit the application. Close the stream as soon as it is not needed. Better yet, investigate the Using Block so that a resource will be automatically disposed of when the block finishes.

This post has been edited by andrewsw: 11 May 2013 - 03:00 PM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3615
  • View blog
  • Posts: 12,442
  • Joined: 12-December 12

Re: Error Closing streamwriter file

Posted 11 May 2013 - 03:04 PM

Mm.. your code appears, from your description, to come from two separate areas. Nevertheless, it is sensible to close the stream as soon as it is no longer needed.

I only mention for completeness that it is possible to check if a stream is open:

If sr.BaseStream.CanRead Then

but it is recommended that you just close the stream when not needed.

This post has been edited by andrewsw: 11 May 2013 - 03:09 PM

Was This Post Helpful? 0
  • +
  • -

#4 amholmes222  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-May 13

Re: Error Closing streamwriter file

Posted 11 May 2013 - 03:08 PM

The streamwriter is opened with a start button, but is needed to stay open for input from up to two different forms linked to the main form by the menu strip. This is where I seem to be having an issue. I don't know where to put the .close() to properly close the streamwrite and allow the streamreader to be opened.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3615
  • View blog
  • Posts: 12,442
  • Joined: 12-December 12

Re: Error Closing streamwriter file

Posted 11 May 2013 - 03:13 PM

I would rethink your design. You could, for example, collect text in a StringBuilder, or even a primitive string, and only open and write to the file on a specific action.

In particular, having 3 forms which can write to a file is a poor recipe.

Quote

I don't know where to put the .close() to properly close the streamwrite and allow the streamreader to be opened.

This only serves to illustration the problems you are constructing for yourself.

This post has been edited by andrewsw: 11 May 2013 - 03:15 PM

Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3615
  • View blog
  • Posts: 12,442
  • Joined: 12-December 12

Re: Error Closing streamwriter file

Posted 11 May 2013 - 03:21 PM

Sorry if I'm sounding a bit dogmatic. You are, of course, free to ignore my advice.

MSDN: StringBuilder

This post has been edited by andrewsw: 11 May 2013 - 03:23 PM

Was This Post Helpful? 0
  • +
  • -

#7 amholmes222  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-May 13

Re: Error Closing streamwriter file

Posted 11 May 2013 - 03:34 PM

No, you are fine. I am a beginner and I do agree about the multiple forms comment, however it is my final project for the semester and this is the first one I have struggled with. As for the multi form design, it was built over the last two projects and it is the way the instructor wants it so my hands are kind of tied. I have to make it work somehow. I thank you for the comments so far. I am exploring options now, but am running out of time since this is due by midnight tomorrow night.
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3615
  • View blog
  • Posts: 12,442
  • Joined: 12-December 12

Re: Error Closing streamwriter file

Posted 11 May 2013 - 03:43 PM

Well, a quick and dirty solution is to store a Boolean value in a Module that indicates whether a stream is open or closed. Not ideal :dontgetit:

Slightly better would be to store it as a property of the main form.

This post has been edited by andrewsw: 11 May 2013 - 03:45 PM

Was This Post Helpful? 0
  • +
  • -

#9 amholmes222  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-May 13

Re: Error Closing streamwriter file

Posted 11 May 2013 - 03:50 PM

I have tried a boolean in the module and in the form itself and I have also tried to use a Which and have had mixed error results. I have tried closing the stream in the form closings of all three forms which can access it as well as in the exit or close buttons on each of those same forms with again mixed error results. I have something going on even more now because I got past it far enough just now to try and write to the file and had an exception error there as well. Maybe I need to give up on this one and hope what I do have written will be good enough.
Was This Post Helpful? 0
  • +
  • -

#10 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3615
  • View blog
  • Posts: 12,442
  • Joined: 12-December 12

Re: Error Closing streamwriter file

Posted 11 May 2013 - 03:58 PM

Erm, tried error handling?

Good luck!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1