ํผ ๋์์ธ์ ๊ฐ๋ณ๊ฒ 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#๋ ๋ณ๋ ฌ(๋น๋๊ธฐ์)์ด ๊ฐ๋ฅํ์ง๋ง.. ์ด๊ฒ ๋ด๋ถ์ ์ผ๋ก ์๋ฒฝํ ๋ณ๋ ฌ์ด๋ผ๊ณ ํ ์ ์์๊น?
๋ด๋ถ๊ตฌ์กฐ๊ฐ ๊ถ๊ธํด์ง๋๋ค.. ํ๊ตฌํ๊ฒ ๋๋ฉด ํฌ์คํ ์ ์ถ๊ฐํ๋๋ก ํ๊ฒ ์ต๋๋ค.
์๋ฌดํผ ๋น๋๊ธฐ์์ผ๋ก ๋ณด์ด๋ ๋น๋๊ธฐ ์ฒ๋ฆฌ ๋ฐฉ๋ฒ ํ์ธ ์์ ํ๋ก๊ทธ๋จ์ ์ค์ค๋ก ๋ง๋ค์ด๋ณด์์๋๋ค.