ImageList Holding Reference

  • Thread starter Stephen Costanzo
  • Start date
S

Stephen Costanzo

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.FromFile(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
 
O

One Handed Man [ OHM ]

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

Best Regards - OHM

(e-mail address removed)




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.FromFile(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
 
S

Stephen Costanzo

nope. nor do I get it (in Explorer, at least) when I stop the application

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

Best Regards - OHM

(e-mail address removed)




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.FromFile(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
 
O

One Handed Man [ OHM ]

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

(e-mail address removed)




nope. nor do I get it (in Explorer, at least) when I stop the application

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

Best Regards - OHM

(e-mail address removed)




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.FromFile(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
 
P

Peter Huang

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/issues/1100/GCI/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.
 
O

One Handed Man [ OHM ]

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 said:
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/issues/1100/GCI/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 (e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top