Microsoft MVP성태의 닷넷 이야기
선생님 안녕하세요 async await 교착 관련 질문입니다 [링크 복사], [링크+제목 복사],
조회: 8745
글쓴 사람
ckm (dktkwkak03 at gmail.com)
홈페이지
첨부 파일
 

아래 교착이 발생하는 코드입니다
코드 맥락상 교착이 발생할만한 부분이 없는 것 같은데 왜 교착이 발생하는 걸까요?
MainLoginForm.activeNumber 정적 필드를 제거하니 코드가 정상적으로 호출되긴 하는데
MainLoginForm.activeNumber 때문은 아닌 것 같아서요
안내해 주시면 정말 감사하겠습니다

MainForm.cs
private bool isRunningHeartbeat;

private void MainForm_Load(object sender, EventArgs e)
{
    Task.Run(() => HeartBeatSender.SendHeartbeat());
    this.isRunningHeartbeat = true;
}


HeartBeatSender.cs
class HeartBeatSender
{
    public static async void SendHeartbeat()
    {
        while (true)
        {
            try
            {
                Dictionary<string, string> body = Cipher.GetHeartbeatEncryptedData("kk", "KakaoMap", MainLoginForm.activeNumber);
                HttpContent content = new FormUrlEncodedContent(body);

                string result = await Client.PostAsync("https://ckmauth.com/new/heartbeat", content);
                WriteLine(Thread.CurrentThread.ManagedThreadId); // 호출 안 됨
            }
            catch (Exception ex)
            {
                // 예외로도 안 빠짐
                MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace);
                await Task.Delay(5000);
            }
        }
    }
}


Cipher.cs
public static Dictionary<string, string> GetHeartbeatEncryptedData(string id, string programType, string activeNumber)
{
    using (Aes aes = Aes.Create())
    {
        aes.KeySize = 128;
        aes.IV = Encoding.UTF8.GetBytes("ckminitialvector");
        aes.Key = Encoding.UTF8.GetBytes("ckminitialkeyckm");

        return new Dictionary<string, string>()
        {
            { "i1", EncryptStringToBase64_Aes(id, aes.Key, aes.IV) },
            { "i2", EncryptStringToBase64_Aes(programType, aes.Key, aes.IV) },
            { "i3", EncryptStringToBase64_Aes(activeNumber, aes.Key, aes.IV) },
        };
    }
}


Client.cs
public static async Task<string> PostAsync(string url, HttpContent paylaod)
{
    try
    {
        using (HttpResponseMessage response = await client.PostAsync(url, paylaod)) // PostAsnyc 호출 후 교착 발생
        {
            WriteLine(Thread.CurrentThread.ManagedThreadId); // 여기부터 호출 안 됨
            string result = await response.Content.ReadAsStringAsync();
            return result;
        }
    }
    catch (Exception ex)
    {
        // 예외로도 안 빠짐
        MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace);
    }

    return null;
}


MainLoginForm.cs
partial class MainLoginForm : Form
{
    public static string activeNumber = "2"
}








[최초 등록일: ]
[최종 수정일: 7/16/2024]


비밀번호

댓글 작성자
 



2024-07-16 03시53분
조각 코드 말고 재현 코드("https://www.sysnet.pe.kr/2/0/11452")를 올려주세요.

일례로, 위와 같은 식으로 Task.Run에서 while 루프를 돌며 http 호출을 했다고 해서 일반적으로는 교착 상태에 빠지진 않을 것입니다. 그러니 제가 저 코드만 가지고 답변할 수 없습니다.
정성태

... 46  47  48  49  50  51  52  53  54  55  56  [57]  58  59  60  ...
NoWriterDateCnt.TitleFile(s)
1241(non...3/22/201421022(글쓴이의 요청으로 삭제합니다.) [4]
1240이석주3/21/201423660인터넷 익스플로러가 hang이 걸리는 현상 문의 [1]파일 다운로드1
1238(non...3/13/201417569(글쓴이의 요청으로 삭제합니다.) [2]
1237(non...3/11/201417638(글쓴이의 요청으로 삭제합니다.) [2]
1236(non...3/11/201418471(글쓴이의 요청으로 삭제합니다.) [2]
1235(non...3/10/201417570(글쓴이의 요청으로 삭제합니다.) [2]
1234(non...3/10/201420718(글쓴이의 요청으로 삭제합니다.) [3]
1233(non...3/9/201418422(글쓴이의 요청으로 삭제합니다.) [4]
1232(non...3/8/201417324(글쓴이의 요청으로 삭제합니다.) [2]
1231(non...3/7/201418616(글쓴이의 요청으로 삭제합니다.) [9]
1230POCO3/7/201418967쓰레드 안에서 DependencyProperty get, set시 또 다른 스레드 오류.. [1]
1229(non...3/6/201419586(글쓴이의 요청으로 삭제합니다.) [11]
1228POCO3/6/201417662안녕하세요. 질문이 있습니다. [1]
1226김형진3/4/201427821안녕하세요 windows azure에 관해 질문했던 사람입니다. [2]
1224(non...3/3/201423159(글쓴이의 요청으로 삭제합니다.) [11]
1223sadf...3/3/201417725아래 질문에 답변 감사드립니다. 한가지 더 궁금한점이 있어 질문드립니다. [1]
1222(non...3/2/201418189(글쓴이의 요청으로 삭제합니다.) [4]
1221(non...3/1/201418565(글쓴이의 요청으로 삭제합니다.) [2]
1220Until2/28/201416865질문드립니다. [1]
1219이성환2/28/201416333string.Join()과 Enumerable.Aggregate()의 차이가 궁금합니다. [2]파일 다운로드1
1218김형진2/25/201418129안녕하세요. window azure에 대해서 질문이 있어서 문의 드립니다 [4]
1217(non...2/23/201418724(글쓴이의 요청으로 삭제합니다.) [1]
1215아리수2/20/201422155C# 공부하면서 WPF에 대한 질문. [2]
1214조광훈2/20/201420189IIS8 응용프로그램 풀 관련 질문 드립니다. [2]파일 다운로드1
1213김태훈2/17/201417352가상화 프로그램 질문입니다. [1]파일 다운로드1
1212조광훈2/13/201416140ISAPI 필터에서 커스텀 헤더 정보 추가 [1]파일 다운로드1
... 46  47  48  49  50  51  52  53  54  55  56  [57]  58  59  60  ...