Created
          August 7, 2025 10:04 
        
      - 
      
 - 
        
Save opentechnologist/0215d42e477dc1f5e7deaa7ddc113d43 to your computer and use it in GitHub Desktop.  
    a tiny c# project that shows the use of the system timer class that periodically calls a class method after a specified amount of time.
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | using System; | |
| using System.Timers; | |
| namespace MainPackage | |
| { | |
| class MainClass | |
| { | |
| private static Timer timer; | |
| static void Main(string[] args) | |
| { | |
| timer = new Timer(1000); // 1 second interval | |
| timer.Elapsed += TimerElapsed; | |
| timer.AutoReset = true; | |
| timer.Start(); | |
| Console.WriteLine("Press any key to exit..."); | |
| Console.ReadKey(); | |
| // Stop timer after keypress before exit | |
| timer.Stop(); | |
| timer.Dispose(); | |
| Console.WriteLine("Goodbye."); | |
| } | |
| private static void TimerElapsed(object sender, ElapsedEventArgs e) | |
| { | |
| Console.WriteLine("{0:d} {0:T}", DateTime.Now); | |
| } | |
| } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <?xml version="1.0" encoding="utf-8"?> | |
| <Project | |
| xmlns="http://schemas.microsoft.com/developer/msbuild/2003" | |
| Sdk="Microsoft.NET.Sdk" | |
| DefaultTargets="Build" | |
| > | |
| <PropertyGroup> | |
| <AssemblyName>main</AssemblyName> | |
| <OutputPath>bin\</OutputPath> | |
| </PropertyGroup> | |
| <ItemGroup> | |
| <Compile Include="main.cs" /> | |
| </ItemGroup> | |
| <Target Name="Build" Inputs="@(Compile)" Outputs="$(OutputPath)$(AssemblyName).exe"> | |
| <MakeDir Directories="$(OutputPath)" Condition="!Exists('$(OutputPath)')" /> | |
| <Csc Sources="@(Compile)" OutputAssembly="$(OutputPath)$(AssemblyName).exe"> | |
| <Output TaskParameter="OutputAssembly" ItemName="OutputFile" /> | |
| </Csc> | |
| <Message Text="Generated file: @(OutputFile)" Condition="Exists('@(OutputFile)')" /> | |
| </Target> | |
| <Target Name="Clean" > | |
| <Delete Files="$(OutputPath)$(AssemblyName).exe" /> | |
| </Target> | |
| <Target Name="Rebuild" DependsOnTargets="Clean;Build" /> | |
| </Project> | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment