System.Timers.Timer Not Triggered On Time in CPU-Intensive Program - c#

I have .Net framwork 4.7.2 application, which handles cpu-intensive workload.
It uses system.timers.timer, intend to force program timeout after x mins.
The timer sometimes is not able to trigger on time.
Any suggestion on making the timer work properly, without loosing workload efficiency?
Following is the code to demonstrate the issue.
The timer set to triggered every 5 secs, but result is not as expected, which is shown in screenshot.
Please note that size of taskCollection is not predictable.
class Program
{
static void Main(string[] args)
{
var taskCollection = new string[new Random().Next(10, 100)];
SetupTimer();
Parallel.ForEach(taskCollection
, new ParallelOptions { MaxDegreeOfParallelism = -1 }
, task =>
{
SlowTask();
}
);
}
static void SetupTimer()
{
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += (s_, e_) =>
{
OnTimedEvent(s_, e_);
};
aTimer.Interval = 5000;
aTimer.Enabled = true;
Console.WriteLine($"Timer has been started at {DateTime.Now}.");
}
static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine($"Timer has been triggered at {DateTime.Now}.");
}
static void SlowTask()
{
long nthPrime = FindPrimeNumber(10000000); //set higher value for more time
}
static long FindPrimeNumber(int n)
{
int count = 0;
long a = 2;
while (count < n)
{
long b = 2;
int prime = 1;
while (b * b <= a)
{
if (a % b == 0)
{
prime = 0;
break;
}
b++;
}
if (prime > 0)
{
count++;
}
a++;
}
return (--a);
}
}

Related

Windows fourm crashes after clicking button

I decided to create a basic timer just for my use and everytime i click the start button the program freezes up completely. Im i missing something obvious or?
namespace Timer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_Start_Click(object sender, EventArgs e)
{
int secs = 0, mins = 0, hours = 0;
for (int num1 = 1; num1 > 0;)
{
txt_secs.Text = secs.ToString() ;
System.Threading.Thread.Sleep(1000);
secs = secs + 1;
if (secs == 60)
{
secs = 0;
mins = mins + 1;
txt_mins.Text = mins.ToString();
}
if (mins == 60)
{
mins = 0;
hours = hours + 1;
txt_hours.Text = hours.ToString();
}
}
}
}
}
The issue is in System.Threading.Sleep()
Per Microsoft Documentation your thread will be suspended. The thread is in an infinite loop and thus will be (essentially) permanently suspended.
Consider using Timers or the Stopwatch Class or (if you need threading) System.Threading.Timer Instead

C#: Task Factory starts task repeatedly?

