469,580 Members | 1,888 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,580 developers. It's quick & easy.

ImageList Holding Reference

In the code below I receive an exception error because the target file is being used by another process. I know this to be false because the target doesn't exist. It seems like the application believes that the file is still in use by the application. My concern is that I do not think that I still have a reference to it. In the removeImage function, I remove the item from the image list and list view controls and refresh the list view control to ensure that it is no longer even on the screen. No joy.

Thanks in advance for any help, also if I have questions like this do i need to include the entire project or just the area of confusion?
Imports System.IO

Public Class Form1
Inherits System.Windows.Forms.Form
Dim mData As ArrayList

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents ImageList1 As System.Windows.Forms.ImageList
Friend WithEvents ListView1 As System.Windows.Forms.ListView
Friend WithEvents Button2 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.Button1 = New System.Windows.Forms.Button
Me.ImageList1 = New System.Windows.Forms.ImageList(Me.components)
Me.ListView1 = New System.Windows.Forms.ListView
Me.Button2 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(224, 24)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(64, 48)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'ImageList1
'
Me.ImageList1.ImageSize = New System.Drawing.Size(254, 130)
Me.ImageList1.TransparentColor = System.Drawing.Color.Transparent
'
'ListView1
'
Me.ListView1.LargeImageList = Me.ImageList1
Me.ListView1.Location = New System.Drawing.Point(8, 16)
Me.ListView1.Name = "ListView1"
Me.ListView1.Size = New System.Drawing.Size(208, 216)
Me.ListView1.SmallImageList = Me.ImageList1
Me.ListView1.TabIndex = 1
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(224, 80)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(64, 48)
Me.Button2.TabIndex = 2
Me.Button2.Text = "Button2"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.ListView1)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim dirInfo As New DirectoryInfo("c:\bcrcc\images")
Dim tmpDir As DirectoryInfo
Dim fTemp() As FileInfo
Dim sGoodDirs As ArrayList
Dim excep As Exception
Dim bDone As Boolean = False

For Each tmpDir In dirInfo.GetDirectories()
fTemp = tmpDir.GetFiles("*.jpg")
If fTemp.GetUpperBound(0) > 0 Then
RetrieveData(tmpDir.FullName, excep)
bDone = True
Exit For
End If
Next
End Sub

Private Sub RetrieveData(ByVal sDirectory As String, ByVal e As System.Exception)
Dim dir As Directory
Dim ddata() As String = dir.GetFiles(sDirectory, "*.jpg")
Dim iTemp As Integer
Dim iItem(ddata.GetUpperBound(0)) As ListViewItem
Dim sFile As String
Dim iLocation As Integer = 0
ImageList1.Images.Clear()
ListView1.Items.Clear()
mData = New ArrayList

For Each sFile In ddata
'ReDim Preserve mData(iLocation)
mData.Add(sFile)
ImageList1.Images.Add(System.Drawing.Image.FromFil e(sFile))
iItem(iLocation) = New ListViewItem(sFile, iLocation)
ListView1.Items.Add(iItem(iLocation))
iLocation = iLocation + 1
Next
End Sub

