Visual Studio c#互联网连接
我正在开发VisualStudio2010C#web表单应用程序。当应用程序断开互联网连接时,除了显示消息外,我还可以使用它。目前,当无法找到internet连接时,它会显示一个空白屏幕。在我的代码中,我有一些代码可以捕捉到通信断开,并显示“正在尝试连接…[{0}]”,以秒为单位倒计时。我遇到的问题是消息没有显示。谁能看出我错在哪里 我的MainForm.cs:Visual Studio c#互联网连接,c#,C#,我正在开发VisualStudio2010C#web表单应用程序。当应用程序断开互联网连接时,除了显示消息外,我还可以使用它。目前,当无法找到internet连接时,它会显示一个空白屏幕。在我的代码中,我有一些代码可以捕捉到通信断开,并显示“正在尝试连接…[{0}]”,以秒为单位倒计时。我遇到的问题是消息没有显示。谁能看出我错在哪里 我的MainForm.cs: using System; using System.Runtime.InteropServices; using System.Th
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace Dis_Browser
{
public partial class MainForm : Form
{
public MainForm()
{
if (!InternetExplorerBrowserEmulation.IsBrowserEmulationSet())
{
InternetExplorerBrowserEmulation.SetBrowserEmulationVersion();
}
InitializeComponent();
Cursor.Hide();
}
private void MainForm_Load(object sender, EventArgs e)
{
browser.DocumentTitleChanged += ValidateConnection;
//this.WindowState = FormWindowState.Maximized;
}
private bool offline;
private void ValidateConnection(object sender, EventArgs e)
{
if (offline)
return;
//if (browser.DocumentTitle == "Internet Explorer cannot display the webpage" || browser.DocumentTitle == "Navigation Canceled" || browser.DocumentTitle == "This page can’t be displayed" || browser.DocumentTitle == "Broadband Link - Error")
if (browser.DocumentTitle != string.Empty)
{
offline = true;
browser.Visible = false;
ThreadPool.QueueUserWorkItem(obj => WaitConnection());
return;
}
browser.Visible = true;
}
private int countdown = 20;
private void WaitConnection()
{
while (!Native.IsOnline("http://www.google.com"))
{
Invoke((MethodInvoker)(() => { connectLbl.Text = String.Format(" Trying to connect ...[{0}]", countdown--); }));
if (countdown == 0)
countdown = 20;
Thread.Sleep(TimeSpan.FromSeconds(5));
}
offline = false;
Invoke((MethodInvoker)(() =>
{
browser.Refresh();
browser.Visible = true;
countdown = 20;
}));
}
private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
}
private void connectLbl_Click(object sender, EventArgs e)
{
}
}
public class Native
{
[DllImport("wininet.dll", SetLastError = true)]
static extern bool InternetCheckConnection(string lpszUrl, int dwFlags, int dwReserved);
public static bool IsOnline(string url)
{
return InternetCheckConnection(url, 1, 0);
}
}
}
我的MainForm.Designer.cs
名称空间浏览器
{
部分类主形式
{
///
///必需的设计器变量。
///
private System.ComponentModel.IContainer components=null
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.browser = new System.Windows.Forms.WebBrowser();
this.connectLbl = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// browser
//
this.browser.Dock = System.Windows.Forms.DockStyle.Fill;
this.browser.Location = new System.Drawing.Point(0, 0);
this.browser.Margin = new System.Windows.Forms.Padding(0);
this.browser.MinimumSize = new System.Drawing.Size(1920, 1080);
this.browser.Name = "browser";
this.browser.ScriptErrorsSuppressed = true;
this.browser.ScrollBarsEnabled = false;
this.browser.Size = new System.Drawing.Size(1920, 1080);
this.browser.TabIndex = 0;
this.browser.Url = new System.Uri("http://www.google.com", System.UriKind.Absolute);
this.browser.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.browser_DocumentCompleted);
//
// connectLbl
//
this.connectLbl.Dock = System.Windows.Forms.DockStyle.Fill;
this.connectLbl.Font = new System.Drawing.Font("Microsoft Sans Serif", 48F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
this.connectLbl.Location = new System.Drawing.Point(0, 0);
this.connectLbl.MaximumSize = new System.Drawing.Size(1920, 1080);
this.connectLbl.MinimumSize = new System.Drawing.Size(1920, 1080);
this.connectLbl.Name = "connectLbl";
this.connectLbl.Size = new System.Drawing.Size(1920, 1080);
this.connectLbl.TabIndex = 1;
this.connectLbl.Text = " Trying to connect ...[20] Please check your Internet router";
this.connectLbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.connectLbl.Click += new System.EventHandler(this.connectLbl_Click);
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1920, 1080);
this.Controls.Add(this.browser);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.MaximumSize = new System.Drawing.Size(1920, 1080);
this.MinimumSize = new System.Drawing.Size(1920, 1080);
this.Name = "MainForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Dis Systems";
this.Load += new System.EventHandler(this.MainForm_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.WebBrowser browser;
private System.Windows.Forms.Label connectLbl;
}
}
//
///清理所有正在使用的资源。
///
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域Windows窗体设计器生成的代码
///
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
///
私有void InitializeComponent()
{
this.browser=new System.Windows.Forms.WebBrowser();
this.connectLbl=new System.Windows.Forms.Label();
这个.SuspendLayout();
//
//浏览者
//
this.browser.Dock=System.Windows.Forms.DockStyle.Fill;
this.browser.Location=新系统.Drawing.Point(0,0);
this.browser.Margin=新系统.Windows.Forms.Padding(0);
this.browser.MinimumSize=新系统图纸尺寸(19201080);
this.browser.Name=“browser”;
this.browser.ScriptErrorsSuppressed=true;
this.browser.ScrollBarsEnabled=false;
this.browser.Size=新系统.图纸.尺寸(1920,1080);
this.browser.TabIndex=0;
this.browser.Url=new System.Uri(“http://www.google.com“,System.UriKind.Absolute);
this.browser.DocumentCompleted+=新系统.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.browser\u DocumentCompleted);
//
//连接器
//
this.connectLbl.Dock=System.Windows.Forms.DockStyle.Fill;
this.connectLbl.Font=new System.Drawing.Font(“Microsoft Sans Serif”,48F,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point,((字节)(204)));
this.connectLbl.Location=新系统图点(0,0);
this.connectLbl.MaximumSize=新系统图纸尺寸(19201080);
this.connectLbl.MinimumSize=新系统图纸尺寸(19201080);
this.connectLbl.Name=“connectLbl”;
this.connectLbl.Size=新系统图纸尺寸(19201080);
this.connectLbl.TabIndex=1;
this.connectLbl.Text=“正在尝试连接…[20]请检查您的Internet路由器”;
this.connectLbl.TextAlign=System.Drawing.ContentAlignment.MiddleCenter;
this.connectLbl.Click+=new System.EventHandler(this.connectLbl\u Click);
//
//主要形式
//
此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=新系统图纸尺寸(19201080);
this.Controls.Add(this.browser);
this.FormBorderStyle=System.Windows.Forms.FormBorderStyle.None;
this.MaximumSize=新系统图纸尺寸(19201080);
此最小尺寸=新系统图纸尺寸(19201080);
this.Name=“MainForm”;
this.StartPosition=System.Windows.Forms.FormStartPosition.CenterScreen;
此.Text=“Dis系统”;
this.Load+=new System.EventHandler(this.MainForm\u Load);
此选项为.resume布局(false);
}
#端区
private System.Windows.Forms.WebBrowser浏览器;
private System.Windows.Forms.Label connectLbl;
}
}
我知道写这篇文章有很多方法,但这是我的尝试。非常感谢您抽出时间。是否可以切换到最新版本的visual Studio这是可能的,但这意味着要构建一台运行最新版本的新windows计算机。最新版本会使我的问题更容易解决吗。@k7s5a我已经在一台新电脑上安装了Visual Studio Community 2017。您能帮助我解决问题吗?或者我应该尝试在较新版本的VS中重建应用程序吗?谢谢。@k7s5a您好,我按照您的建议做了,现在正在运行Visual Studio 2017。你或其他人能帮我解决我的问题吗。非常感谢。请尝试并调试:是否调用了WaitConnection?是否将connectLbl.Text设置为任何值(由于某种原因未显示)?