C#async + metro mahapp进度环

时间:2017-11-29 08:22:13

标签: c# wpf asynchronous mahapps.metro

我需要在我的应用程序中进行响铃并且异步,因为GUI会冻结。 我尝试了不同的设置async / wait / task / task.run但是GUI仍然被冻结并且响铃没有显示,而主要和CS中的WaitRing.IsActive。 是的,我知道ping很快但是当地址IP离线时,GUI冻结了4-5秒。 当方法在MainForm中时,更容易应用async / await。从Class执行此操作时遇到问题,并且当mahapp的消息框需要等待时。

XAML:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Testin script</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
    <div>
    <label>
        Child</label>
    <input value="1" type="number" onchange="update_child(this);">
  </div>

  <div id="childs">
    <label>1 child</label>
    <input value="" type="number">
  </div>

  <script type="text/javascript">
    function update_child(me){
      var num_child = $(me)[0].value;
      var child = "";
      for(var i=0;i<num_child;i++){
        child += '<label>'+(i+1)+' child</label><input value="" type="number"><br>';
      }
      $("#childs")[0].innerHTML = child;
    }
  </script>
</body>
</html>

CS:

公共课DrukarkaPing {     public TextBox TextBox_Drukarki_IP {get;组; }

<Controls:ProgressRing x:Name="WaitRing" IsActive="False" Foreground="{DynamicResource AccentColorBrush}" />

主要

public async Task CheckPing(TextBox TextBox_Drukarki_IP)
{

    try
    {
        if (TextBox_Drukarki_IP.Text == "" || TextBox_Drukarki_IP.Text == " ")
        {
            return;
        }
        else
        {
            Ping PingZapytanie = new Ping();
            PingReply PingOdp = PingZapytanie.Send(TextBox_Drukarki_IP.Text);

            if (PingOdp.Status == IPStatus.Success)
            {
                TextBox_Drukarki_IP.Background = new SolidColorBrush(Colors.Green);
            }
            else
            {
            TextBox_Drukarki_IP.Background = new SolidColorBrush(Colors.Red);
            }
        }
    }
    catch (Exception e)
    {
        var window = Application.Current.Windows.OfType<MetroWindow>().FirstOrDefault();
        if (window != null)
            await window.ShowMessageAsync("Błąd!", e.Message);
        return;
    }

}

SecondCS:

private async void Button_Drukarki_Sprawdz_Click(object sender, RoutedEventArgs e)
{
    WaitRing.IsActive = true;

    TextBox_Drukarki_IPNS.Text = string.Join("", TextBox_Drukarki_IPNS.Text.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries));

    Drukowanie.Drukarka Sprawdz = new Drukowanie.Drukarka(TextBox_Drukarki_IPNS.Text);
    await Sprawdz.CheckCSV(TextBox_Drukarki_IP, TextBox_Drukarki_NS, TextBox_Drukarki_Salon, TextBox_Drukarki_Terminal, TextBox_Drukarki_Status, TextBox_Drukarki_Serwer,
    TextBox_Drukarki_Model, TextBox_Drukarki_Podlaczenie, TextBox_Drukarki_Lokalizacja);

    DrukarkaPing Sprawdznext = new DrukarkaPing();
    await Sprawdznext.CheckPing(TextBox_Drukarki_IP);

    WaitRing.IsActive = false;
}

1 个答案:

答案 0 :(得分:2)

您似乎误解了异步和等待背后的概念。

您执行的代码不会异步执行,因为它未包装在Task中。 如果您希望PING在后台运行,CheckPing方法应该如下所示。

public async Task CheckPing(TextBox TextBox_Drukarki_IP)
{
    try
    {
        var text = TextBox_Drukarki_IP.Text; // get TextBox.Text in UI thread

        if (!string.IsNullOrWhiteSpace(text))
        {
            //Now the Ping.Send is running in Background
            PingReply PingOdp = await Task.Run(() => 
            {
                 Ping PingZapytanie = new Ping();
                 return PingZapytanie.Send(text);
            });

            //This code is running on the UI Thread again (because you access a FrameworkElement)
            if (PingOdp.Status == IPStatus.Success)
            {
                TextBox_Drukarki_IP.Background = new SolidColorBrush(Colors.Green);
            }
            else
            {
                TextBox_Drukarki_IP.Background = new SolidColorBrush(Colors.Red);
            }
        }
    }
    catch (Exception e)
    {
        var window = Application.Current.Windows.OfType<MetroWindow>().FirstOrDefault();
        if (window != null)
            await window.ShowMessageAsync("Błąd!", e.Message);
        return;
    }
}

您应该check here获取有关该概念的一些信息