"new System.Timers.ElapsedEventHandler(DoStuff)" call did not work - c#

I'am trying to create watch folder aplicaction in C# that will do an action when new file will arrive. Since the watched folder is on GPFS share I'am unable to use FileSystemWatcher (which works fine for me in NTFS). So I've based the app on other collegue solution.
The app shows nicely "Timer starts" message but when it comes to
timer.Elapsed += new System.Timers.ElapsedEventHandler(DoStuff);
it did not calls the DoStuff method - "Starting new files proc" message never show up. What I've done wrong? Here is complete code:
namespace MonitorFolderActivity
{
public partial class frmMain : Form
{
List<string> fileList = new List<string>();
System.Timers.Timer timer;
DateTime LastChecked;
public frmMain()
{
InitializeComponent();
}
private void abortAcitivityMonitoring()
{
btnStart_Stop.Text = "Start";
txtActivity.Focus();
}
private void startActivityMonitoring(string sPath)
{
if (sPath.Length < 3)
{
MessageBox.Show("You have to enter a folder to monitor.",
"Hey..!", MessageBoxButtons.OK, MessageBoxIcon.Stop);
abortAcitivityMonitoring();
}
else
{
TS_AddLogText(string.Format("Timer starts\r\n"));
timer = new System.Timers.Timer();
timer.AutoReset = false;
timer.Elapsed += new System.Timers.ElapsedEventHandler(DoStuff);
}
}
private void stopActivityMonitoring()
{
TS_AddLogText(string.Format("Timer stops\r\n"));
this.timer.Stop();
}
private void DoStuff(object sender, System.Timers.ElapsedEventArgs e)
{
TS_AddLogText(string.Format("Starting new files proc\r\n"));
LastChecked = DateTime.Now;
string[] files = System.IO.Directory.GetFiles("D:\\MEDIAIN\\", "*", System.IO.SearchOption.AllDirectories);
foreach (string file in files)
{
if (!fileList.Contains(file))
{
fileList.Add(file);
TS_AddLogText(string.Format(file));
}
}
TimeSpan ts = DateTime.Now.Subtract(LastChecked);
TimeSpan MaxWaitTime = TimeSpan.FromMinutes(1);
if (MaxWaitTime.Subtract(ts).CompareTo(TimeSpan.Zero) > -1)
timer.Interval = MaxWaitTime.Subtract(ts).TotalMilliseconds;
else
timer.Interval = 1;
timer.Start();
}
private delegate void AddLogText(string text);
private void TS_AddLogText(string text)
{
if (this.InvokeRequired)
{
AddLogText del = new AddLogText(TS_AddLogText);
Invoke(del, text);
}
else
{
txtActivity.Text += text;
}
}
private void btnStart_Stop_Click(object sender, EventArgs e)
{
if (btnStart_Stop.Text.Equals("Start"))
{
btnStart_Stop.Text = "Stop";
startActivityMonitoring(txtFolderPath.Text);
}
else
{
btnStart_Stop.Text = "Start";
stopActivityMonitoring();
}
}
private void lblActivity_Click(object sender, EventArgs e)
{
}
private void lblToMonitor_Click(object sender, EventArgs e)
{
}
}
}

There are few issues in your code.
First of all you are not setting the time at which timer should elapse, which means it will read the default value which is
100 ms
Secondly you are not starting your timer. You need to add this line to your code in this method startActivityMonitoring else statement.
timer.Interval = yourdesiredinterval;
timer.Start();
Thirdly, as you are doing stop and start (by looks of your code) you should not create a new timer on each call of your startActivityMonitoring method. Rather you should do this
If(timer == null)
{
timer = new System.Timers.Timer();
timer.AutoReset = false;
timer.Interval = yourinterval;
timer.Elapsed += new System.Timers.ElapsedEventHandler(DoStuff);
}
timer.Start();

In your else clause, you never start the timer. Here's a fix:
else
{
TS_AddLogText(string.Format("Timer starts\r\n"));
timer = new System.Timers.Timer();
timer.AutoReset = false;
timer.Elapsed += new System.Timers.ElapsedEventHandler(DoStuff);
timer.Start();
}

Related

CSHARP running while every X seconds but until bool is true

