读书人

初学者有关问题,C#程序执行效率

发布时间: 2012-04-07 17:31:50 作者: rapoo

菜鸟问题,C#程序执行效率

C# code
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Net;using System.IO;namespace POST测试{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void BTN_POST_Click(object sender, EventArgs e)        {            TBX_RETURN.Text = PostWebRequest(TBX_URL.Text, TBX_DATA.Text, Encoding.ASCII );        }        private string PostWebRequest(string postUrl, string paramData, Encoding dataEncode)        {            string ret = string.Empty;            try            {                byte[] byteArray = dataEncode.GetBytes(paramData); //转化                HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));                webReq.Method = "POST";                webReq.ContentType = "application/x-www-form-urlencoded";                webReq.ContentLength = byteArray.Length;                Stream newStream = webReq.GetRequestStream();                newStream.Write(byteArray, 0, byteArray.Length);//写入参数                newStream.Close();                HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default);                ret = sr.ReadToEnd();                sr.Close();                response.Close();                newStream.Close();            }            catch (Exception ex)            {                MessageBox.Show(ex.Message);            }            return ret;        }    }}



第一次写C#程序,上面的代码应该没错误,已经编译通过了,只是执行的时候,第一次点击按钮程序会假死一段时间,然后返回结果,第二次点就没事儿了,是.NET程序都这样还是代码问题?

另外问一下,ICON设置的问题.
原来弄C++的MFC程序时,直接把图标放进资源里在简单调用一下就可以了.在C#里我实验了一下,程序图标和标题栏上的图标要选两次,生成出来的文件图标也加载了两次.不知道怎么搞.
就是说怎样让程序图标和标题栏的图标使用同一个资源.

[解决办法]
第二次没事,应该是有缓存,
正常情况下,对于httpwebrequest这种请求,都会有延迟,尤其你是在WINFORM下,必然会引发UI假死的情况,可以使用线程解决,
在控制台下就不会有这种情况

读书人网 >C#

热点推荐