반응형
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 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int sum = 0; new LoadingForm(() => { for (int i = 0; i < 1000; i++) { sum += i; } Thread.Sleep(3000); Action act = () => this.label1.Text = sum.ToString(); if (this.InvokeRequired) this.BeginInvoke(act); else act(); }).ShowDialog(); } } } |
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 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp2 { public partial class LoadingForm : Form { private readonly MethodInvoker method; public LoadingForm(MethodInvoker action) { InitializeComponent(); this.Load += LoadingForm_Load; this.method = action; this.TopMost = true; this.StartPosition = FormStartPosition.CenterScreen; } private void LoadingForm_Load(object sender, EventArgs e) { new Thread(() => { method.Invoke(); InvokeAction(this, Dispose); }).Start(); } private void InvokeAction(LoadingForm control, MethodInvoker action) { if (control.InvokeRequired) control.BeginInvoke(action); else action(); } } } |
실행화면
'프로그래밍 > C#' 카테고리의 다른 글
[C#]DateTime간에 간격 구하기 (0) | 2017.11.02 |
---|---|
[C#]정규식 한글체크 (2) | 2017.05.23 |
[C#]SendKeys.Send (0) | 2017.05.16 |
[C#]픽셀의 RGB정보 가져오기 (0) | 2017.05.05 |
[C#]폼 중복 실행 방지 (0) | 2017.05.03 |