I would like to check updates from txt file on web server. and send a messagebox when there is version mismatch, I'm using this code and it works but I want it only to connect the server every 10 minutes and check the text file.
is there any option to do that?
maybe using another way to create this loop?
I also would like to send the messagebox only once
bool hasDisplayed = false;
private string UpdateCheckServer()
{
WebClient client = new WebClient();
Stream stream = client.OpenRead("http://localhost/update/update_version.txt");
StreamReader reader = new StreamReader(stream);
string serverversion = reader.ReadToEnd();
return serverversion;
}
private string UpdateCheckClient()
{
string ClientVersion = System.IO.File.ReadAllText("update_version.txt");
return ClientVersion;
}
private void UpdateCheckTimer()
{
while (!hasDisplayed)
{
if (starter.Enabled == true && UpdateCheckServer() == UpdateCheckClient())
{
//Nothing here
}
if (starter.Enabled == true && UpdateCheckServer() != UpdateCheckClient())
{
MessageBox.Show("not updated");
hasDisplayed = true;
}
}
An implementation based on timer can look like this:
private bool _equalVersion = true;
private System.Timers.Timer _timer = new System.Timers.Timer(1000 * 60 * 10); //millisecond * seconds * minutes
private void StartUpdateCheckTimer()
{
_timer.Elapsed += UpdateCheck;
_timer.Start();
}
private void UpdateCheck(object sender, ElapsedEventArgs e)
{
_timer.Stop();
_equalVersion = (UpdateCheckServer() == UpdateCheckClient());
if (!_equalVersion)
{
MessageBox.Show("not updated");
}
else
{
_timer.Start();
}
}
in this way you get a timer:
triggered each 10 minutes,
performing the check
notifying the version change (and stopping) or proceeding with the subsequent iterations
then you can add some more logic like for example reset of the check, more proper notification action (separating your check routine from the GUI part) and many more.
Another approach (as pointed out by #imsmn) can be to make your implementation System.Threading.Sleep based, but I'd suggest the first option based on timer.
After several tries and learning I managed to do that.
private string UpdateCheckServer()
{
WebClient client = new WebClient();
Stream stream = client.OpenRead("http://localhost/update/update_version.txt");
StreamReader reader = new StreamReader(stream);
string serverversion = reader.ReadToEnd();
return serverversion;
}
private string UpdateCheckClient()
{
string ClientVersion = System.IO.File.ReadAllText("update_version.txt");
return ClientVersion;
}
private void UpdateCheckTimer()
{
System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
t.Interval = 15000; // specify interval time as you want
t.Tick += new EventHandler(timer_Tick);
t.Start();
void timer_Tick(object sender, EventArgs e)
{
//Call method
if (starter.Enabled == true && UpdateCheckServer() != UpdateCheckClient())
{
t.Stop();
MessageBox.Show("not updated");
}
}
}

C# - Timer.Stop() isn't working

I make a call via client side to initiate the StopTickerTimer function which successfully calls the StopTickerTimer function but unfortunately doesn't actually stop the timer. The timer starts and works properly but I can't seem to understand why my timer isn't stopping. I've tried setting timer.Enabled = false. I've tried various solutions that I've found on here but nothing seems to work.
public Timer timer = new Timer();
private string jsonContents;
private string currentTickerPlaylist;
private int i = 0;
private List<string> playlistTickers;
public void StartTickerTimer(int seconds, string selectedPlaylist)
{
currentTickerPlaylist = selectedPlaylist;
InitPlaylistTickerTimer(seconds);
}
public void StopTickerTimer()
{
StopTimer();
}
private void InitPlaylistTickerTimer(int seconds)
{
timer.Elapsed += new ElapsedEventHandler(t_Tick);
timer.Interval = seconds * 1000;
timer.AutoReset = true;
timer.Start();
}
private void StopTimer()
{
timer.Stop();
}
private void t_Tick(object sender, EventArgs e)
{
SetPlaylistTickerSettings();
if (i >= playlistTickers.Count)
{
i = 0;
Payload ticker = new Payload
{
ticker = playlistTickers[i]
};
Payload ticker2 = new Payload
{
payload = ticker
};
var json = ServiceStack.Text.JsonSerializer.SerializeToString(ticker2);
PutToSingular("url", json);
i++;
}
else
{
Payload ticker = new Payload
{
ticker = playlistTickers[i]
};
Payload ticker2 = new Payload
{
payload = ticker
};
var json = ServiceStack.Text.JsonSerializer.SerializeToString(ticker2);
PutToSingular("url", json);
i++;
}
}
You started your timer, but you are not calling your "StopTimer" anywhere in the code...

C# Windows Service timer ticks more than one

I'm trying to make a service that should execute my codes every second after my codes execution ends. But when I start the service and debug it, timer ticks more than one, I mean, my codes executed twice before first execution ends.
here is my codes ;
Timer timer1 = new Timer(1000);
bool _service_working = false;
protected override void OnStart(string[] args)
{
timer1.Elapsed += new ElapsedEventHandler(runProcess);
timer1.Enabled = true;
timer1.Start();
_service_working = false;
}
protected override void OnStop()
{
timer1.Enabled = false;
}
private void runProcess(object sender, ElapsedEventArgs e)
{
try
{
if (_service_working == false)
{
timer1.Enabled=false;
_service_working = true;
}
#region myCodes
}
catch (Exception ex)
{
_logService.insert_log(1022, 2, ex.Message, "Path : runProcess");
}
finally
{
_service_working = false;
timer1.Enabled = true;
}
}
When I debug it, timer ticks again at runProcess if block...
Assuming you're using System.Timers.Timer then Timer events are raised in a background thread usually which means without any locking your timer event methods can overlap.
An easy solution is to set AutoReset to false and instead in your timer event restart it once complete, for example:
private bool terminating;
public Service()
{
terminating = false;
timer = new Timer(1000);
timer.Elapsed += new ElapsedEventHandler(runProcess);
timer.Enabled = true;
timer.AutoReset = false;
}
protected override void OnStart(string[] args)
{
timer.Start();
}
protected override void OnStop()
{
terminating = true;
timer1.Stop();
}
private void runProcess(object sender, ElapsedEventArgs e)
{
// Do stuff
if (!terminating)
timer.Start(); // Restart timer
}

C# Timer Event not firing off

I can't get the timer to tick every time when i create a new command object I am wondering what is causing this. I am new to C# so if i could get help as to why this is happening it would be greatly appreciated.
This should trigger, but doesnt
command Cmd = new command("!example", 10);
Here's the code.
public class Timeout
{
public Timeout() { }
public static List<command> timeouts = new List<command>();
public class command
{
public string cmd;
public int seconds;
private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer() { Interval = 1000 };
public command(string cmd, int seconds)
{
Debug.WriteLine("created, " + cmd + ", " + seconds);
this.cmd = cmd;
this.seconds = seconds;
this.timer.Tick += new EventHandler(Timer_Tick);
timer.Start();
}
private void Timer_Tick(object sender, EventArgs e)
{
Debug.WriteLine("tick -> " + seconds);
if (seconds > 0)
seconds--;
else
{
timer.Tick -= Timer_Tick;
timeouts.Remove(this);
Debug.WriteLine("removed");
}
}
}
}
actually you can do this way.
call the timer once you create the object
command cmd =new command("!example",10);
//then am calling the timer event
timer = new Timer(3000); // Set up the timer for 3 seconds
timer.Elapsed += new ElapsedEventHandler(timerElapsed);
//_timer_Elapsed is the event name
timer.Enabled = true;
public static void timerElapsed(object sender, ElapsedEventArgs e)
{
//do something here
}
please find the link here for more reference.hope my comment helps you :)

Creating a SplashScreen/LoadingScreen with timer

I created a working SplashScreen/LoadingScreen.
I used the following code to show and close the LoadinScreen:
LoadingScreen LS = new LoadingScreen();
LS.Show();
databaseThread = new Thread(CheckDataBase);
databaseThread.Start();
databaseThread.Join();
LS.Close();
This code is doing a great job for me, showing and closing the LoadingScreen.
The problem is: I got some text on the LoadingScreen, that says: Loading Application...
I want to create a Timer to let the dots at the end of the text(Label) do the following:
Loading Application.
1 second later:
Loading Application..
1 second Later:
Loading Application...
I suppose that I need to add a timer to the Load_event of the LoadingScreen form.
How can I achieve this?
Maybe something like this?
class LoadingScreen
{
Timer timer0;
TextBox mytextbox = new TextBox();
public LoadingScreen()
{
timer0 = new System.Timers.Timer(1000);
timer0.Enabled = true;
timer0.Elapsed += new Action<object, System.Timers.ElapsedEventArgs>((object sender, System.Timers.ElapsedEventArgs e) =>
{
switch (mytextbox.Text)
{
case "Loading":
mytextbox.Text = "Loading.";
break;
case "Loading.":
mytextbox.Text = "Loading..";
break;
case "Loading..":
mytextbox.Text = "Loading...";
break;
case "Loading...":
mytextbox.Text = "Loading";
break;
}
});
}
}
Edit:
A good way to prevent UI thread to block waiting for database operation is to move the database operation to a BackgroundWorker ex:
public partial class App : Application
{
LoadingScreen LS;
public void Main()
{
System.ComponentModel.BackgroundWorker BW;
BW.DoWork += BW_DoWork;
BW.RunWorkerCompleted += BW_RunWorkerCompleted;
LS = new LoadingScreen();
LS.Show();
}
private void BW_DoWork(System.Object sender, System.ComponentModel.DoWorkEventArgs e)
{
//Do here anything you have to do with the database
}
void BW_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
LS.Close();
}
}
It should be as simple as:
Timer timer = new Timer();
timer.Interval = 300;
timer.Tick += new EventHandler(methodToUpdateText);
timer.Start();

Categories

Resources