12 Replies - 1007 Views - Last Post: 14 January 2010 - 02:28 PM Rate Topic: -----

#1 zukeru  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 132
  • Joined: 25-December 09

How Do you set code to do on program close

Posted 14 January 2010 - 09:41 AM

I don't know if this is possible i want my program to execute certain code before it will close even when the x is pressed is that possible. Becuase its leavign streamreaders open.

timer1

open sreamreader etc

so the timer is set to 100ms to constantly update the data waht happens is its leaving it open when someone closes it during its cycle.
Is This A Good Question/Topic? 0
  • +

Replies To: How Do you set code to do on program close

#2 zukeru  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 132
  • Joined: 25-December 09

Re: How Do you set code to do on program close

Posted 14 January 2010 - 09:47 AM

and is it possible to force vb to take control of a file and open and write to it even if another program has it opne
Was This Post Helpful? 0
  • +
  • -

#3 woodjom  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 29
  • View blog
  • Posts: 549
  • Joined: 08-May 08

Re: How Do you set code to do on program close

Posted 14 January 2010 - 09:47 AM

Form.OnExit?
Was This Post Helpful? 0
  • +
  • -

#4 woodjom  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 29
  • View blog
  • Posts: 549
  • Joined: 08-May 08

Re: How Do you set code to do on program close

Posted 14 January 2010 - 09:57 AM

Sorry bad humor on thursday

Here is a Form's Lifecycle knowledge read for you as well:
c-sharpconer (might have to translate to VB)<br/>
Order of Events - Windows Forms

Here are 4 possible events you might want to look at:
Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed

	End Sub

	Protected Overrides Sub Finalize()
		MyBase.Finalize()
	End Sub

	Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed

	End Sub

	Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

	End Sub


Was This Post Helpful? 1
  • +
  • -

#5 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: How Do you set code to do on program close

Posted 14 January 2010 - 10:29 AM

No it is not possible for VB to 'force take controll of a file' if a program is using it (unless you know the executable name of the other program, in which case you can just use Process.Kill)

This post has been edited by Jack Eagles1: 14 January 2010 - 10:31 AM

Was This Post Helpful? 0
  • +
  • -

#6 woodjom  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 29
  • View blog
  • Posts: 549
  • Joined: 08-May 08

Re: How Do you set code to do on program close

Posted 14 January 2010 - 10:31 AM

Jack is right, i would suggest against it as the user of the program may not like what happens. If it is a network file and someone else on the network has it open, your SOL for closing the program that has it opened, without being a File Server admin that is ;p~

This post has been edited by woodjom: 14 January 2010 - 10:32 AM

Was This Post Helpful? 0
  • +
  • -

#7 Luc001  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 617
  • Joined: 04-May 09

Re: How Do you set code to do on program close

Posted 14 January 2010 - 10:38 AM

Hi,

You can try something like this:

