有什么方法可以停止 WPF 的 UI window 实例
Is there any way to stop the UI window instance of WPF
当我在服务器上发送数据并且服务器未启动时,我收到异常无法连接到服务器并且 UI window 实例在执行该行时关闭代码:
(响应 = 等待 client.PostAsJsonAsync(“windows/actions”,数据)。ConfigureAwait(假);)。我怎样才能停止 UI window 它不应该被关闭。
我的代码:
public static async void PostInfo(List<ElementProps> requestObj)
{
try
{
HttpResponseMessage response;
using (HttpClient client = new HttpClient())
{
// Setting Base address.
client.BaseAddress = new Uri("http://126.1.1.1:8888/");
// Setting content type.
client.DefaultRequestHeaders.Accept.Add(new
MediaTypeWithQualityHeaderValue("application/json"));
string list = JsonConvert.SerializeObject(requestObj);
object data = JsonConvert.DeserializeObject(list);
// HTTP POST ** Here is the error **
response = await client.PostAsJsonAsync("windows/actions", data).ConfigureAwait(false);
// Verification
if (response.IsSuccessStatusCode)
{
System.Windows.MessageBox.Show("Recording saved successfully!"); <br/>
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
ErrorLog.Log(ex);
}
}
我的主线程在 http 客户端异步操作完成之前退出。
我只是将 .Wait()
添加到调用者以维护主线程。所以,我有这个:
PostInfo(Records).Wait();
当我在服务器上发送数据并且服务器未启动时,我收到异常无法连接到服务器并且 UI window 实例在执行该行时关闭代码: (响应 = 等待 client.PostAsJsonAsync(“windows/actions”,数据)。ConfigureAwait(假);)。我怎样才能停止 UI window 它不应该被关闭。
我的代码:
public static async void PostInfo(List<ElementProps> requestObj)
{
try
{
HttpResponseMessage response;
using (HttpClient client = new HttpClient())
{
// Setting Base address.
client.BaseAddress = new Uri("http://126.1.1.1:8888/");
// Setting content type.
client.DefaultRequestHeaders.Accept.Add(new
MediaTypeWithQualityHeaderValue("application/json"));
string list = JsonConvert.SerializeObject(requestObj);
object data = JsonConvert.DeserializeObject(list);
// HTTP POST ** Here is the error **
response = await client.PostAsJsonAsync("windows/actions", data).ConfigureAwait(false);
// Verification
if (response.IsSuccessStatusCode)
{
System.Windows.MessageBox.Show("Recording saved successfully!"); <br/>
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
ErrorLog.Log(ex);
}
}
我的主线程在 http 客户端异步操作完成之前退出。
我只是将 .Wait()
添加到调用者以维护主线程。所以,我有这个:
PostInfo(Records).Wait();