Skip to content

Instantly share code, notes, and snippets.

@owlscatcher
Last active June 27, 2020 07:52
Show Gist options
  • Save owlscatcher/c3f8e1446a14d2df40871c458ae3a0de to your computer and use it in GitHub Desktop.
Save owlscatcher/c3f8e1446a14d2df40871c458ae3a0de to your computer and use it in GitHub Desktop.
Пример реализации таймера, который должен запускать метод в определенное время
using System;
using System.Diagnostics;
using System.Threading;
namespace Timer_test
{
class Program
{
static Process myProcess = Process.GetCurrentProcess();
static long peakPagedMem = 0,
peakWorkingSet = 0,
peakVirtualMem = 0;
static void Main(string[] args)
{
Thread thread = new Thread(TimerThread)
{
IsBackground = true,
Name = "TimerThread"
};
thread.Start();
Console.ReadLine();
}
private static void TimerThread()
{
System.Timers.Timer timer = new System.Timers.Timer()
{
Enabled = true,
Interval = 3600000,
AutoReset = true
};
timer.Elapsed += Timer_Elapsed;
timer.Start();
}
private static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//if (DateTime.Now.Hour == 6 && DateTime.Now.Minute == 00 && DateTime.Now.Second == 00)
Print();
}
private static void Print()
{
Console.WriteLine("\n" + DateTime.Now.ToString() + "\n");
Console.WriteLine($"{myProcess} -");
Console.WriteLine("-------------------------------------");
Console.WriteLine($" Physical memory usage : {myProcess.WorkingSet64}");
Console.WriteLine($" Base priority : {myProcess.BasePriority}");
Console.WriteLine($" Priority class : {myProcess.PriorityClass}");
Console.WriteLine($" User processor time : {myProcess.UserProcessorTime}");
Console.WriteLine($" Privileged processor time : {myProcess.PrivilegedProcessorTime}");
Console.WriteLine($" Total processor time : {myProcess.TotalProcessorTime}");
Console.WriteLine($" Paged system memory size : {myProcess.PagedSystemMemorySize64}");
Console.WriteLine($" Paged memory size : {myProcess.PagedMemorySize64}");
// Update the values for the overall peak memory statistics.
peakPagedMem = myProcess.PeakPagedMemorySize64;
peakVirtualMem = myProcess.PeakVirtualMemorySize64;
peakWorkingSet = myProcess.PeakWorkingSet64;
Console.WriteLine();
// Display peak memory statistics for the process.
Console.WriteLine($" Peak physical memory usage : {peakWorkingSet}");
Console.WriteLine($" Peak paged memory usage : {peakPagedMem}");
Console.WriteLine($" Peak virtual memory usage : {peakVirtualMem}");
}
}
}
@owlscatcher
Copy link
Author

Как посмотреть потребляемую память процессом: ссылка на msdn 1, ссылка на msdn 2
Как использовать таймеры: ссылка на msdn

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment