Hello Everyone.
I don't know if what i'm trying to do is possible. I'm kinda lost on the thread scenario.
What i want is to restart an timer, everytime a file is created. However, even stopping\starting the timer nothing happens.
I've searched google for my question (c# reset timer filesystemwatcher) but i've not found anything related to this question.
My point is: Is that even possible ? If i'm not mistaken the FileSystemWatcher runs in one thread and Timer in another - right ?
Thanks in advance for any reply\indication on how to do that.
I don't know if what i'm trying to do is possible. I'm kinda lost on the thread scenario.
What i want is to restart an timer, everytime a file is created. However, even stopping\starting the timer nothing happens.
I've searched google for my question (c# reset timer filesystemwatcher) but i've not found anything related to this question.
My point is: Is that even possible ? If i'm not mistaken the FileSystemWatcher runs in one thread and Timer in another - right ?
Thanks in advance for any reply\indication on how to do that.
C#:
namespace DI_Plot_2018
{
public partial class RadForm1 : Telerik.WinControls.UI.RadForm
{
FileSystemWatcher InputFileWatcher;
FileProcessor TIFFProcessor;
static Timer timer1;
bool RestartTimer;
LoadInternalSettings InternalSettings;
public RadForm1()
{
InitializeComponent();
RestartTimer = false;
InternalSettings = new LoadInternalSettings();
//counter = Int32.Parse(InternalSettings.Timer);
timer1 = new Timer();
timer1.Tick += new EventHandler(SecondElapsed);
timer1.Enabled = false;
TIFFProcessor = new FileProcessor();
LoadControls(InternalSettings);
}
DateTime start;
double s;
private void CountDown()
{
start = DateTime.Now;
s = Double.Parse(InternalSettings.Timer);
timer1.Start();
}
private void SecondElapsed(object sender, EventArgs e)
{
double remainingSeconds = s - (DateTime.Now - start).TotalSeconds;
if (remainingSeconds <= 0)
{
TIFFProcessor.TriggerWork();
timer1.Interval = Int32.Parse(InternalSettings.Timer);
timer1.Stop();
timer1.Start();
}
var x = TimeSpan.FromSeconds(remainingSeconds);
TimerTextBox.Text = x.Seconds.ToString();
}
private void FileCreated(Object sender, FileSystemEventArgs e)
{
JobDetails jobInfo = new JobDetails(e.FullPath);
if (TIFFProcessor.JobFilesAndInfoDictionary.ContainsKey(jobInfo.JobNameWithoutColor))
{
TIFFProcessor.JobFilesAndInfoDictionary[jobInfo.JobNameWithoutColor].Add(e.FullPath);
}
else
{
TIFFProcessor.JobFilesAndInfoDictionary.Add(jobInfo.JobNameWithoutColor, new List<string>() { e.FullPath });
}
//Here's where i attempt to restart the timer, after the FileCreatedEvent has been triggered. But nothing happens when i call the Countdown (the method executes but timer doesn't restart, since the tick event doesn't get fired).
timer1.Stop();
CountDown();
}
private void ButtonChangedEventHandler(object sender, EventArgs e)
{
Telerik.WinControls.UI.RadButton bt = sender as Telerik.WinControls.UI.RadButton;
string buttonName = (String)bt.Name;
SwitchButtonImage buttonImageSwitch;
switch (buttonName)
{
case "StartButton":
{
InputFileWatcher = new FileSystemWatcher(InputFolderTextBox.Text);
InputFileWatcher.Created += new FileSystemEventHandler(FileCreated);
InputFileWatcher.Filter = "*.tif";
InputFileWatcher.EnableRaisingEvents = true;
StopButton.Enabled = true;
StartButton.Enabled = false;
CountDown();
//TimerStart();
break;
}
case "StopButton":
{
InputFileWatcher.EnableRaisingEvents = false;
timer1.Stop();
StopButton.Enabled = false;
StartButton.Enabled = true;
break;
}
case "ImageRotationButton":
{
buttonImageSwitch = new SwitchButtonImage(ImageRotationButton, "ImageRotationIndex");
break;
}
case "ImageOrientationButton":
{
buttonImageSwitch = new SwitchButtonImage(ImageOrientationButton, "ImageOrientationIndex");
break;
}
case "InputFolderBrowseButton":
{
InputFolderTextBox.Text = new ItemSaveToConfig.SelectFolderSaveProperty(InputFolderTextBox.Text, "Select the input folder", "InputFolder").folderSelect;
break;
}
case "OutputFolderBrowseButton":
{
OutputFolderTextBox.Text = new ItemSaveToConfig.SelectFolderSaveProperty(OutputFolderTextBox.Text, "Select the output folder", "OutputFolder").folderSelect;
break;
}
}
}
private void LoadControls(LoadInternalSettings Settings)
{
InputFolderTextBox.Text = InternalSettings.InputFolder;
OutputFolderTextBox.Text = InternalSettings.OutputFolder;
NumberOfZonesTextBox.Text = InternalSettings.NumberOfZones;
FirstZoneWidthTextBox.Text = InternalSettings.FirstZoneWidth;
LastZoneWidthTextBox.Text = InternalSettings.LastZoneWidth;
ZoneAreaWidthTextBox.Text = InternalSettings.ZoneAreaWidth;
ZoneAreaHeightTextBox.Text = InternalSettings.ZoneAreaHeight;
ZoneWidthTextBox.Text = InternalSettings.ZoneWidth;
TimerTextBox.Text = InternalSettings.Timer;
ZoneWidthTextBox.Text = InternalSettings.ZoneWidth;
ImageRotationButton.ChangeClickCountFromConfigFile(Int32.Parse(InternalSettings.ImageRotationIndex));
ImageOrientationButton.ChangeClickCountFromConfigFile(Int32.Parse(InternalSettings.ImageOrientationIndex));
//xmlRotateFront.ChangeClickCountFromConfigFile(internalSettings.XMLtoPPFRotateFrontIndex);
var RotatePreview = new SwitchButtonImage(ImageRotationButton, "ImageRotationIndex");
var OrientationPreview = new SwitchButtonImage(ImageOrientationButton, "ImageOrientationIndex");
}
private void TextBoxChangedEventHandler(object sender, EventArgs e)
{
Telerik.WinControls.UI.RadTextBox TxtBx = sender as Telerik.WinControls.UI.RadTextBox;
string TxtName = TxtBx.Name;
switch (TxtName)
{
case "NumberOfZonesTextBox":
{
NumberOfZonesTextBox.Text = new ItemSaveToConfig.ChangeAndSaveTextBox(NumberOfZonesTextBox.Text, "NumberOfZones", false).TextToApply;
break;
}
case "ZoneWidthTextBox":
{
ZoneWidthTextBox.Text = new ItemSaveToConfig.ChangeAndSaveTextBox(ZoneWidthTextBox.Text, "ZoneWidth", false).TextToApply;
break;
}
case "FirstZoneWidthTextBox":
{
ZoneWidthTextBox.Text = new ItemSaveToConfig.ChangeAndSaveTextBox(FirstZoneWidthTextBox.Text, "FirstZoneWidth", false).TextToApply;
break;
}
case "LastZoneWidthTextBox":
{
ZoneWidthTextBox.Text = new ItemSaveToConfig.ChangeAndSaveTextBox(LastZoneWidthTextBox.Text, "LastZoneWidth", false).TextToApply;
break;
}
}
}
private void RadForm1_FormClosed(object sender, FormClosedEventArgs e)
{
timer1.Stop();
new ItemSaveToConfig.SelectItemSaveProperty("Timer", TimerTextBox.Text.ToString());
//WaitingTimer.Stop();
Environment.Exit(0);
}
}
}