2016-04-03 32 views
0
<%@ Page Language="c#" Debug="true" %> 
<%@ Import Namespace="System" %> 
<%@ Import Namespace="System.Net" %> 

<script language="c#" runat="server"> 

void BtnCheck_Click(Object sender,EventArgs e) { 
try 
{ 
    IPHostEntry GetIPHost = Dns.GetHostByName(Request.QueryString["domain"] + ".org"); 
    LblValue.Text += "<br>" + Request.QueryString["domain"] + ".org#"; 
    foreach(IPAddress ip in GetIPHost.AddressList) 
    { 
     long HostIpaddress = ip.Address; 
     LblValue.Text += ip.ToString() + "#"; 
    } 
} 
catch(Exception ex){ 
    LblValue.Text += "<br>" + Request.QueryString["domain"] + ".org#"; 
    LblValue.Text += "" + ex.Message + "#"; 
} 

} 
</script> 

<html> 
<title>DNS LOOKUP</title> 
<head> 
</head> 
<body OnLoad="BtnCheck_Click" value="Send" runat="server"> 
<asp:Label id="LblValue" runat="server" /> 
</body> 
</html> 

Bir dns araması yapmaya çalıştığımda, bilgi almak 3-4 saniyeden uzun sürebilir. Yükleme süresini 1000 milisaniyeye sınırlamak istiyorum. 1001 milisaniyeye veya daha fazlasına geçerse, try bloğundan çıkmak istiyorum. Bu kod için bir zamanlayıcıyı nasıl ekleyebilirim?Zaman aşımı, yükleme süresini sınırlamak için {} catch {} içinde asp.net

+0

Zaman aşımı ile 'try' bloğundan çıkarsa 'catch' bloğunu yürütmesini ister misiniz? – Enigmativity

+0

, yakalama bloğunun yanıtı gerekli değildir. Ben sadece bilgi edinme ** GetHostByName() ** 1000 milisaniye ile almak istiyorum, eğer herhangi bir bilgi dönemezse o zaman bir NULL sonuç tamam –

cevap

0

Bunu yapmak için WaitForExit uzantı yöntemini shown in this answer kullanabilirsiniz.

Dns.GetHostByName aramanızı Action ürününe sarın ve sonuç GetIPHost değişkenine atar ve uzantı yöntemini çağırır.

try 
{ 
    IPHostEntry GetIPHost = null; 
    var action = new Action(() => GetIPHost = Dns.GetHostByName(Request.QueryString["domain"] + ".org")); 
    action.WaitForExit(1000); // this will cancel the method after 1000 milliseconds 
    if (GetIPHost != null) 
    { 
     LblValue.Text += "<br>" + Request.QueryString["domain"] + ".org#"; 
     foreach (IPAddress ip in GetIPHost.AddressList) 
     { 
      long HostIpaddress = ip.Address; 
      LblValue.Text += ip.ToString() + "#"; 
     } 
    } 
} 
catch (Exception ex) 
{ 
    LblValue.Text += "<br>" + Request.QueryString["domain"] + ".org#"; 
    LblValue.Text += "" + ex.Message + "#"; 
} 

WaitForExit uzatma yöntemi: Bunun için Microsoft'un Reaktif Uzantıları kullanmak

public static class ActionExtentions 
{ 
    public static bool WaitForExit(this Action action, int timeout) 
    { 
     var cts = new CancellationTokenSource(); 
     var task = Task.Factory.StartNew(action, cts.Token); 
     if (Task.WaitAny(new[] { task }, TimeSpan.FromMilliseconds(timeout)) < 0) 
     { 
      cts.Cancel(); 
      return false; 
     } 
     else if (task.Exception != null) 
     { 
      cts.Cancel(); 
      throw task.Exception; 
     } 
     return true; 
    } 
} 
+0

sorumu eddited, tüm kodları yazdı benim .aspx sayfası Bu benim ilk asp.net ile çalışın, kodlarınızı kopyalayın ve komut dosyalarının içine yapıştırıp satırdaki hatayı alın * public static bool WaitForExit (bu eylem eylemi, int zaman aşımı) * –

+0

Hata nedir? Genel statik bir sınıf içinde uzantı yöntemlerini bildirmeniz gerekir, bu nedenle "public static class ActionExtentions" öğesini de eklediğinizden emin olun. –

1

. Sadece NuGet "Rx-Main". Sonra bunu yapabilirsiniz:

string hostName = Request.QueryString["domain"] + ".org"; 

IObservable<string> getIPAddresses = 
    from host in Observable.Start(() => Dns.GetHostByName(hostName)) 
    select String.Join("#", host.AddressList.Select(ip => ip.Address.ToString())); 

IObservable<string> getTimeout = 
    from x in Observable.Timer(TimeSpan.FromMilliseconds(1001)) 
    select ""; // Or Timeout message here 

string text = Observable.Amb(getIPAddresses, getTimeout).Wait(); 

LblValue.Text += "<br>" + hostName + "#"; 
LblValue.Text += text; 

bu işi yapmanın anahtarı temelde gözlenebilirleri ve bir değer kazanır üretmek için ilk bir ikisi çalıştırmayı dener Observable.Amb operatörüdür. Wait() daha sonra IObservable<string>'u string'a gözlemlenebilir tarafından üretilen son değere dayanarak döndürür ve gözlenebilirler sadece aldığınız tek bir değer üretir.

+0

Asp.net ile ilk denemem dediğim gibi, cevabınızı anlayamıyorumsa, bu kodları yukarıdaki kodlarıma kopyalayıp yapıştırıyorum, bu hatayı aldım; * CS0103: 'Gözlemlenebilir' adı içeriğin içinde geçerli değil * dilden ingilizceye çeviri gibi bir şey var THANKS –

+0

@ G.KISA - Sizce NuGet "Rx-Main"? Bunu yaptıktan sonra doğru ad alanlarını eklemeniz gerekir, ancak iyi çalışması gerekir. – Enigmativity

İlgili konular