Picturebox refresh problems

J

John Dann

I have a remote network application that is placing a frequently
updated copy of an image file on to a network drive every few seconds
and I'm trying to write a little utility to view and automatically
refresh the image on a local PC. The refresh is meant to happen in 2
stages - a file copy and an image refresh - which are both running in
a timer loop:

Timer1_Tick(etc...)
File.Copy(RemoteImageFile, LocalImageFile, True))
Picturebox1.Image = Image.FromFile(LocalImageFile)
Picturebox1.Refresh
End Sub

This runs OK for the first timer event but at the second tick then
there's an IOException at the File.Copy line, apparently because the
file copy cannot complete and then presumably because the picturebox
still has the image file open.

Is there some way of closing the picturebox's access to the file (if
indeed that's what's causing the problem)?
 
A

Armin Zingler

John said:
I have a remote network application that is placing a frequently
updated copy of an image file on to a network drive every few seconds
and I'm trying to write a little utility to view and automatically
refresh the image on a local PC. The refresh is meant to happen in 2
stages - a file copy and an image refresh - which are both running in
a timer loop:

Timer1_Tick(etc...)
File.Copy(RemoteImageFile, LocalImageFile, True))
Picturebox1.Image = Image.FromFile(LocalImageFile)
Picturebox1.Refresh
End Sub

This runs OK for the first timer event but at the second tick then
there's an IOException at the File.Copy line, apparently because the
file copy cannot complete and then presumably because the picturebox
still has the image file open.

Is there some way of closing the picturebox's access to the file (if
indeed that's what's causing the problem)?

Untested:

Timer1_Tick(etc...)
dim img as image

img = picturebox1.image

if img isnot nothing then
picturebox1.image = nothing
img.dispose
end if

File.Copy(RemoteImageFile, LocalImageFile, True))

Picturebox1.Image = Image.FromFile(LocalImageFile)
Picturebox1.Refresh
End Sub


Does this work?


Armin
 
L

Lloyd Sheen

John Dann said:
I have a remote network application that is placing a frequently
updated copy of an image file on to a network drive every few seconds
and I'm trying to write a little utility to view and automatically
refresh the image on a local PC. The refresh is meant to happen in 2
stages - a file copy and an image refresh - which are both running in
a timer loop:

Timer1_Tick(etc...)
File.Copy(RemoteImageFile, LocalImageFile, True))
Picturebox1.Image = Image.FromFile(LocalImageFile)
Picturebox1.Refresh
End Sub

This runs OK for the first timer event but at the second tick then
there's an IOException at the File.Copy line, apparently because the
file copy cannot complete and then presumably because the picturebox
still has the image file open.

Is there some way of closing the picturebox's access to the file (if
indeed that's what's causing the problem)?

For some reason the code you are using retains a lock on the file. I use
the following code:

Using stream As Stream = New FileStream(fileName, FileMode.Open,
FileAccess.Read)
pb.Image = Image.FromStream(stream)
End Using

This will not hold a lock on the file. Go figure.

LS
 
J

John Dann

Thanks everyone - I'll give those solutions a go.

Cor, the reason for using a timer was a combination of ignorance and
safety. I just wasn't sure how well FSW works when monitoring a file
across a network, or if it works well (as it probably does) how much
of a load/traffic it might place across the network. This watching
event would need to happen every 2-3 seconds continuously 24/7 and I'm
also anxious not to interfere in any way with the completely separate
process that generates the source file. So it was safety first really
even if it's a less elegant solution..
 

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