본문 바로가기

C#

(13)
c# countdownevent로 여러 스레드가 조건을 만족했을 시 메인 스레드 통제 == 코드 == 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 using System; using System.Threading; using System.Collections.Generic; namespace TestMultiThreadLock { class Program { static CountdownEvent countEvent = new CountdownEvent(5); static void Main(string[] args) { for (int i=0; i 0) { countEvent.Signal(); Console.WriteLine("{0}: 투표", id); } ..
c# autoresetevent로 스레드 통제하기 autoresetevent에서 set과 waitone 명령어로 스레드의 동작을 재개/중지가 가능하다 set을 보내면 waitone 밑의 코드가 실행된다. 동시에 autoresetevent의 상태는 false가 되어서 set 명령어가 다시 실행되어야만 waitone 밑의 코드가 실행되게 되어있다. 이는 manualresetevent 객체와 다른점이다. manualresetevent는 set을 하면 manualresetevent의 상태는 true가 되고, 명시적으로 reset 명령어를 해 주어야만 manualresetevent의 상태는 false가 된다. lock 명령어로 queue를 사용할 시 하나의 스레드만 접근 가능하도록 만든다. main thread 또한 추가적으로 접근이 가능하기에 그렇다. == 코..
c# 제한된 스레드 수만 접근 가능한 세마포어 ==코드== Semaphore(int initial threads, int maximun threads)이다. 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 42 43 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; namespace ConsoleApp6 { class Program { static void Main(string[] args) { va..
c# wait과 pulse를 활용한 스레드간 신호전달 === 코드 === 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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; namespa..
c# thread lock 명령어로 critical section 조절하기 ==코드== 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 42 43 44 45 46 47 48 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; namespace ConsoleApp5 { class Program { static void Main(string[] args) { LockThread lockthread = new LockThread(); ..
c# winform UI스레드, Worker스레드 원칙적으로 c#에서는 UI스레드에서만 UI를 갱신할 수 있지 Worker스레드에서 UI스레드의 클래스들(label box, text box,..) 같은 것들을 갱신할 수 없다. label box 같은 클래스는 자체적으로 UI스레드에서 온 명령인지 worker 스레드에서 온 명령인지 InvokeRequired 명령어로 체크한다. 만약 labelbox1.InvokeRequired가 true라면, BeginInvoke(new Action(() => label1.Text = data));와 같은 델리게이트를 넘겨 주어서 UI스레드를 갱신할 수 있다. == 코드 == 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 3..
c# winform 특정 시간마다 thread 실행하기 코드 1234567891011121314151617181920212223242526272829303132333435363738394041using 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.Elaps..
c# winform timer 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 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace timer { public partial class Form1 : Form { public Form1() { InitializeComponent(); ..