일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- pythoncom37.dll
- 리눅스
- Linux
- error
- Python
- Eclipse
- Custom
- Anaconda
- checkbox
- 원한
- ORA-28002
- JQuery
- SCADA
- geckodriver
- 말라키
- 가상환경
- 맥코트
- DataTables
- LOG
- STS
- 분노
- build
- HMI
- 파이썬
- 명령어
Archives
- Today
- Total
2010년 5월 1일, 2막
[c#] WORKING WITH ASYNCHRONOUS METHODS IN C# 본문
출처 : http://gigi.nullneuron.net/gigilabs/working-with-asynchronous-methods-in-c/
gigi.nullneuron.net
반환값 없는 경우
private async void Button_Click(object sender, RoutedEventArgs e)
{
await GetHtmlAsync();
}
private async Task GetHtmlAsync()
{
var baseAddress = new Uri("http://mta.com.mt");
using (var httpClient = new HttpClient() { BaseAddress = baseAddress })
{
var response = await httpClient.GetAsync("/");
var content = await response.Content.ReadAsStringAsync();
MessageBox.Show("Response arrived!", "Slow website");
}
}
반환값이 있는 경우
private async void Button_Click(object sender, RoutedEventArgs e)
{
string html = await GetHtmlAsync();
}
private async Task<string> GetHtmlAsync()
{
var baseAddress = new Uri("http://mta.com.mt");
using (var httpClient = new HttpClient() { BaseAddress = baseAddress })
{
var response = await httpClient.GetAsync("/");
var content = await response.Content.ReadAsStringAsync();
return content;
}
}
Summary
- Use async Task methods wherever you can.
- Use async void methods for event handlers, but beware the dangers even there.
- Use the –Async suffix in asynchronous method names to make them easily recognisable as awaitable.
- Use Task.CompletedTask and Task.FromResult() to satisfy asynchronous contracts with synchronous code.
- Asynchronous code in Main() has not been possible until recently. Use C# 7.1+ or a workaround to get this working.
'Computer > Tips' 카테고리의 다른 글
[C#] 트레이 아이콘 적용 (0) | 2020.07.19 |
---|---|
Git 사용법 관련 (0) | 2020.04.24 |
[c#] 시스템 트레이 프로그램 작성 (0) | 2019.08.23 |
[mysql] datetime 날짜 검색 (0) | 2019.08.07 |
[C#] 크로스 스레드와 Control.Invoke (0) | 2019.07.24 |