I have a probably simple question about the task factory. I have to following code:
In this task is a loop that is polling data from the RS232 and a counter that stops polling after 10 times. After this "doCollect" will be set to false.
And now comes the strange thing: The task runs repeatedly. The caller code is:
// class Main()
RS232DataAquisition _RS232DataAquisition = new RS232DataAquisition();
public override void Run()
{
System.Diagnostics.Stopwatch timeout = new System.Diagnostics.Stopwatch();
timeout.Start();
_RS232DataAquisition.Start();
while ((timeout.ElapsedMilliseconds <= (dataGatherTime_inSeconds * 1000)) && _RS232DataAquisition.DoCollect)
{
System.Threading.Thread.Sleep(100);
}
timeout.Stop();
_RS232DataAquisition.Stop();
}
Per my understanding the Run() function should start the thread and return into the while-loop waiting for the thread to finish. But it never does?!
Here's the code for ReadDataFromRS232:
// sealed class RS232DataAquisition
private bool doCollect = false;
public bool DoCollect
{
get { return doCollect; }
}
public void Start()
{
doCollect = true;
currentTask = System.Threading.Tasks.Task.Factory.StartNew(() =>
{
this.ReadDataFromRS232();
});
}
private void ReadDataFromRS232(int NumtoRead = 10)
{
var port = new System.IO.Ports.SerialPort(PortName);
int waitCount = 5;
var portExists = System.IO.Ports.SerialPort.GetPortNames().Any(x => x == PortName);
if (!portExists)
{
throw new ArgumentException("Port does not exist!");
}
while (port.IsOpen && waitCount-- > 0)
{
doCollect = false;
Wait();
}
doCollect = true;
if (!port.IsOpen)
{
port.Open();
port.NewLine = _NewLine;
port.ReadTimeout = 2000;
int number;
try { }
finally { }
port.Write("flashon\r");
while (doCollect && (_readCounter <= NumtoRead))
{
string s;
try
{
s = port.ReadLine();
}
catch
{
s = "-1";
}
int i;
if (int.TryParse(s, out i))
{
number = Convert.ToInt32(s, 10);
}
else
{
number = 0;
}
lock (thisLock) _data.Add(number);
_readCounter++;
}
port.Write("flashoff\r");
port.Close();
port.Dispose();
Wait(); Wait();
}
}
private void Wait()
{
System.Threading.Thread.Sleep(10);
System.Threading.Thread.SpinWait(1);
}
I don't get, why "ReadDataFromRS232" is beeing repeated until the timeout stops this task.
Thank you for any help :)
EDIT: Added some missing code.
As Dennis said the problem seemed to come from the missing volatile. It works now even though I have no idea why it didn't before.