Private Sub removeImage(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ImageList1.Images.RemoveAt(0)
ListView1.Items.RemoveAt(0)
ListView1.Refresh()
Dim ffile As File
Application.DoEvents()

ffile.Move(mData(0).ToString, mData(0).ToString & ".tmp")
mData.RemoveAt(0)
End Sub
End Class
Nov 20 '05 #1
6 1274
nope. nor do I get it (in Explorer, at least) when I stop the application
"One Handed Man [ OHM ]" <te***************************@BTOpenworld.com> wrote in message news:Oz*************@TK2MSFTNGP11.phx.gbl...

If you were to turn your machine off and reboot it. Do you get the same error message ?

Best Regards - OHM

On**********@BTInternet.Com




"Stephen Costanzo" <sx********@hotmail.com> wrote in message news:O5**************@tk2msftngp13.phx.gbl...
In the code below I receive an exception error because the target file is being used by another process. I know this to be false because the target doesn't exist. It seems like the application believes that the file is still in use by the application. My concern is that I do not think that I still have a reference to it. In the removeImage function, I remove the item from the image list and list view controls and refresh the list view control to ensure that it is no longer even on the screen. No joy.

Thanks in advance for any help, also if I have questions like this do i need to include the entire project or just the area of confusion?
Imports System.IO

Public Class Form1
Inherits System.Windows.Forms.Form
Dim mData As ArrayList

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents ImageList1 As System.Windows.Forms.ImageList
Friend WithEvents ListView1 As System.Windows.Forms.ListView
Friend WithEvents Button2 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.Button1 = New System.Windows.Forms.Button
Me.ImageList1 = New System.Windows.Forms.ImageList(Me.components)
Me.ListView1 = New System.Windows.Forms.ListView
Me.Button2 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(224, 24)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(64, 48)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'ImageList1
'
Me.ImageList1.ImageSize = New System.Drawing.Size(254, 130)
Me.ImageList1.TransparentColor = System.Drawing.Color.Transparent
'
'ListView1
'
Me.ListView1.LargeImageList = Me.ImageList1
Me.ListView1.Location = New System.Drawing.Point(8, 16)
Me.ListView1.Name = "ListView1"
Me.ListView1.Size = New System.Drawing.Size(208, 216)
Me.ListView1.SmallImageList = Me.ImageList1
Me.ListView1.TabIndex = 1
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(224, 80)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(64, 48)
Me.Button2.TabIndex = 2
Me.Button2.Text = "Button2"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.ListView1)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim dirInfo As New DirectoryInfo("c:\bcrcc\images")
Dim tmpDir As DirectoryInfo
Dim fTemp() As FileInfo
Dim sGoodDirs As ArrayList
Dim excep As Exception
Dim bDone As Boolean = False

For Each tmpDir In dirInfo.GetDirectories()
fTemp = tmpDir.GetFiles("*.jpg")
If fTemp.GetUpperBound(0) > 0 Then
RetrieveData(tmpDir.FullName, excep)
bDone = True
Exit For
End If
Next
End Sub

Private Sub RetrieveData(ByVal sDirectory As String, ByVal e As System.Exception)
Dim dir As Directory
Dim ddata() As String = dir.GetFiles(sDirectory, "*.jpg")
Dim iTemp As Integer
Dim iItem(ddata.GetUpperBound(0)) As ListViewItem
Dim sFile As String
Dim iLocation As Integer = 0
ImageList1.Images.Clear()
ListView1.Items.Clear()
mData = New ArrayList

For Each sFile In ddata
'ReDim Preserve mData(iLocation)
mData.Add(sFile)
ImageList1.Images.Add(System.Drawing.Image.FromFil e(sFile))
iItem(iLocation) = New ListViewItem(sFile, iLocation)
ListView1.Items.Add(iItem(iLocation))
iLocation = iLocation + 1
Next
End Sub

