Reputation: 45
I'm passing information from a timer onto another function, but after the information is passed, it keeps passing it. I don't want to turn the timer off. Is there a way i can stop the loop? The timer interval is 5 seconds.
private static List<DirectoryInfo> list_to_copy = new List<DirectoryInfo>();
DirectoryInfo usbdirectory;
backing_up_interface backing_up_interface;
bool newfilesfound = false;
private void usbchecker_timer_Tick(object sender, EventArgs e)
{
foreach (DriveInfo usbname in DriveInfo.GetDrives().Where(usbproperty => usbproperty.DriveType == DriveType.Removable && usbproperty.IsReady))
{
if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory.ToString() + usbname.VolumeLabel + @"\"))
{
usbdirectory = new DirectoryInfo(usbname.Name);
if (!list_to_copy.Contains(usbdirectory))
{
list_to_copy.Add(usbdirectory);
newfilesfound = true;
}
}
}
if (newfilesfound == true)
{
process_copy();
newfilesfound = false;
}
}
//where information is passed to
private void process_copy()
{
for (int i = 0; i < list_to_copy.Count; i++)
{
backing_up_interface = new backing_up_interface(list_to_copy[i]);
backing_up_interface.Show(); MessageBox.Show(list_to_copy[i].ToString());
}
}
Upvotes: 1
Views: 157
Reputation: 45
It keeps looping because the directory doesn't exist so, i made it create a new directory if the directory isn't found. This stopped it from looping.
private static List<DirectoryInfo> list_to_copy = new List<DirectoryInfo>();
DirectoryInfo usbdirectory;
backing_up_interface backing_up_interface;
bool newfilesfound = false;
private void usbchecker_timer_Tick(object sender, EventArgs e)
{
foreach (DriveInfo usbname in DriveInfo.GetDrives().Where(usbproperty => usbproperty.DriveType == DriveType.Removable && usbproperty.IsReady))
{
if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory.ToString() + usbname.VolumeLabel + @"\"))
{
Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory.ToString() + usbname.VolumeLabel + @"\");
usbdirectory = new DirectoryInfo(usbname.Name);
if (!list_to_copy.Contains(usbdirectory))
{
list_to_copy.Add(usbdirectory);
newfilesfound = true;
}
}
}
if (newfilesfound == true)
{
process_copy();
newfilesfound = false;
}
}
//where information is passed to
private void process_copy()
{
for (int i = 0; i < list_to_copy.Count; i++)
{
backing_up_interface = new backing_up_interface(list_to_copy[i]);
backing_up_interface.Show(); MessageBox.Show(list_to_copy[i].ToString());
}
}
Upvotes: 1
Reputation: 125
Create a directory if the file is not found. it will stop it from looping.
Upvotes: 1
Reputation: 117185
I would use Microsoft's Reactive Extensions (Rx) for this. It is basically is an alternative way to create events with tons of operators from making life very easy.
One operator is Observable.Interval
which effectively sets up a timer, but rather than ticking on a constant basis it will time the interval between when the last handler code ran before the next starts. So if you have a interval of 2
seconds, but your processing takes X
seconds then there will be a X + 2
second gap between the starts, if that makes sense.
Observable
.Interval(TimeSpan.FromSeconds(5.0))
.Subscribe(_ =>
{
foreach (DriveInfo usbname in DriveInfo.GetDrives().Where(usbproperty => usbproperty.DriveType == DriveType.Removable && usbproperty.IsReady))
{
if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory.ToString() + usbname.VolumeLabel + @"\"))
{
usbdirectory = new DirectoryInfo(usbname.Name);
if (!list_to_copy.Contains(usbdirectory))
{
list_to_copy.Add(usbdirectory);
newfilesfound = true;
}
}
}
if (newfilesfound == true)
{
process_copy();
newfilesfound = false;
}
});
Just NuGet "Rx-Main".
Upvotes: 1
Reputation: 54453
You can set some value in the Timer.Tag
property and use it as a flag to control what should happen in the Tick
event.
Of course you need to know when to set and reset the flag..
Tag
is of type object
and can hold anything, including a simple int
or a nice Enum
..
In your case I suggest to use at least three state values:
I would return directly from the Tick
while the copying is in progress; do a check otherwise and set the flag when new files are found.
Then set it before and reset it after the copy method call..
But what logic you want to code is up to you.
You actually are using a 2-state flag already; but copy operations may take longer than expected, so one first thing to try is moving newfilesfound = false;
up before process_copy();
..but having three states would be better, no matter if you store it at class level or with the timer.
Upvotes: 1
Reputation: 27
if (!Directory.Exists......) {
} else { return; // stopping the loop from reaching rest of code! }
Upvotes: 1