์ด๋ฒคํธ ๋ฐ์ดํฐ๋?!!?
public delegate void EventHandler(object sender, EventArgs e);
Public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e);
- public delegate void EventHandler(object sender, EventArgs e);
- ์ด๋ฒคํธ ๋ฐ์ดํฐ๊ฐ ์๋ ์ด๋ฒคํธ๋ฅผ ์ฒ๋ฆฌํ ๋ฉ์๋๋ฅผ ๋ํ๋
- Public delegate void EventHandler(object sender, TEventArgs e);
- ์ด๋ฒคํธ๊ฐ ๋ฐ์ดํฐ๋ฅผ ์ ๊ณตํ ๋ ์ด๋ฒคํธ๋ฅผ ์ฒ๋ฆฌํ ๋ฉ์๋๋ฅผ ๋ํ๋
- ์ด๋ฒคํธ ๋ฐ์ดํฐ๋ฅผ ์ค๋ช
ํ๊ธฐ ์ํด ๋๋ฒ์งธ ์ธ์๋ก ์๋
e
์ ๋ํด์ ์์๋ณด๋ฉด 1
๋ฒ ๋ฉ์๋์ ๊ฒฝ์ฐ ๋๋ฒ์งธ e
๊ฐ์ฒด๋ ์ด๋ฒคํธ์ ์ ๋ฌด๋ง์ ์๋ฆฌ๋ฉฐ ํด๋น ๊ฐ์ฒด๋ ๋ด์ฉ์ด ์๋ค. => ์ฆ, ๋น ๊ป๋ฐ๊ธฐ์ด๋ค.
2
๋ฒ ๋ฉ์๋์ ๊ฒฝ์ฐ ํด๋น e
๊ฐ์ฒด์ ๋ฐ์ดํฐ๊ฐ ์กด์ฌํ๋ฉฐ ์ด๋ ํด๋น ๋ฐ์ดํฐ๋ EventArgs
ํด๋์ค๋ฅผ ์์๋ฐ์์ผ ํ๋ค.
๋ฉํฐ์บ์คํธ ๋ธ๋ฆฌ๊ฒ์ดํธ => ์กฐ์ฌํด๋ณด๊ณ ์ ๋ฆฌํด๋ณด์
์ถ๊ฐ์ ์ผ๋ก ๋คํธ์ํฌ์์ ์ฌ์ฉํ๋ ๊ฐ๋
์ธ ์ ๋์บ์คํธ, ๋ฉํฐ์บ์คํธ, ๋ธ๋ก๋์บ์คํธ ๊ฐ๋
์ ๋ํด์๋ ๋ค์ ์ ๋ฆฌํด๋ณด์
delegate์ event ํค์๋
delegate
๋ ์ธ๋ถ์์๋ ํธ์ถ์ด ๊ฐ๋ฅํ๋ค.
event
ํค์๋๋ก ์ ์ธํ๋ฉด public
์ผ์ง๋ผ๋ ์ธ๋ถ์์ ํธ์ถ์ด ๋ถ๊ฐ๋ฅํ๋ค.
- ๋ฐ๋ผ์ ํ๋ก๊ทธ๋จ์ ๋ฐ๋ผ ํด๋น ํค์๋๋ค์ ์ฌ์ฉ์ ์ฃผ์ํด์ ๊ตฌ์กฐ๋ฅผ ์ง์ผํ๋ค.
reflection
=> ํด๋์ค์ ๋ฉํ๋ฐ์ดํฐ๋ฅผ ๋ฝ์๋ผ ์ ์๋ค๋ ๊ฐ๋
- ์คํ์ค์ธ ํ๋ก๊ทธ๋จ์์๋ ๋ฉํ๋ฐ์ดํฐ๋ฅผ ์ถ์ถ๊ฐ๋ฅํ๋ฉฐ private์ผ๋ก ์ ์ธ๋ ๋ฐ์ดํฐ๋ ๋ฝ์ ์ ์๋ค. (๋จ,
private
์ผ๋ก ์ ์ธ๋ ๊ฒฝ์ฐ์ ์กฐ๊ธ ์์ธํ ๋ด์ฉ์ ๊ฐ์ง๊ณ ์ถ์ถํด์ผ ๋ฐ์ดํฐ๋ฅผ ๋ฝ์ ์ ์๋ค.)
๋ค์ ๋ฐํ ํฌํจ ๋ด์ฉ๋ค
Sortingํ ๋ ๋ฐํ๊ฐ์ด 0๋ณด๋ค ํฐ๊ฐ, 0, 0๋ณด๋ค ์์๊ฐ์ผ๋ก ์ ๋ ฌ์ ํ๋ ์ด์ ๊ฐ ๋ฌด์์ผ๊น?
∩ ∩
- ํ์ฌ ์ด๊ฑฐ ์ง์ง ํ๋ฒ๋ง ๊ฐ๋ฅด์ณ ์ฃผ์๋ฉด ์๋ ๊น์ ?! (ใ
_ใ
)
⊂| |⊃
∪ ∪
Func์์ ๋ฐํํ์
์ Tuple(ํํ)๋ก ๋ฐํ๋ฐ์ ์ ์์๊น?
using System;
namespace FuncTest
{
class MainApp
{
static void Main(string[] args)
{
Func<int> func1 = () => 10;
// ์
๋ ฅ ๋งค๊ฐ๋ณ์๋ ์์ผ๋ฉฐ ๋ฌด์กฐ๊ฑด intํ๊ฐ์ธ 10์ ๋ฐํ
Console.WriteLine(func1()); // 10 ์ถ๋ ฅ
Func<int, int> func2 = (x) => x * 2;
//์
๋ ฅ ๋งค๊ฐ๋ณ์๋ int ํ์ ํ๋, ๋ฐํ ํ์๋ int
Console.WriteLine(func2(3)); // 6 ์ถ๋ ฅ
Func<int, int, int> func3 = (x, y) => x + y;
//์
๋ ฅ ๋งค๊ฐ๋ณ์๋ int ํ์ ๋, ๋ฐํ ํ์๋ int
Console.WriteLine(func3(2, 3)); // 5 ์ถ๋ ฅ
Func<int, int, (int, int)> func4 = (x, y) => (x + y, x - y);
//์
๋ ฅ ๋งค๊ฐ๋ณ์๋ int ํ์ ๋, ๋ฐํ ํ์๋ int
Console.WriteLine(func4(2, 3)); // (5,-1) ์ถ๋ ฅ
}
}
}
Output
10
6
5
(5, -1)
- ํ์ธํด๋ณด๋ Func์์ ๋ฐํํ์
์ ํํ๋ก ๋ฐํ๋ฐ์ ์ ์๋์!
์ฆ์์ฟผ๋ฆฌ & ์ง์ฐ์ฟผ๋ฆฌ ๋ฐ IQueryable & IEnumerable
Timer ์ FileSystemWatcher ๋ฅผ ์ด์ฉํ ์์ ์ค์ตํด๋ณด๊ธฐ!!
C#
์๋ ์ธ๊ฐ์ง Timer
๊ฐ ์๋ค.
1. System.Windows.Forms.Timer
- ์ฌ์ฉ์๊ฐ ์ ์ํ ๊ฐ๊ฒฉ๋ง๋ค ์ด๋ฒคํธ๋ฅผ ๋ฐ์์ํค๋ ํ์ด๋จธ๋ฅผ ๊ตฌํ
- ์ด ํ์ด๋๋ Windows Forms ์์ฉํ๋ก๊ทธ๋จ์์ ์ฌ์ฉํ ์ ์๋๋ก ์ต์ ํ ๋์์ผ๋ฉฐ ์ฐฝ์์ ์ฌ์ฉ
2. System.Threading.Timer
- ์ง์ ๋ ๊ฐ๊ฒฉ์ผ๋ก ๋ฉ์๋๋ฅผ ์คํํ๋ ๋ฉ์ปค๋์ฆ์ ์ ๊ณต
3. System.Timers.Timer
- ์์ฉ ํ๋ก๊ทธ๋จ์ ๋ํ์ด ์ด๋ฒคํธ๋ฅผ ์์ฑ
- ๋ค์์ผ๋ก
FileSystemWatcher
๊ฐ ์๋ค. ์ด๋ ํน์ ํด๋ ๊ฒฝ๋ก์ ๋ชจ๋ ํ์ผ์ ๋ํด ํ์ผ์ด ์์ฑ๋๊ฑฐ๋ ๋ณ๊ฒฝ๋๋ฉด ํจ์๋ฅผ ํธ์ถํด์ค๋ค.
FileSystemWatcher ์ฌ์ฉ๋ฐฉ๋ฒ
1. FileSystemWatcher ์์ฑ์ ํธ์ถ
2. ๊ฐ์ํ ํด๋ ์ค์ (Directory path)
3. ๊ฐ์ํ ํญ๋ชฉ๋ค ์ค์ (ํ์ผ ์์ฑ, ํฌ๊ธฐ, ์ด๋ฆ ๋ฑ๋ฑ)
4. ๊ฐ์ํ ํ์ผ ์ ํ ์ ํ => ์ด ๊ฒฝ์ฐ `*.*` = ๋ชจ๋ ํ์ผ
5. ๊ฐ์ํ ์ด๋ฒคํธ ์ค์ (์์ฑ, ๋ณ๊ฒฝ ...)
6. FileSystemWatcher ๊ฐ์ ๋ชจ๋ํฐ๋ง ํ์ฑํ
7. ๊ฐ์ํ ํด๋ ๋ด๋ถ ๋ณ๊ฒฝ์ event ํธ์ถ
Timer ์ฌ์ฉ ์์ ์ฝ๋
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
public class Example
{
private static Timer timer;
public static void Main(string[] args)
{
SetTimer();
Console.WriteLine("์ข
๋ฃํ๊ณ ์ถ์ผ๋ฉด Enter ํค๋ฅผ ๋๋ฅด์ธ์!");
Console.WriteLine("ํ์ฌ ์๊ฐ : {0:HH:mm:ss.ff}",DateTime.Now);
Console.ReadLine();
timer.Stop();
timer.Dispose();
Console.WriteLine("์ข
๋ฃ..");
Console.WriteLine();
}
private static void SetTimer()
{
timer = new Timer(5000);
//์ฃผ๊ธฐ 20์ด ์ค์
timer.Elapsed += alarm;
//alarmํจ์ ์ถ๊ฐ
timer.AutoReset = true;
timer.Enabled = true;
}
private static void alarm(Object source, ElapsedEventArgs e)
{
Console.WriteLine("์๋!! ์๋!! ์๋!! {0:HH:mm:ss.ff}",e.SignalTime);
}
}
Output
์ข
๋ฃํ๊ณ ์ถ์ผ๋ฉด Enter ํค๋ฅผ ๋๋ฅด์ธ์!
ํ์ฌ ์๊ฐ : 15:01:11.45
์๋!! ์๋!! ์๋!! 15:01:16.46
์๋!! ์๋!! ์๋!! 15:01:21.47
์๋!! ์๋!! ์๋!! 15:01:26.48
์๋!! ์๋!! ์๋!! 15:01:31.48
์๋!! ์๋!! ์๋!! 15:01:36.49
์ข
๋ฃ..
FileSystemWatcher ์ฌ์ฉ ์์ ์ฝ๋
using System;
using System.IO;
namespace MyNamespace
{
class MyClassCS
{
static void Main()
{
using var watcher = new FileSystemWatcher(@"C:\test");
//FileSystemWatcher ์์ฑ์ ํธ์ถ ๋ฐ ๊ฐ์ํ ํด๋ ์ค์
watcher.NotifyFilter = NotifyFilters.Attributes
| NotifyFilters.CreationTime
| NotifyFilters.DirectoryName
| NotifyFilters.FileName
| NotifyFilters.LastAccess
| NotifyFilters.LastWrite
| NotifyFilters.Security
| NotifyFilters.Size;
// ๊ฐ์ํ ํญ๋ชฉ๋ค => ํ์ผ ์์ฑ, ํฌ๊ธฐ, ์ด๋ฆ ๋ฑ๋ฑ
watcher.Changed += OnChanged;
watcher.Created += OnCreated;
watcher.Deleted += OnDeleted;
watcher.Renamed += OnRenamed;
watcher.Error += OnError;
// ๊ฐ์ํ ์ด๋ฒคํธ ์ค์ (์์ฑ, ๋ณ๊ฒฝ, ์ญ์ ....)
watcher.Filter = "*.txt";
// ๊ฐ์ํ ํ์ผ ์ ํ ์ ํ
watcher.IncludeSubdirectories = true;
watcher.EnableRaisingEvents = true;
Console.WriteLine("Press enter to exit.");
Console.ReadLine();
}
private static void OnChanged(object sender, FileSystemEventArgs e)
{
if (e.ChangeType != WatcherChangeTypes.Changed)
{
return;
}
Console.WriteLine($"Changed: {e.FullPath}");
}
private static void OnCreated(object sender, FileSystemEventArgs e)
{
string value = $"Created: {e.FullPath}";
Console.WriteLine(value);
}
private static void OnDeleted(object sender, FileSystemEventArgs e) =>
Console.WriteLine($"Deleted: {e.FullPath}");
private static void OnRenamed(object sender, RenamedEventArgs e)
{
Console.WriteLine($"Renamed:");
Console.WriteLine($" Old: {e.OldFullPath}");
Console.WriteLine($" New: {e.FullPath}");
}
private static void OnError(object sender, ErrorEventArgs e) =>
PrintException(e.GetException());
private static void PrintException(Exception? ex)
{
if (ex != null)
{
Console.WriteLine($"Message: {ex.Message}");
Console.WriteLine("Stacktrace:");
Console.WriteLine(ex.StackTrace);
Console.WriteLine();
PrintException(ex.InnerException);
}
}
}
}
Output
Changed: C:\test\asdfad.txt
Changed: C:\test\asdfad.txt
Deleted: C:\test\asdfad.txt
์ดํดํ๊ธฐ ์ด๋ ค์ ๋ ์ฝ๋ ~
using System;
using System.Threading;
using System.Threading.Tasks;
class Program
{
private static Timer timer;
//Timer ๊ฐ์ฒด
static void Main(string[] args)
{
var timerState = new TimerState { Counter = 0 };
timer = new Timer(callback: new TimerCallback(TimerTask), state: timerState, dueTime: 1000, period: 2000);
//Timer ์ธ์คํด์คํ => dueTime์ Timer ๊ฐ ์์ํ๊ธฐ ์ ๋๊ธฐ(์ง์ฐ)์๊ฐ์ด๋ฉฐ period๋ ๋ฐ๋ณต ์ฃผ๊ธฐ์ด๋ค.
while (timerState.Counter < 10)
{
Task.Delay(1000).Wait();
// 1์ด๋ฅผ ์ง์ฐ์ํจ๋ค while ์ ์ด๋ฌธ์์ ํ์ธํ ์ ์๋๋ก ํจ
// ์ด๋ ์์์
์ด ๋จ์์ ์ ์์ผ๋ฏ๋ก ํด๋น ์์
๋ค์ ๋ํด Wait()์ ์ํํ๋๋ก ํจ
}
timer.Dispose();
Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff}: done.");
}
private static void TimerTask(object timerState)
{
Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff}: starting a new callback.");
var state = timerState as TimerState;
Interlocked.Increment(ref state.Counter);
}
class TimerState
{
public int Counter;
}
}
Output
16:58:29.118: starting a new callback.
16:58:31.135: starting a new callback.
16:58:33.129: starting a new callback.
16:58:35.132: starting a new callback.
16:58:37.134: starting a new callback.
16:58:39.125: starting a new callback.
16:58:41.128: starting a new callback.
16:58:43.126: starting a new callback.
16:58:45.134: starting a new callback.
16:58:47.122: starting a new callback.
16:58:47.277: done.
LINQ ํ์ค ์ฐ์ฐ์ ์ฌ์ฉ ์์
using System;
using System.Linq;
namespace MinMaxAvg
{
class Profile
{
public string Name { get; set; }
public int Height { get; set; }
}
class MainApp
{
static void Main(string[] args)
{
Profile[] arrProfile =
{
new Profile() { Name="์ ์ฐ์ฑ" , Height = 186},
new Profile() { Name="๊นํํฌ" , Height = 158},
new Profile() { Name="๊ณ ํ์ " , Height = 172},
new Profile() { Name="์ด๋ฌธ์ธ" , Height = 178},
new Profile() { Name="ํํ" , Height = 171}
};
var profiles = arrProfile
.Where(profile => profile.Height < 175)
.OrderBy(profile => profile.Height)
.Select(profile => new
{
Name = profile.Name,
InchHeight = profile.Height * 0.393
});
Console.WriteLine("-------Linq ๋ฐํ ์ปฌ๋ ์
-------");
foreach (var profile in profiles)
{
Console.WriteLine("{0}, {1}", profile.Name, profile.InchHeight);
}
Console.WriteLine("\n-------FirstOrDefault-------");
Console.WriteLine(profiles.FirstOrDefault());
// ์ปฌ๋ ์
์ ์ฒซ๋ฒ์งธ ์์๋ฅผ ๋ฐํ, ๋ง์ผ ์ปฌ๋ ์
๋ด ์์๊ฐ ์๋ค๋ฉด ๊ธฐ๋ณธ๊ฐ์ ๋ฐํ
Console.WriteLine("\n-------ToList-------");
var ListProfiles = profiles.ToList();
foreach (var profile in ListProfiles)
{
Console.WriteLine("{0}, {1}", profile.Name, profile.InchHeight);
}
Console.WriteLine("\n-------ToDictionary-------");
var DicProfiles = profiles.ToDictionary(profile => profile.Name, profile => profile.InchHeight);
// Key ๋ก profile.Name์ ์ฌ์ฉ, Element๋ก InchHeight๋ฅผ ์ฌ์ฉ => (this source(์๋ต๊ฐ๋ฅ), KeySelector, ElementSelector)
for (int i = 0; i < DicProfiles.Count; i++)
{
Console.WriteLine(DicProfiles[ListProfiles[i].Name]);
}
}
}
}
Output
-------Linq ๋ฐํ ์ปฌ๋ ์
-------
๊นํํฌ, 62.094
ํํ, 67.203
๊ณ ํ์ , 67.596
-------FirstOrDefault-------
{ Name = ๊นํํฌ, InchHeight = 62.094 }
-------ToList-------
๊นํํฌ, 62.094
ํํ, 67.203
๊ณ ํ์ , 67.596
-------ToDictionary-------
62.094
67.203
67.596