Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在web连接签入windows窗体应用程序时创建加载窗口?_C#_Web_Connection - Fatal编程技术网

C# 如何在web连接签入windows窗体应用程序时创建加载窗口?

C# 如何在web连接签入windows窗体应用程序时创建加载窗口?,c#,web,connection,C#,Web,Connection,我有一个c#的测试web连接表单。我想在检查连接时显示一个加载窗口,然后显示检查结果 这是我测试web连接的代码: public bool ConnectionAvailable(string strServer) { try { HttpWebRequest reqFP = (HttpWebRequest)HttpWebRequest.Create(strServer); HttpWebResponse

我有一个c#的测试web连接表单。我想在检查连接时显示一个加载窗口,然后显示检查结果

这是我测试web连接的代码:

   public bool ConnectionAvailable(string strServer)
   {
        try
        {
            HttpWebRequest reqFP = (HttpWebRequest)HttpWebRequest.Create(strServer);

           HttpWebResponse rspFP = (HttpWebResponse)reqFP.GetResponse();
            if (HttpStatusCode.OK == rspFP.StatusCode)
            {
              // HTTP = 200 - Internet connection available, server online
                rspFP.Close();
                return true;
            }
            else
           {
               // Other status - Server or connection not available
                rspFP.Close();
                return false;
            }
        }
        catch (WebException)
        {
            // Exception - connection not available
            return false;
        }
    }
这是:

    private void button1_Click(object sender, EventArgs e)
    {
        string url = "Web-url";
        label1.Text = "Checking ...";
        button1.Enabled = false;

        if (ConnectionAvailable(url))
        {
            WebClient w = new WebClient();
            w.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
            label1.Text = w.UploadString(url, "post", "SN=" + textBox1.Text);
            button1.Enabled = true;
        }
        else
        {
            label1.Text = "Conntion fail";
            button1.Enabled = true;
        }

    }

我在考虑穿线!一个线程检查连接,另一个线程显示加载窗口。例如,如果已建立连接,您可以通知另一个线程并显示结果。

在windows窗体应用程序上,用户界面在一个线程上运行,如果您尝试运行一个长时间运行的进程,检查web连接可能最终失败,这将导致窗体冻结,直到完成工作

因此,我将启动一个新线程来进行检查。然后引发一个事件以返回结果。当所有这些发生时,你可以通过用户界面做你喜欢的事情,比如加载图形,甚至允许用户继续使用不需要互联网连接的功能

创建自己的EventArgs类,以便可以传回结果:

public class ConnectionResultEventArgs : EventArgs
{
    public bool Available { get; set; }
}
然后在表单类中,创建事件、处理程序和方法,以便在事件到达时执行操作

//Create Event and Handler
    public delegate void ConnectionResultEventHandler(object sender, ConnectionResultEventArgs e);
    public event ConnectionResultEventHandler ConnectionResultEvent;

//Method to run when the event has been receieved, include a delegate in case you try to interact with the UI thread
    delegate void ConnectionResultDelegate(object sender, ConnectionResultEventArgs e);
    void ConnectionResultReceived(object sender, ConnectionResultEventArgs e)
    {
        //Check if the request has come from a seperate thread, if so this will raise an exception unless you invoke.
        if (InvokeRequired)
        {
            BeginInvoke(new ConnectionResultDelegate(ConnectionResultReceived), new object[] { this, e });
            return;
        }

        //Do Stuff
        if (e.Available)
        {
            label1.Text = "Connection Good!";
            return;
        }

        label1.Text = "Connection Bad";
    }
在窗体加载时订阅事件:

private void Form1_Load(object sender, EventArgs e)
    {
        //Subscribe to the the results event.
        ConnectionResultEvent += ConnectionResultReceived;
    }
然后设置工作线程:

//Check the connection
    void BeginCheck()
    {
        try
        {
            HttpWebRequest reqFP = (HttpWebRequest)HttpWebRequest.Create("http://google.co.uk");

            HttpWebResponse rspFP = (HttpWebResponse)reqFP.GetResponse();
            if (HttpStatusCode.OK == rspFP.StatusCode)
            {
                // HTTP = 200 - Internet connection available, server online
                rspFP.Close();

                ConnectionResultEvent(this, new ConnectionResultEventArgs {Available = true});
            }
            else
            {
                // Other status - Server or connection not available
                rspFP.Close();

                ConnectionResultEvent(this, new ConnectionResultEventArgs { Available = false });
            }
        }
        catch (WebException)
        {

            // Exception - connection not available
            //Raise the Event - Connection False
            ConnectionResultEvent(this, new ConnectionResultEventArgs { Available = false });
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //loading graphic, screen or whatever
        label1.Text = "Checking Connection...";

        //Begin the checks - Start this in a new thread
        Thread t = new Thread(BeginCheck);
        t.Start();
    }