Ping Completed Event Handler not working [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
When I execute this code in command line, it's working fine:
class Program
{
private static List<Ping> pingers = new List<Ping>();
private static List<string> value = new List<string>();
private static int instances = 0;
private static object #lock = new object();
private static int result = 0;
private static int timeOut = 2500;
private static int ttl = 7;
public static void Main()
{
string baseIP = "192.168.1.";
Console.WriteLine("Pinging destinations of D-class in {0}*", baseIP);
CreatePingers(254);
PingOptions po = new PingOptions(ttl, true);
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
byte[] data = enc.GetBytes("");
SpinWait wait = new SpinWait();
int cnt =1;
Stopwatch watch = Stopwatch.StartNew();
foreach (Ping p in pingers)
{
lock (#lock)
{
instances += 1;
}
p.SendAsync(string.Concat(baseIP, cnt.ToString()), timeOut, data, po);
cnt += 1;
}
//while (instances > 0)
//{
// wait.SpinOnce();
//}
watch.Stop();
for (int i = 0; i < value.Count; i++)
{
Console.WriteLine(value[i]);
}
DestroyPingers();
Console.WriteLine("Finished in {0}. Found {1} active IP-addresses.", watch.Elapsed.ToString(), result);
Console.ReadKey();
}
public static void Ping_completed(object s, PingCompletedEventArgs e)
{
lock (#lock)
{
instances -= 1;
}
if (e.Reply.Status == IPStatus.Success)
{
string sa = string.Concat("Active IP: ", e.Reply.Address.ToString());
value.Add(sa);
//Console.WriteLine(sa);
String diachiip = e.Reply.Address.ToString();
result += 1;
}
else
{
//Console.WriteLine(String.Concat("Non-active IP: ", e.Reply.Address.ToString()))
}
}
private static void CreatePingers(int cnt)
{
for (int i = 1; i <= cnt; i++)
{
Ping p = new Ping();
p.PingCompleted += Ping_completed;
pingers.Add(p);
}
}
private static void DestroyPingers()
{
foreach (Ping p in pingers)
{
p.PingCompleted -= Ping_completed;
p.Dispose();
}
pingers.Clear();
}
}
But when I convert from it to window form, it doesn't work. I don't kwow why, I have tried many different ways...
Code is here:
public partial class Form1 : Form
{
public static List<Ping> pingers = new List<Ping>();
public static List<string> value = new List<string>();
public static int instances = 0;
public static object #lock = new object();
public static int result = 0;
public int timeout = 2500;
public static int ttl = 7;
public Form1()
{
InitializeComponent();
}
public void btnscan_Click(object sender, EventArgs e)
{
string baseIP = "192.168.1.";
//int kt = Int32.Parse(txtkt.Text);
//int start = Int32.Parse(txtstart.Text);
CreatePingers(254);
PingOptions po = new PingOptions(ttl, true);
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
byte[] data = enc.GetBytes("");
int cnt = 1;
Stopwatch watch = Stopwatch.StartNew();
foreach (Ping p in pingers)
{
lock (#lock)
{
instances += 1;
}
p.SendAsync(string.Concat(baseIP, cnt.ToString()), timeout, data, po);
cnt += 1;
}
watch.Stop();
//Result alway return 0
lst1.Items.Add(result.ToString());
lst1.Items.Add(value.Count.ToString());
for (int i = 0; i < value.Count; i++)
{
lst1.Items.Add(value[i]);
lst1.Items.Add("\n");
}
DestroyPingers();
string a = "Finished in " + watch.Elapsed.ToString() + ". Found " + result + " active IP-addresses.";
lst1.Items.Add(a);
}
public static void CreatePingers(int kt)
{
for (int start = 1; start <= kt; start++)
{
// class System.Net.NetworkInformation.Ping
Ping p = new Ping();
p.PingCompleted += Ping_completed();
pingers.Add(p);
}
}
public static PingCompletedEventHandler Ping_completed()
{
PingCompletedEventHandler a = new PingCompletedEventHandler(abc);
return a;
}
static void abc(object s, PingCompletedEventArgs e)
{
value.Add("abc");
lock (#lock)
{
instances -= 1;
}
if (e.Reply.Status == IPStatus.Success)
{
string abcd = string.Concat("Active IP: ", e.Reply.Address.ToString());
value.Add(abcd);
result += 1;
}
}
public static void DestroyPingers()
{
foreach (Ping p in pingers)
{
p.PingCompleted -= Ping_completed();
p.Dispose();
}
pingers.Clear();
}
}
What is wrong in this code?
Method SendAsync returns 0 because you are not waiting for it to complete. You are missing await and async (see msdn):
async void btnscan_Click(object sender, EventArgs e)
{
...
await p.SendAsync(string.Concat(baseIP, cnt.ToString()), timeout, data,
...
}
SpinWait was making code to work in console application. In winforms you should not use SpinWait (nor Sleep) in UI thread. You can create another thread (e.g. by using Task) and then you can copy/paste code from console application 1-to-1. But then you will need to use Invoke each time when you want to access UI controls.
async/await is really better.. if it will work (I concluded that from method name, I've no idea what method does, nor how to use it).
Perhaps I miss one thing, if SendAsync returns value, then you can get it by (the requirement to mark method where you use await with async still):
var result = await p.SendAsync(...);

how do i reset the timer at the end of the day automatically?

How do i reset the timer at the end of the day automatically and how do i display the time and date it was executed the last time?
The program is -
namespace Time_Writer
{
class Program
{
static int count = 1;
static double seconds;
static int total = 10000;
private static System.Timers.Timer aTimer;
static void Main(string[] args)
{
ReadCountFromFile();
aTimer = new System.Timers.Timer();
aTimer.Elapsed +=new System.Timers.ElapsedEventHandler(aTimer_Elapsed);
aTimer.Interval = 5000;
aTimer.Enabled = true;
Console.WriteLine("Press Enter To Exit The Program\n");
Console.ReadLine();
AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);
}
private static void ReadCountFromFile()
{
try
{
if (File.Exists(".\\mynumber.dat"))
{
using (var file = File.Open(".\\mynumber.dat", FileMode.Open))
{
byte[] bytes = new byte[4];
file.Read(bytes, 0, 4);
count = BitConverter.ToInt32(bytes, 0);
total = total - count;
Console.WriteLine("Total count left is = {0}", total);
Console.WriteLine("Limit = 10000");
Console.WriteLine("Count = {0}", count);
}
}
}
catch (Exception ex)
{
Console.WriteLine("Problem reading file.");
}
}
static void CurrentDomain_ProcessExit(Object sender, EventArgs e)
{
using (var file = File.Open(".\\mynumber.dat", FileMode.OpenOrCreate))
{
var buffer = BitConverter.GetBytes(count);
file.Write(buffer, 0, buffer.Length);
}
}
private static void aTimer_Elapsed(object source, ElapsedEventArgs e)
{
Console.WriteLine("Name is Yap {0}", e.SignalTime);
seconds += 5;
count += 1;
if (count>10000 || seconds == 86400)
{
aTimer.Enabled = false;
Console.WriteLine("\n\nTimer is off at {0}\n\n", e.SignalTime.TimeOfDay.ToString());
}
}
}
}
I modify your code and wrap your timer into a thread. I reduces the timer and count as well to make it easier to test. I'm sure there is a much better way to code it but this solution seems to work. you may need to adjust the thread sleep according to your need.
You can adjust when the process should stop and restart by playing with the condition
if (count > TOTAL || _processStart.AddSeconds(1) < DateTime.Now) )
in the function aTimer_Elapsed.
Currenlty the process restart if it has been running for more than 1s or the count is reach.
class Program
{
private static DateTime _processStart;
static int count = 1;
const int TOTAL = 15;
private static Timer aTimer;
private static Thread _process;
static void Main(string[] args)
{
_process = new Thread(DoProcess);
_process.Start();
Console.WriteLine("Press Enter To Exit The Program\n");
Console.ReadLine();
ProcessExit();
}
static void DoProcess()
{
_processStart = DateTime.Now;
ReadCountFromFile();
if (count < TOTAL)
{
Console.WriteLine("******START TIMER******");
aTimer = new Timer();
aTimer.Elapsed += aTimer_Elapsed;
aTimer.Interval = 500;
aTimer.Enabled = true;
while (aTimer.Enabled)
{
Thread.Sleep(1000);
}
Console.WriteLine("******END TIMER******");
ProcessExit();
DoProcess();
}
}
private static void ReadCountFromFile()
{
try
{
if (File.Exists(".\\mynumber.dat"))
{
using (var file = File.Open(".\\mynumber.dat", FileMode.Open))
{
byte[] bytes = new byte[4];
file.Read(bytes, 0, 4);
count = BitConverter.ToInt32(bytes, 0);
Console.WriteLine("Total count left is = {0} / Limit = {1} / Count = {2}", TOTAL - count, TOTAL, count);
}
}
}
catch (Exception ex)
{
Console.WriteLine("Problem reading file.");
}
}
static void ProcessExit()
{
using (var file = File.Open(".\\mynumber.dat", FileMode.OpenOrCreate))
{
var buffer = BitConverter.GetBytes(count);
file.Write(buffer, 0, buffer.Length);
}
}
private static void aTimer_Elapsed(object source, ElapsedEventArgs e)
{
//Console.WriteLine("Name is Yap {0}", e.SignalTime);
if (count < TOTAL)
{
count += 1;
Console.WriteLine("Count is {0}", count);
}
if (count > TOTAL || _processStart.AddSeconds(1) < DateTime.Now)
{
aTimer.Enabled = false;
Console.WriteLine("Timer is off at {0} count is {1}", e.SignalTime.TimeOfDay.ToString(),count);
}
}
}

Performance counter CPU usage for current process is more than 100

I want to display CPU usage for my multithread application (working over multicore processor). I want to receive numbers close to Task manager's. But I got numbers more than 100%. Even more than 500%. Yes, I know, than counter "% Processor Time" for category "Process" I need to divide into Environment.ProcessorCount or "NumberOfLogicalProcessors" (same for my configuration). And 500% is a result after this operation. I tested this example on different computers with different hardware (i7, i5, Core2) and software configurations (Windows 7 SP1 with all updates, Windows 2008 R2 SP1 with all updates) and got same problem.
public static class SystemInfo
{
private static Process _thisProc;
private static bool HasData = false;
private static PerformanceCounter _processTimeCounter;
private static void Init()
{
if (HasData)
return;
if (CheckForPerformanceCounterCategoryExist("Process"))
{
_processTimeCounter = new PerformanceCounter();
_processTimeCounter.CategoryName = "Process";
_processTimeCounter.CounterName = "% Processor Time";
_processTimeCounter.InstanceName = FindInstanceName("Process");
_processTimeCounter.NextValue();
}
MaximumCpuUsageForCurrentProcess = 0;
HasData = true;
}
private static bool CheckForPerformanceCounterCategoryExist(string categoryName)
{
return PerformanceCounterCategory.Exists(categoryName);
}
public static string FindInstanceName(string categoryName)
{
string result = String.Empty;
_thisProc = Process.GetCurrentProcess();
if (!ReferenceEquals(_thisProc, null))
{
if (!String.IsNullOrEmpty(categoryName))
{
if (CheckForPerformanceCounterCategoryExist(categoryName))
{
PerformanceCounterCategory category = new PerformanceCounterCategory(categoryName);
string[] instances = category.GetInstanceNames();
string processName = _thisProc.ProcessName;
if (instances != null)
{
foreach (string instance in instances)
{
if (instance.ToLower().Equals(processName.ToLower()))
{
result = instance;
break;
}
}
}
}
}
}
return result;
}
public static int CpuUsageForCurrentProcess
{
get
{
Init();
if (!ReferenceEquals(_processTimeCounter, null))
{
int result = (int) _processTimeCounter.NextValue();
result /= Environment.ProcessorCount; //NumberOfLogicalProcessors //same for me
if (MaximumCpuUsageForCurrentProcess < result)
MaximumCpuUsageForCurrentProcess = result;
return result;
}
return 0;
}
}
public static int MaximumCpuUsageForCurrentProcess { private set; get; }
}
and code to execute (you need to create windows forms application with two labeles, one BackgroundWorker and one button)
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
IList<Task> tasks = new List<Task>();
for (int i = 0; i < 10; i++)
{
Task t = new Task(() =>
{
do {
if (backgroundWorker1.CancellationPending)
break;
} while (true);
});
t.Start();
tasks.Add(t);
}
Task displayProgress = new Task(() => { do {
if (backgroundWorker1.CancellationPending)
break;
backgroundWorker1.ReportProgress(1);
Thread.Sleep(10);
} while (true); });
displayProgress.Start();
tasks.Add(displayProgress);
Task.WaitAll(tasks.ToArray());
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
label1.Text = SystemInfo.CpuUsageForCurrentProcess.ToString();
label2.Text = SystemInfo.MaximumCpuUsageForCurrentProcess.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = SystemInfo.CpuUsageForCurrentProcess.ToString();
if (backgroundWorker1.IsBusy)
backgroundWorker1.CancelAsync();
else
backgroundWorker1.RunWorkerAsync();
}
Please show me my error. And yes, I read this article and noticed that
“\Process(…)\% Processor Time” can go up to N*100 (where N is the number of CPUs) because it adds up the CPU usage of the requested process across all the CPUs.
This (somewhat related) question suggests using the System.Diagnostics.Process.TotalProcessorTime and System.Diagnostics.ProcessThread.TotalProcessorTime properties instead, for low overhead and easy implementation.
(Edit: Here's an article explaining how to use the properties, as well.)
Also, it looks like you're not waiting long enough between calls to "_processTimeCounter.NextValue()." As per the documentation, you're supposed to wait at least 1 second. Not sure if that would cause your strange numbers or not.

Categories

Resources