If MessageBox.Show("Are You Sure?", "Quit Application", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then

			e.Cancel = True
		Else
			' Close the streamreader first or stop the timer
			e.Cancel = False
		End If


Was This Post Helpful? 0
  • +
  • -

#8 crepitus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 84
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: How Do you set code to do on program close

Posted 14 January 2010 - 11:36 AM

StreamReader wraps a Stream, in you case it is a file stream and the stream is keeping a native file handle open.

When you use a StreamReader you should ensure that you call the Dispose (or Close method) when you have finished with it. If you call StreamReader1.Close, internally it will call its own Dispose method, the Dispose method will call the Close method of the Stream that it wraps, which will call the Stream.Dispose method which will deal with the file handle:

StreamReader.Close calls StreamReader.Dispose calls Stream.Close calls Stream.Dispose which tidies up the unmanaged resource...

If you do nothing special, then quitting the program will leave the file handle open, and the file will be unusable for a while (until windows deals with the leaky handle).

To ensure that the Dispose method of the StreamReader is called, you can wrap its use inside a Try ... Finally block:

Imports System.IO

Public Class Form1

	Sub DoSomething()
		Dim sr As StreamReader = Nothing
		Dim allText As String
		Try
			sr = New StreamReader("C:\somthing.blah")
			' use it here...
			allText = sr.ReadToEnd()
		Finally
			' close it here
			If sr IsNot Nothing Then sr.Close()
		End Try

	End Sub

End Class


But, it's neater to use a Using block, which does the same thing - it will call dispose as execution passes out of the block:

Imports System.IO

Public Class Form1

	Sub DoSomething()
		Dim allText As String
		Using sr As New StreamReader("C:\something.blah")
			' use it here...
			allText = sr.ReadToEnd()
		End Using
	End Sub

End Class


If you have a StreamReader declared at the class level, then it gets more complicated - you can't wrap it in a using, or try .. finally block. Instead you have to ensure that the class that holds the StreamReader implements IDisposable, and then close the StreamReader in the classes Dispose method. If you have a Form holding the StreamReader then it already implements IDisposable.Dispose, but it is in the hidden auto-generated file. You can safely add code to this method : in the solution explorer click show all files and open up the FormX.Designer.vb file and add code into the Dispose method. It's an ugly way to do things though. (Ideally you would be using a proper design such as MVP or MVVM so that the Form was very lightweight and didn't have any fancy fields that needed to be disposed - they would be in a different class.)
Was This Post Helpful? 0
  • +
  • -

#9 zukeru  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 132
  • Joined: 25-December 09

Re: How Do you set code to do on program close

Posted 14 January 2010 - 11:41 AM

I always close it however if they close the process while vb is executing the timer code before it gets to the .close portion it leaves the file open.
public greenalt as string = "C:/whatever.txt"
Public SUb Timer1_tick()

dim oread as streamreader
dim contents as string
oread = new streamreader(greenalt)
contents = oread.readline
label1.text = contents
oread.close

end sub



Was This Post Helpful? 0
  • +
  • -

#10 woodjom  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 29
  • View blog
  • Posts: 549
  • Joined: 08-May 08

Re: How Do you set code to do on program close

Posted 14 January 2010 - 11:48 AM

View Postzukeru, on 14 Jan, 2010 - 12:41 PM, said:

I always close it however if they close the process while vb is executing the timer code before it gets to the .close portion it leaves the file open.
public greenalt as string = "C:/whatever.txt"
Public SUb Timer1_tick()

dim oread as streamreader
dim contents as string
oread = new streamreader(greenalt)
contents = oread.readline
label1.text = contents
oread.close

end sub




You could always SyncLock the reader, so the program doesnt close until the reader is done.

Again, you need to short-circuit the close operation so that way if there are any processes open, you get the time to either kill the process, wait for the process to end, or give the user the option to either kill the process or wait for the process to end.

You may want to make a private variable to your top-level form (incase you are using a parent-child form view), and have it hold the filestreamreader object, then handle the disposing/closing of the reader at form dispose.

Cant remember if there is an Application event that fires for the whole application outside of the form.
Was This Post Helpful? 0
  • +
  • -

#11 zukeru  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 132
  • Joined: 25-December 09

Re: How Do you set code to do on program close

Posted 14 January 2010 - 01:05 PM

guess i could have explained it a little btter, I have miltiple applications reading and writing to the code, I have my tcp repeater which connects to a server grabs 9 streams and then writes it to a binary file with my own extensions on a network drive. Then the clients read that text file into themselves. We have 18 clients so orginally i had 18 clients making 9 connections simutaniously however the server couldnt handle that so i made the repeater. my program deconflicts aircraft.

right now i solved it by closing out each reader and writer on from close like such

 Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

msgbox("You Are Now Closing Airspace-X")
reader1.close
writer1.close


	End Sub



and so on and so fourth we will see if that works.
Was This Post Helpful? 0
  • +
  • -

#12 woodjom  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 29
  • View blog
  • Posts: 549
  • Joined: 08-May 08

Re: How Do you set code to do on program close

Posted 14 January 2010 - 01:15 PM

View Postzukeru, on 14 Jan, 2010 - 02:05 PM, said:

guess i could have explained it a little btter, I have miltiple applications reading and writing to the code, I have my tcp repeater which connects to a server grabs 9 streams and then writes it to a binary file with my own extensions on a network drive. Then the clients read that text file into themselves. We have 18 clients so orginally i had 18 clients making 9 connections simutaniously however the server couldnt handle that so i made the repeater. my program deconflicts aircraft.

right now i solved it by closing out each reader and writer on from close like such

 Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

msgbox("You Are Now Closing Airspace-X")
reader1.close
writer1.close


	End Sub



Have you looked into doing multi-document writing? As such, you write a document named "client1.txt" and then have a service run that takes all the txt files and combines them into 1 file for reading?

Also, if i remember right, the filereader threads have the option to lock the file or not. If all you are doing is reading the file, then you dont need to lock it. If you are reading the file and then writing to it from the same client, then you will need to lock it.

It would probably be best as well to have a Master client that has locking priveleges to the files and can boot the other clients from the file if it needs it. But this would require a very sophicated algorithm to for multi-threading, as you will need to have the child-clients communicate to the master-client that they are going to read from this file, so that the master-client will know what each of the child-clients are doing and can interrupt the other children from using that file as longas one of the children has it open.

As well, you could also have the read-only files copied to the computer that needs it for reading, on a cyclical basis, and then have that client append the updated data to the writing-document.

There are alot of options available to you since you are using a Flat-File database information system. Not wise but for TCP and pending on what you are doing, maybe the best solution you have available.

and so on and so fourth we will see if that works.

Was This Post Helpful? 0
  • +
  • -

#13 zukeru  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 132
  • Joined: 25-December 09

Re: How Do you set code to do on program close

Posted 14 January 2010 - 02:28 PM

well i have a repeater how do i lock the files so that only the repeater can write to them no matter what and the viewing clients just say ooo look i see you
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1