Click here to Skip to main content
15,998,180 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
See more: C#
I am trying to schedule an image capture every 15 seconds; I have a string array of two gates. I have tried to set up intervals on the image capture, but I am not getting the results needed the closes I get is the image getting saved with a current timestamp; as the code run through the second sets of images after interval 15 seconds the new image takes on the time stamp of the first sets of images thus replacing original image with same timestamp but current image taken.

I would need to have every image on a schedule every 15 seconds taking the current time of the image creation.



below is what I have so far; Thank you for your help in advance

private void timer1_Tick(object sender, EventArgs e)
{

foreach (string G in GateNumber1)
{
string path1 = string.Format(@"C:\Users\Ig\Desktop\temp\Gate" + G + "{0:yyMMddHHmmss}" + ".jpg", currentTime);
FileStream fs = File.Create(path1);
pictureBox1.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
fs.Close();

}


foreach (string G in GateNumber3)
{

string path1 = string.Format(@"C:\Users\Ig\Desktop\temp\Gate" + G + "{0:yyMMddHHmmss}" + ".jpg", currentTime);
FileStream fs = File.Create(path1);
pictureBox1.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
fs.Close();

}
Posted
Comments
Sergey Alexandrovich Kryukov 23-Aug-14 23:14pm    
To start with, never ever use System.Windows.Forms.Timer (it looks like this is what you use) for anything which is supposed to be more or less periodic, or require any accuracy at all...
—SA
@SkyHigh34 23-Aug-14 23:50pm    
I did as you suggested;

private void Form1_Load(object sender, EventArgs e)
{


JPEGStream stream = new JPEGStream("http://192.168.0.10/cgi-bin/camera?ch=[CHANNEL]&resolution=");
stream.NewFrame += new NewFrameEventHandler(FinalFrame_NewFrame);
stream.Start();

aTimer = new System.Timers.Timer(1000);
aTimer.Elapsed += new ElapsedEventHandler(Form1_Load);
aTimer.Interval = 15000;
aTimer.Enabled = true;

foreach (string G in GateNumber1)
{
string path1 = string.Format(@"C:\Users\Ig\Desktop\temp\Gate" + G + "{0:yyMMddHHmmss}" + ".Jpg", currentTime);
FileStream fs = File.Create(path1);
pictureBox1.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);

}

}







I set my own timers but now I get

An exception of type 'System.IO.IOException' occurred in mscorlib.dll but was not handled in user code

Additional information: The process cannot access the file 'C:\Users\Ig\Desktop\temp\GateOne140823234809.Jpg' because it is being used by another process.

If there is a handler for this exception, the program may be safely continued.
Sergey Alexandrovich Kryukov 23-Aug-14 23:53pm    
That timer type is much better, but don't assume the handler is called on the UI thread.
As to the exception, find out: in what line?
—SA
@SkyHigh34 23-Aug-14 23:58pm    
I should have posted the line; it is in line FileStream fs = File.Create(path1);

Thanks
Sergey Alexandrovich Kryukov 24-Aug-14 0:27am    
Thank you. It's clear enough now. Please see my answer.
—SA

1 solution

@SkyHigh34 wrote:
Additional information: The process cannot access the file 'C:\Users\Ig\Desktop\temp\GateOne140823234809.Jpg' because it is being used by another process.
I'll tell you what to do in such situations, which a pretty usual. This is easy to investigate.

First of all, the similar question was asked here many times, and from this experience I know: in most cases the blocking process is your own process. You could have forgotten to dispose/close something in the same application. So, first of all, check it up. To explore this possibility, please see my past answer:
Clearing a Handle in C#[^].

In this answer, pay attention for the using of the using statement which helps you to guarantee that appropriate file system object is properly disposed after use, not keeping the file locked.

In some cases, you really need to investigate which process holds which file. For this, I recommend using one utility from the Sysinternals Suite. This set of utilities (formerly from Winternals company, presently at Microsoft) is a must-have for any developer, please see:
http://technet.microsoft.com/en-us/sysinternals/bb842062[^],
http://technet.microsoft.com/en-us/sysinternals/bb545027[^].

The utility you need is "handle.exe", please see:
http://technet.microsoft.com/en-us/sysinternals/bb896655[^].

In your case, you use it with file name parameter:
handle.exe <file_name>


This utility will scan all kinds of handles, not just file handles. For file, it will scan all file handles matching the file name (so it does not have to be a full path name) and return information sufficient to identify each process, including its pid. So, if you need more information on a process in question, you can also use other Sysinternals utilities, in particular, its Process Explorer:
http://technet.microsoft.com/en-us/sysinternals/bb896653[^].

Good luck,
—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900