Question Restart timer on FileCreated Event

pablobhz

Member
Joined
Sep 4, 2018
Messages
6
Programming Experience
1-3
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.

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);
        }
    }
}
 
Nevermind.
Fixed creating an outside boolean.
I wanted to delete the thread but couldn't find where to do so.

Thanks in advance !
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;




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(RestartTimer == true)
            {
                timer1.Stop();
                CountDown();
                RestartTimer = false;
            }
            else 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 });
            }
            RestartTimer = true;
        }
        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);
        }
    }
}
 
It's not that a FileSystemWatcher "runs" in another thread, but rather that it raises its events on background threads. The solution is then to marshal a method call to the UI thread, e.g.
private void ResetTimer()
{
    if (InvokeRequired)
    {
        Invoke(New MethodInvoker(ResetTimer));
    }
    else
    {
        Timer1.Stop();
        Timer1.Start();
    }
}

You might like to read this to see how that code pattern can be constructed for any scenario.
 

Latest posts

Back
Top Bottom