
폼 디자인은 가볍게 Button 1개와 글을 써줄 공간인 FlowLayoutPanel 1개를 넣어주자
* FlowLayoutPanel 은 무언가 동적으로 처리할 때 많이 사용함
예를 들면.. 버튼이나 라벨 같은 걸 동적으로 추가해줘야 하는 경우
왜냐면.. 자동 배치 기능(?)이 있기 때문.. *
이 것에 대해서는 차후에 포스팅을 하도록 해야겠다
<<Form1.Designer.cs>>
namespace Syn_example
{
partial class Form1
{
/// <summary>
/// 필수 디자이너 변수입니다.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 사용 중인 모든 리소스를 정리합니다.
/// </summary>
/// <param name="disposing">관리되는 리소스를 삭제해야 하면 true이고, 그렇지 않으면 false입니다.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form 디자이너에서 생성한 코드
/// <summary>
/// 디자이너 지원에 필요한 메서드입니다.
/// 이 메서드의 내용을 코드 편집기로 수정하지 마세요.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(97, 11);
this.button1.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(358, 62);
this.button1.TabIndex = 0;
this.button1.Text = "Button";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// flowLayoutPanel1
//
this.flowLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.flowLayoutPanel1.Location = new System.Drawing.Point(0, 99);
this.flowLayoutPanel1.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.flowLayoutPanel1.Name = "flowLayoutPanel1";
this.flowLayoutPanel1.Size = new System.Drawing.Size(562, 351);
this.flowLayoutPanel1.TabIndex = 3;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(562, 450);
this.Controls.Add(this.flowLayoutPanel1);
this.Controls.Add(this.button1);
this.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel1;
}
}
<<Form1.cs>>
using System;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Syn_example
{
public partial class Form1 : Form
{
int i = 1;
Random randomObj = new Random();
public Form1()
{
InitializeComponent();
//minute = randomObj.Next(1000, 10000);
}
private void button1_Click(object sender, EventArgs e)
{
Task delay = AsyncFunc();
}
public async Task AsyncFunc()
{
int minute = randomObj.Next(1000, 10000);
await Task.Delay(minute);
Label lbl = new Label();
lbl.Text = string.Format((i++) + "번째: " + (minute / 1000) + "초");
flowLayoutPanel1.Controls.Add(lbl);
minute = 0;
}
/*private async Task button1_ClickAsync(object sender, EventArgs e)
{
minute = randomObj.Next(1000, 10000);
//Delay(minute);
await Task.Delay(1000);
Label lbl = new Label();
lbl.Text = String.Format(i++ + "번째: " + (minute / 1000) + "초");
flowLayoutPanel1.Controls.Add(lbl);
}*/
/*private static DateTime Delay(int MS)
{
DateTime ThisMoment = DateTime.Now;
TimeSpan duration = new TimeSpan(0, 0, 0, 0, MS);
DateTime AfterWards = ThisMoment.Add(duration);
while (AfterWards >= ThisMoment)
{
System.Windows.Forms.Application.DoEvents();
ThisMoment = DateTime.Now;
}
return DateTime.Now;
}*/
}
}
변수를 어디서 지정해주고, 어디서 값을 입혀주냐에 따라 시스템 구동이 다르다는 것을 알게됨
마냥 전역변수가 무기는 아니다 라는 것도....
delay 함수를 만들어서 진행하였으나...
c#은 애초에 비동기 기반이 아니기에 뭔가 애매하게 기능구현이 되더라
나는 버튼 여러번 눌렀을 때 비동기적으로 처리해주고,
(몇 번 누르던 처리가 비동기적으로 stack에 쌓여있다가 처리 되어야 하는 case)
그래서 그냥 delay 함수를 호출하여 async 로 다시 구현.
만약 일시적으로 사용하거나 stack에 쌓여있어야 하는 구조가 아니고 delay를 주어야 한다면,
그냥 함수 사용이 더 간편할 것 같음..
생각보다 async가 이것저것 신경 써줘야할 것이 많더라 ... ** 추가 포스팅 하자
그래서 이 코드에는 2가지 방법이 다 들어 있다는 말씀...

c#도 병렬(비동기식)이 가능하지만.. 이게 내부적으로 완벽한 병렬이라고 할 수 있을까?
내부구조가 궁금해짐니다.. 탐구하게 되면 포스팅을 추가하도록 하겠습니다.
아무튼 비동기식으로 보이는 비동기 처리 방법 확인 예제 프로그램을 스스로 만들어보았읍니다.