바람직한 코딩 방법

object로 여러 Device를 등록할 때 절대로 하지 말아야 할 것 : 한 Device에서 Event로 다른 Device에 참조하는 일. Device에서 Device로 데이터를 참조하고 싶으면 Communication Loop에서 Data 교환을 할것.


왜냐하면 Device들이 누가 먼저 등록되는지 정확히 알수가 없으므로 (먼저 참조되는 Device가 먼저 등록된다. 하지만 프로젝트가 커지면 먼저 참조되는 Device를 찾기가 힘들다) Device에서 Device로 event 참조를 하기 시작하면 나중에 아직 Instance가 생성되지 않은 Device를 참조하는 경우가 생기게 된다. 따라서 Communication Loop에서 모든 Device를 동시에 등록하고 프로그램을 시작하는 것이 바람직하다.



'Language > C#' 카테고리의 다른 글

public, private, protected, internal keyword  (0) 2018.10.24
async await 사용시 조심해야 할 점  (0) 2018.10.14
'await' keyword  (1) 2018.10.13
C# 'sealeld'  (0) 2018.10.09
메타데이터란?  (0) 2018.09.22
Posted by 나무길 :

'await' keyword

2018. 10. 13. 01:42 from Language/C#

await awaitable method return type

async await로 method를 만들면 Main Thread와 Sub Thread가 생성됨.


Main Thread는 await keyword를 만나면 async await method를 탈출함. Sub Thread는 await 다음에 나오는 awaitable method를 기다린다. 이때 awaitable method는 반환인자가 있으면 Task<Tresult> Method name (value) 이렇게 선언해야 한다. 


awaitable method를 기다린 다음 결과를 받을 때에는 아래처럼 반환인자 형태를 그대로 받아주면 된다. 

(DownloadWebSite declare : Task < WebsiteDateModel > DownloadWebSite (string site) )


private async Task RunDowloadAsync()

{

List <string> websites = PrepData();


foreach (string site in websites)

{

WebsiteDateModel results = await Task.Run( ( ) => DownloadWebSite (site) );

}

}



아래는 병렬적으로 여러개 파일을 받는 경우로써 Task.Run으로 awaitable method를 await로 기다리지 않고(기다리지 않았기 때문에 Task의 반환인자를 받는 것이 아니라 Task를 반환 받는다), 모든 Task가 완료되는 것을 await로 기다린다(이때는 기다렸다가 받는 것이므로 다시 Task의 반환인자를 반환 받는다).


private async Task RunDownload ParallelAsync()

{

List <string> websites = PrepData();


// 반환인자를 WeibsiteDateModel로 갖는 Task를 List로 만든다.

List <Task<WebsiteDateModel>> tasks = new List <Task<WebsiteDatemodel>>( );


foreach (string site in websites)

{

tasks.Add(Task.Run( ( ) => DownloadWebsite(site)));

}


var results = await Task.WhenAll(tasks);


foreach (var item in results)

{

ReportWeibsiteInfo(item);

}

}





Task Run ( ( ) => sync method )



'Language > C#' 카테고리의 다른 글

public, private, protected, internal keyword  (0) 2018.10.24
async await 사용시 조심해야 할 점  (0) 2018.10.14
Object Oriented Robot Programming  (0) 2018.10.14
C# 'sealeld'  (0) 2018.10.09
메타데이터란?  (0) 2018.09.22
Posted by 나무길 :

C# 'sealeld'

2018. 10. 9. 04:47 from Language/C#

Sealed concept

When applied to a class, the 'sealed' modifier prevent other classes from inheriting from it.



sealed Example

In the Following example, Z inherits from Y but Z cannot override the virtual function F that is declared in X and sealed in Y.


class X { protected virtual void F() { Console.WriteLine("X.F"); } protected virtual void F2() { Console.WriteLine("X.F2"); } } class Y : X { sealed protected override void F() { Console.WriteLine("Y.F"); } protected override void F2() { Console.WriteLine("Y.F2"); } } class Z : Y { // Attempting to override F causes compiler error CS0239. // protected override void F() { Console.WriteLine("Z.F"); } // Overriding F2 is allowed. protected override void F2() { Console.WriteLine("Z.F2"); } }






'Language > C#' 카테고리의 다른 글

public, private, protected, internal keyword  (0) 2018.10.24
async await 사용시 조심해야 할 점  (0) 2018.10.14
Object Oriented Robot Programming  (0) 2018.10.14
'await' keyword  (1) 2018.10.13
메타데이터란?  (0) 2018.09.22
Posted by 나무길 :