首先贴代码吧using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Web;
using System.Threading;namespace WindowsApplication1
{
public partial class FrmMain : Form
{
public string url;
public int Intcount = 0;
public FrmMain()
{
InitializeComponent();
notifyIcon1.Visible = false;
}
private void button2_Click(object sender, EventArgs e)
{
Txtci.Text = "";
Intcount =0;
this.openFileDialog1.Filter = "*.txt|*.txt";
openFileDialog1.FileName = "";
DialogResult ok=openFileDialog1.ShowDialog();
if (ok==DialogResult.OK)
{
StreamReader sr = new StreamReader(openFileDialog1.FileName, Encoding.GetEncoding("GBK"));
string stringtemp;
stringtemp = sr.ReadLine();
Txtci.Text = stringtemp + "\r\n";
while (stringtemp != null)
{
if (stringtemp != null)
{
stringtemp = sr.ReadLine();
Txtci.Text += stringtemp + "\r\n";
Intcount++;
}
}
sr.Close();
label4.Text = Intcount.ToString()+"条";
progressBar1.Maximum = Intcount;
}
} private void button3_Click(object sender, EventArgs e)
{
this.Size = new Size(341, 255);
} private void button4_Click(object sender, EventArgs e)
{
this.Size = new Size(341, 225);
url = TxtUrl.Text;
} private void button1_Click(object sender, EventArgs e)
{
try
{
StreamReader sr = new StreamReader(openFileDialog1.FileName, Encoding.GetEncoding("GBK"));
string stringtemp;
stringtemp = sr.ReadLine();
Fcpost(stringtemp);
while (stringtemp != null)
{
stringtemp = sr.ReadLine();
if (stringtemp!=null)
{
Fcpost(stringtemp);
}
}
}
catch (Exception x)
{ MessageBox.Show(x.ToString());
} } private string Fcpost(string _temp)
{
// CookieContainer cookieContainer = new CookieContainer(); // 声明CookieContainer对象
string postData = "keyword=" + System.Web.HttpUtility.UrlEncode(_temp, Encoding.GetEncoding("GBK"));
string contentType = "application/x-www-form-urlencoded";
CookieContainer cc = new CookieContainer();
byte[] byteArray = Encoding.UTF8.GetBytes(postData); // 转化
HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create(new Uri(url));
/// webRequest2.CookieContainer = cc;
webRequest2.Method = "POST";
webRequest2.ContentType = contentType;
webRequest2.ContentLength = byteArray.Length;
Stream newStream = webRequest2.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length); //写入参数
newStream.Close();
HttpWebResponse response2 = (HttpWebResponse)webRequest2.GetResponse();
StreamReader sr2 = new StreamReader(response2.GetResponseStream(), Encoding.Default);
string text2 = sr2.ReadToEnd();
progressBar1.Value++;
if (progressBar1.Value == progressBar1.Maximum)
{
MessageBox.Show("提交完毕!");
progressBar1.Value = 0;
}
Thread.Sleep(3000);
return text2;
} private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
notifyIcon1.Visible = false; //设置托盘图标隐藏
this.ShowInTaskbar = true; //让窗体在任务栏中显示
this.WindowState = FormWindowState.Normal; //还原原来窗体
} private void FrmMain_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized) //判断是否最小化
{
this.Activate();
this.ShowInTaskbar = false; //不显示在系统任务栏
notifyIcon1.Visible = true; //托盘图标可见
notifyIcon1.ShowBalloonTip(300, "自动提交工具", "版权所有-海军", ToolTipIcon.Info);
}
}
}
} PS:
在提交Fcpost方法的时候 数据过去.我采用了主线程休眠Thread.Sleep(3000);
然后数据多了.提交速度快了 就程序假死了.但是提交还是在继续.只是页面什么都不能按了.
求解答.
如果用多线程做,应该能解决类似的问题么 ?
采用
Thread.Sleep(3000);方法不能限制提交的时间快慢么?
谢谢各位解答了
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Web;
using System.Threading;namespace WindowsApplication1
{
public partial class FrmMain : Form
{
public string url;
public int Intcount = 0;
public FrmMain()
{
InitializeComponent();
notifyIcon1.Visible = false;
}
private void button2_Click(object sender, EventArgs e)
{
Txtci.Text = "";
Intcount =0;
this.openFileDialog1.Filter = "*.txt|*.txt";
openFileDialog1.FileName = "";
DialogResult ok=openFileDialog1.ShowDialog();
if (ok==DialogResult.OK)
{
StreamReader sr = new StreamReader(openFileDialog1.FileName, Encoding.GetEncoding("GBK"));
string stringtemp;
stringtemp = sr.ReadLine();
Txtci.Text = stringtemp + "\r\n";
while (stringtemp != null)
{
if (stringtemp != null)
{
stringtemp = sr.ReadLine();
Txtci.Text += stringtemp + "\r\n";
Intcount++;
}
}
sr.Close();
label4.Text = Intcount.ToString()+"条";
progressBar1.Maximum = Intcount;
}
} private void button3_Click(object sender, EventArgs e)
{
this.Size = new Size(341, 255);
} private void button4_Click(object sender, EventArgs e)
{
this.Size = new Size(341, 225);
url = TxtUrl.Text;
} private void button1_Click(object sender, EventArgs e)
{
try
{
StreamReader sr = new StreamReader(openFileDialog1.FileName, Encoding.GetEncoding("GBK"));
string stringtemp;
stringtemp = sr.ReadLine();
Fcpost(stringtemp);
while (stringtemp != null)
{
stringtemp = sr.ReadLine();
if (stringtemp!=null)
{
Fcpost(stringtemp);
}
}
}
catch (Exception x)
{ MessageBox.Show(x.ToString());
} } private string Fcpost(string _temp)
{
// CookieContainer cookieContainer = new CookieContainer(); // 声明CookieContainer对象
string postData = "keyword=" + System.Web.HttpUtility.UrlEncode(_temp, Encoding.GetEncoding("GBK"));
string contentType = "application/x-www-form-urlencoded";
CookieContainer cc = new CookieContainer();
byte[] byteArray = Encoding.UTF8.GetBytes(postData); // 转化
HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create(new Uri(url));
/// webRequest2.CookieContainer = cc;
webRequest2.Method = "POST";
webRequest2.ContentType = contentType;
webRequest2.ContentLength = byteArray.Length;
Stream newStream = webRequest2.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length); //写入参数
newStream.Close();
HttpWebResponse response2 = (HttpWebResponse)webRequest2.GetResponse();
StreamReader sr2 = new StreamReader(response2.GetResponseStream(), Encoding.Default);
string text2 = sr2.ReadToEnd();
progressBar1.Value++;
if (progressBar1.Value == progressBar1.Maximum)
{
MessageBox.Show("提交完毕!");
progressBar1.Value = 0;
}
Thread.Sleep(3000);
return text2;
} private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
notifyIcon1.Visible = false; //设置托盘图标隐藏
this.ShowInTaskbar = true; //让窗体在任务栏中显示
this.WindowState = FormWindowState.Normal; //还原原来窗体
} private void FrmMain_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized) //判断是否最小化
{
this.Activate();
this.ShowInTaskbar = false; //不显示在系统任务栏
notifyIcon1.Visible = true; //托盘图标可见
notifyIcon1.ShowBalloonTip(300, "自动提交工具", "版权所有-海军", ToolTipIcon.Info);
}
}
}
} PS:
在提交Fcpost方法的时候 数据过去.我采用了主线程休眠Thread.Sleep(3000);
然后数据多了.提交速度快了 就程序假死了.但是提交还是在继续.只是页面什么都不能按了.
求解答.
如果用多线程做,应该能解决类似的问题么 ?
采用
Thread.Sleep(3000);方法不能限制提交的时间快慢么?
谢谢各位解答了
Txtci.Text += stringtemp + "\r\n";
这会触发无数次Paint(窗体绘制)用StringBuilder拼接字符串,然后一次性赋值给Txtci.Text
加一个Application.DoEvents();让程序相应消息,就不会假死了。 while (stringtemp != null)
{
stringtemp = sr.ReadLine();
if (stringtemp!=null)
{
Fcpost(stringtemp);
Application.DoEvents();
}
}
libinguest兄弟 进度条我做了的
{
Thread.Sleep(5000); // 这里换成楼主下载资源的循环过程。
// 此处不能直接操作和界面相关的控件
Invoke(new EventHandler(DoInvoke)); // 线程同步,访问窗体控件
}private void DoInvoke(object sender, EventArgs e)
{
button1.Enabled = true; // 线程同步,访问窗体控件,比如控件进度条
}private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
Thread vThread = new Thread(new ThreadStart(DoWork));
vThread.Start();
}
{
Thread.Sleep(5000); // 这里换成楼主下载资源的循环过程。
//这里写自己的循环代码吧?
Invoke(new EventHandler(DoInvoke)); // 线程同步,访问窗体控件
}
{
try
{
StreamReader sr = new StreamReader(openFileDialog1.FileName, Encoding.GetEncoding("GBK"));
string stringtemp;
stringtemp = sr.ReadLine();
Fcpost(stringtemp);
while (stringtemp != null)
{
stringtemp = sr.ReadLine();
if (stringtemp!=null)
{
Fcpost(stringtemp);
}
}
}
catch (Exception x)
{ //MessageBox.Show(x.ToString());
}
}Fcpost中访问去掉窗体控件直接访问 private string Fcpost(string _temp)
{
// CookieContainer cookieContainer = new CookieContainer(); // 声明CookieContainer对象
string postData = "keyword=" + System.Web.HttpUtility.UrlEncode(_temp, Encoding.GetEncoding("GBK"));
string contentType = "application/x-www-form-urlencoded";
CookieContainer cc = new CookieContainer();
byte[] byteArray = Encoding.UTF8.GetBytes(postData); // 转化
HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create(new Uri(url));
/// webRequest2.CookieContainer = cc;
webRequest2.Method = "POST";
webRequest2.ContentType = contentType;
webRequest2.ContentLength = byteArray.Length;
Stream newStream = webRequest2.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length); //写入参数
newStream.Close();
HttpWebResponse response2 = (HttpWebResponse)webRequest2.GetResponse();
StreamReader sr2 = new StreamReader(response2.GetResponseStream(), Encoding.Default);
string text2 = sr2.ReadToEnd();
/*progressBar1.Value++;
if (progressBar1.Value == progressBar1.Maximum)
{
MessageBox.Show("提交完毕!");
progressBar1.Value = 0;
}
Thread.Sleep(3000);*/
return text2;
}