Maybe I did not search correctly here in the forum because I did not find a similar problem.
Well, my problem is when I try to execute an async method inside a thread.
When I run the method (Register) without the thread it works perfectly! Below is an example of the scenario.
private SyncProcess _sync = new SyncProcess(); private static HttpClient _httpClient = new HttpClient(); private Thread _thread; public class SyncProcess : ISyncProcess { public event CompleteHandler OnComplete = delegate { }; // another properties ... public void Run() { // import rules // ... OnComplete(); } } public void TestImport() { Register(idsync, "start"); // here register works fine _sync.OnComplete += ImportComplete; _thread = new Thread(() => { try { _sync.Run(); } catch (Exception ex) { // not fall here } }); // _thread.Start(); } private void ImportComplete() { // other end-of-import rules // ... Register(idsync, "complete"); // here register not works } public async Task<string> Register(int idsync, string type) { string url = "myurl"; var stringContent = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("json", "myjson") }); var response = await _httpClient.PostAsync(url + type, stringContent); if (response.IsSuccessStatusCode) { // do something } return ""; } The problem occurs when I call the method (Register) inside the thread, another thing is that is that it does not generate error does not fall into the try, the debugging simply terminates. I've tried adding try code everywhere but never crashed in catch.
Debug always aborts on the following line:
var response = await _httpClient.PostAsync(url + type, stringContent);
What should I do in this case?
Updated the code returning string in the Register method, but the same error remains.
Thanks any suggestions!
httprequest, the return does not matter!