2016년 8월 30일 화요일

c# asnyc await example Simple notation



private void button1_Click(object sender, EventArgs e)
{
Console.WriteLine("in ");
method1();

//Thread.Sleep(1000);
Console.WriteLine( "out");

}

async void method1()
{
Console.WriteLine("in method1");
await Task.Run(() => awmethod() );
Console.WriteLine("out method1");
}

void awmethod()
{
Thread.Sleep(3000);
Console.WriteLine("Here Here");
}

----------------------




private void button1_Click(object sender, EventArgs e)
{
Console.WriteLine("in ");
method1();

//Thread.Sleep(1000);
Console.WriteLine( "out");

}

async void method1()
{
Console.WriteLine("in method1");
await getmethod();
Console.WriteLine("out method1");
}

void awmethod()
{
Thread.Sleep(3000);
Console.WriteLine("Here Here");
}

Task getmethod()
{
Task task = Task.Run(() => awmethod());
return task;
}

----------------------



private void button1_Click(object sender, EventArgs e)
{
Console.WriteLine("in ");
method1();

//Thread.Sleep(1000);
Console.WriteLine( "out");

}

async void method1()
{
Console.WriteLine("in method1");
await getmethod();
Console.WriteLine("out method1");
}

void awmethod()
{
Thread.Sleep(3000);
Console.WriteLine("Here Here");
}

Task getmethod()
{
Task task = new Task(() => awmethod());
task.Start();

return task;
}

c# await 로 비동기 Example

public async void TempSaveGrabData(byte[] inputArr, int buffNum, int scanNum)
{
await TempSaveMethodTask(inputArr,buffNum,scanNum);
Console.WriteLine( "{0} Scan and {1} Buff is Save in C:", scanNum,buffNum);
}

public Task TempSaveMethodTask(byte[] inputArr, int buffNum, int scanNum)
{
Task tempSaveTask = new Task(() => TempSaveMethod(inputArr, buffNum, scanNum));
return tempSaveTask;
}

void TempSaveMethod(byte[] inputArr, int buffNum, int scanNum)
{
string path = "c:\\ImageTemp\\" + scanNum.ToString("D3") + "_" + buffNum.ToString("D3") + ".dat";
Stream outStream = new FileStream(path, FileMode.Create);
outStream.Write(inputArr, 0, inputArr.Length);
outStream.Dispose();
}


여기선 데이터를 받으면 데이터를 하드에 저장을 하는데, 이 저장을 하는 작업을 하는동안 메인 쓰레드가 안멈추길 바란다.
await 로 쓰는동안 제어권을 호출한 메소드에 다시 넘겨준다.

c# Task async await example 2

private async void button1_Click(object sender, EventArgs e)
{

int result = await inttask();
label1.Text = result.ToString();
}

public async Task inttask()
{
Task temptak = new Task(method);
temptak.Start();
int output = await temptak;
return output;
}

int method()
{
Thread.Sleep(4000);
return 999;
}


4초동안 계산을 하는데, 윈폼은 활성화 되있는 상대고, 계산이 끝나면 라벨에 결과가 표시가 된다.

-> 다음은 마우스 클릭 이벤트를 계산이 끝나기 전에 눌렀을떄 어떻게 되나 보자.

private async void button1_Click(object sender, EventArgs e)
{
Console.WriteLine("event out");
int result = await inttask();
label1.Text = result.ToString();
Console.WriteLine("Txt change");
}

public async Task inttask()
{
Task temptak = new Task(method);
temptak.Start();
int output = await temptak;
return output;
}

int ttt = 100;
int method()
{
ttt = ttt * 2;
Thread.Sleep(4000);
return ttt;
}

보면 큐에 쌓인다.

5번 누르면 5번 결과가 바뀐다. 하지만 누른 시간차만큼 바뀌는데 시간이 걸림.

c# Task Async Await Basic Example




private void button1_Click(object sender, EventArgs e)
{
Mymethodasync();
}

public async Task Mymethodasync()
{
Console.WriteLine("1");
Task llrr = longrun();
Console.WriteLine("2");
int output = await llrr;
Console.WriteLine("3");

}

public async Task longrun()
{
Console.WriteLine("longrun Task");
await Task.Delay(1000);
Console.WriteLine("longrun Task Done");
return 1;
}

2016년 8월 23일 화요일

c# Event

http://mrw0119.tistory.com/21

좋은 설명

C# 여러개의 매소드를 순차적으로 실행할때


만약 각 매소드들의 매개변수가 같으면 델리게이트 체인을 이용한다.

EX)

public class testclass
{
public delegate int calc(int a, int b); // 델리게이트 선언 (클래스와같은 형선언, 단지 매개변수만 형태 지정이 가능하다.)

public int chain()
{
calc deleInstance = new calc(method1); // 단순히 델리게이트지정

calc chaindele = (calc)Delegate.Combine(new calc(method2),new calc(method3)); // 델리게이트 체인 지정으로 매소드2,3 이 순차적으로 실행된다.

int output = caindele(20,10);

}


public int method1(int input, int input2)
{
int output = input + input2;
return output;
}

public int method2(int input, int input2)
{
int output = input * input2;
return output;
}
public int method3(int input, int input2)
{
int output = input - input2;
return output;
}




}

c# Assertion

http://illef.tistory.com/entry/Beyond-the-Assertion-Code-Contract-1