Private Sub removeImage(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ImageList1.Images.RemoveAt(0)
ListView1.Items.RemoveAt(0)
ListView1.Refresh()
Dim ffile As File
Application.DoEvents()

ffile.Move(mData(0).ToString, mData(0).ToString & ".tmp")
mData.RemoveAt(0)
End Sub
End Class
Nov 20 '05 #2
Stephen,
I have tried to run your code, but i get all sorts of errors on my machine. Like running out of memory, etc etc etc. I'll loook again tommorrow and if you have not fixed it, I'll take another stab
Best Regards - OHM

On**********@BTInternet.Com




"Stephen Costanzo" <sx********@hotmail.com> wrote in message news:eJ**************@TK2MSFTNGP12.phx.gbl...
nope. nor do I get it (in Explorer, at least) when I stop the application
"One Handed Man [ OHM ]" <te***************************@BTOpenworld.com> wrote in message news:Oz*************@TK2MSFTNGP11.phx.gbl...

If you were to turn your machine off and reboot it. Do you get the same error message ?

Best Regards - OHM

On**********@BTInternet.Com




"Stephen Costanzo" <sx********@hotmail.com> wrote in message news:O5**************@tk2msftngp13.phx.gbl...
In the code below I receive an exception error because the target file is being used by another process. I know this to be false because the target doesn't exist. It seems like the application believes that the file is still in use by the application. My concern is that I do not think that I still have a reference to it. In the removeImage function, I remove the item from the image list and list view controls and refresh the list view control to ensure that it is no longer even on the screen. No joy.

Thanks in advance for any help, also if I have questions like this do i need to include the entire project or just the area of confusion?
Imports System.IO

Public Class Form1
Inherits System.Windows.Forms.Form
Dim mData As ArrayList

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents ImageList1 As System.Windows.Forms.ImageList
Friend WithEvents ListView1 As System.Windows.Forms.ListView
Friend WithEvents Button2 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.Button1 = New System.Windows.Forms.Button
Me.ImageList1 = New System.Windows.Forms.ImageList(Me.components)
Me.ListView1 = New System.Windows.Forms.ListView
Me.Button2 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(224, 24)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(64, 48)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'ImageList1
'
Me.ImageList1.ImageSize = New System.Drawing.Size(254, 130)
Me.ImageList1.TransparentColor = System.Drawing.Color.Transparent
'
'ListView1
'
Me.ListView1.LargeImageList = Me.ImageList1
Me.ListView1.Location = New System.Drawing.Point(8, 16)
Me.ListView1.Name = "ListView1"
Me.ListView1.Size = New System.Drawing.Size(208, 216)
Me.ListView1.SmallImageList = Me.ImageList1
Me.ListView1.TabIndex = 1
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(224, 80)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(64, 48)
Me.Button2.TabIndex = 2
Me.Button2.Text = "Button2"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.ListView1)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim dirInfo As New DirectoryInfo("c:\bcrcc\images")
Dim tmpDir As DirectoryInfo
Dim fTemp() As FileInfo
Dim sGoodDirs As ArrayList
Dim excep As Exception
Dim bDone As Boolean = False

For Each tmpDir In dirInfo.GetDirectories()
fTemp = tmpDir.GetFiles("*.jpg")
If fTemp.GetUpperBound(0) > 0 Then
RetrieveData(tmpDir.FullName, excep)
bDone = True
Exit For
End If
Next
End Sub

Private Sub RetrieveData(ByVal sDirectory As String, ByVal e As System.Exception)
Dim dir As Directory
Dim ddata() As String = dir.GetFiles(sDirectory, "*.jpg")
Dim iTemp As Integer
Dim iItem(ddata.GetUpperBound(0)) As ListViewItem
Dim sFile As String
Dim iLocation As Integer = 0
ImageList1.Images.Clear()
ListView1.Items.Clear()
mData = New ArrayList

For Each sFile In ddata
'ReDim Preserve mData(iLocation)
mData.Add(sFile)
ImageList1.Images.Add(System.Drawing.Image.FromFil e(sFile))
iItem(iLocation) = New ListViewItem(sFile, iLocation)
ListView1.Items.Add(iItem(iLocation))
iLocation = iLocation + 1
Next
End Sub

