본문 바로가기

C#

c# winform 특정 시간마다 thread 실행하기 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Net;
using System.IO;
 
namespace ConsoleApp4
{
    class Program
    {
        static void Main(string[] args)
        {
            Timer timer = new System.Timers.Timer();
            timer.Interval = 1000 * 10// 10초
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            timer.Elapsed += new ElapsedEventHandler(print_Woonggon);
            timer.Start();
 
            Console.WriteLine("Press enter to exit");
            Console.ReadLine();
 
        }
        static void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            WebClient web = new WebClient();
            string webpage = web.DownloadString("https://www.csharpstudy.com/Threads/timer.aspx");
            string time = DateTime.Now.ToString("yyyyMMdd_hhmmss");
 
            string outputFile = string.Format("page_{0}.html", time);
            File.WriteAllText(outputFile, webpage);
        }
        static void print_Woonggon(object sender, ElapsedEventArgs e)
        {
            Console.WriteLine("Hello woonggon");
        }
    }
}
 
cs