Private Sub removeImage(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ImageList1.Images.RemoveAt(0)
ListView1.Items.RemoveAt(0)
ListView1.Refresh()
Dim ffile As File
Application.DoEvents()

ffile.Move(mData(0).ToString, mData(0).ToString & ".tmp")
mData.RemoveAt(0)
End Sub
End Class
Nov 20 '05 #3
Hi Stephen,

You may try to call the GC.Collect() and GC.WaitForPendingFinalizers() to
see if the problem persists.

Modify your code as follows.

Private Sub removeImage(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
ImageList1.Images.RemoveAt(0)
ListView1.Items.RemoveAt(0)
ListView1.Refresh()
Dim ffile As File
Application.DoEvents()
GC.Collect()
GC.WaitForPendingFinalizers()
File.Move(mData(0).ToString, mData(0).ToString & ".tmp")
mData.RemoveAt(0)
End Sub

For performance issue, when you remove the image from the list, the GC will
not collect the free image immediately.

Here is an article about GC.
Garbage Collection: Automatic Memory Management in the Microsoft .NET
Framework
http://msdn.microsoft.com/msdnmag/is...I/default.aspx
Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 20 '05 #4
Thanks Peter,
I had thought about that but wanted to re-create his
problem on my machine first.

I guess I'll just leave this one to you then. After all, I cant compete with
Microsoft can I ?

Regards - OHM

==========================================

Peter Huang wrote:
Hi Stephen,

You may try to call the GC.Collect() and
GC.WaitForPendingFinalizers() to see if the problem persists.

Modify your code as follows.

Private Sub removeImage(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
ImageList1.Images.RemoveAt(0)
ListView1.Items.RemoveAt(0)
ListView1.Refresh()
Dim ffile As File
Application.DoEvents()
GC.Collect()
GC.WaitForPendingFinalizers()
File.Move(mData(0).ToString, mData(0).ToString & ".tmp")
mData.RemoveAt(0)
End Sub

For performance issue, when you remove the image from the list, the
GC will not collect the free image immediately.

Here is an article about GC.
Garbage Collection: Automatic Memory Management in the Microsoft .NET
Framework
http://msdn.microsoft.com/msdnmag/is...I/default.aspx
Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no
rights.


Best Regards - OHMBest Regards - OHM On**********@BTInternet.Com
Nov 20 '05 #5
there are times when competing with Microsoft is a good thing :)

"One Handed Man [ OHM ]" <te***************************@BTOpenworld.com>
wrote in message news:OJ**************@TK2MSFTNGP11.phx.gbl...
Thanks Peter,
I had thought about that but wanted to re-create his problem on my machine first.

I guess I'll just leave this one to you then. After all, I cant compete with Microsoft can I ?

Regards - OHM

==========================================

Peter Huang wrote:
Hi Stephen,

You may try to call the GC.Collect() and
GC.WaitForPendingFinalizers() to see if the problem persists.

Modify your code as follows.

Private Sub removeImage(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
ImageList1.Images.RemoveAt(0)
ListView1.Items.RemoveAt(0)
ListView1.Refresh()
Dim ffile As File
Application.DoEvents()
GC.Collect()
GC.WaitForPendingFinalizers()
File.Move(mData(0).ToString, mData(0).ToString & ".tmp")
mData.RemoveAt(0)
End Sub

For performance issue, when you remove the image from the list, the
GC will not collect the free image immediately.

Here is an article about GC.
Garbage Collection: Automatic Memory Management in the Microsoft .NET
Framework
http://msdn.microsoft.com/msdnmag/is...I/default.aspx
Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no
rights.


Best Regards - OHMBest Regards - OHM On**********@BTInternet.Com

Nov 20 '05 #6
Thank you for your assistance

"Peter Huang" <v-******@online.microsoft.com> wrote in message
news:A3**************@cpmsftngxa06.phx.gbl...
Hi Stephen,

You may try to call the GC.Collect() and GC.WaitForPendingFinalizers() to
see if the problem persists.

Modify your code as follows.

Private Sub removeImage(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
ImageList1.Images.RemoveAt(0)
ListView1.Items.RemoveAt(0)
ListView1.Refresh()
Dim ffile As File
Application.DoEvents()
GC.Collect()
GC.WaitForPendingFinalizers()
File.Move(mData(0).ToString, mData(0).ToString & ".tmp")
mData.RemoveAt(0)
End Sub

For performance issue, when you remove the image from the list, the GC will not collect the free image immediately.

Here is an article about GC.
Garbage Collection: Automatic Memory Management in the Microsoft .NET
Framework
http://msdn.microsoft.com/msdnmag/is...I/default.aspx
Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by mgrant | last post: by
4 posts views Thread by MrPickwick | last post: by
4 posts views Thread by Just Me | last post: by
2 posts views Thread by =?Utf-8?B?S2VuTg==?= | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.