110,535
社区成员
发帖
与我相关
我的任务
分享
private void button2_Click(object sender, EventArgs e)
{
ThreadStart ths = new ThreadStart(WaterAndThumbnail);
th = new Thread(ths);
th.IsBackground = true;
th.Start();
}
private void WaterAndThumbnail()
{
int total = this.listView1.Items.Count;
int ff = 0;
string UserName = string.Empty;//文件名
string FileExten = string.Empty;
string FileName=string.Empty;
string FullPath=@"\\192.168.1.31\xftd2009\UPLOAD\CompanyFiles\";
string tempFile=string.Empty;
string locaPath = @"C:\Documents and Settings\wangjun\桌面\test\";
while(ff<total)
{
UserName = this.listView1.Items[ff].SubItems[0].Text.Split('\\')[0];
FileName = this.listView1.Items[ff].SubItems[0].Text.Split('\\')[1];
tempFile=FileName.Substring(0,FileName.LastIndexOf("."));
FileExten = Path.GetExtension(this.listView1.Items[ff].SubItems[0].Text.Split('\\')[1]);
LoadProgress(((Convert.ToDouble(ff) / total) * 100).ToString("F") + "%");
ff++;
}
Progress(total);
LoadProgress("100%");
}
public delegate string delegateGetCotrolValue(int nItemIndex);
private string GetUserNameValue(int nItemIndex)
{
return this.listView1.Items[nItemIndex].SubItems[0].Text.Split('\\')[0];
}
private string GetFileNameValue(int nItemIndex)
{
return this.listView1.Items[nItemIndex].SubItems[0].Text.Split('\\')[1];
}
UserName = (string)this.Invoke(new delegateGetCotrolValue(GetUserNameValue), new object[] { ff});
FileName = (string)this.Invoke(new delegateGetCotrolValue(GetFileNameValue), new object[] { ff });
/// <summary>
/// 定义委托
/// </summary>
/// <param name="m_t">文件大小</param>
/// <param name="m_p">当前位置</param>
public delegate void SendFileHandler(int m_t, int m_p);
/// <summary>
/// 定义事件,这个事件将被主线程捕捉,并对主线程 进度条进行修改。
/// </summary>
public event SendFileHandler m_SpFlHandler;
public void SendFile()
{
m_SpFlHandler += new SendFileHandler(ChgProgress);
Thread t = new Thread(StartSend);
t.Start();
}
//开始发送文件
public void StartSend()
{
//连接到好友客服端
p2pClient= this.GetP2PClient();
ns=p2pClient.GetStream();
FileStream fs = new FileStream(fullName, FileMode.Open, FileAccess.Read);
this.progressBar1.Visible = true;
this.label10.Visible = true;
int m_totalSize = 0;
int m_curPosition = 0;
//源文件大小
m_totalSize = (int)fs.Length;
byte[] bys = new byte[length];
while (true)
{
int read=fs.Read(bys, 0, bys.Length);
if (read <= 0)
{
break;
}
ns.Write(bys, 0, bys.Length);
ns.Flush();
m_curPosition += read;
this.label10.Text = string.Format("{0}%", (int)(m_curPosition*100/m_totalSize));
//触发事件
m_SpFlHandler(m_totalSize, m_curPosition);
Thread.Sleep(50);
}
this.progressBar1.Visible = false;
this.richTextBox2.Text += string.Format("文件:{0} 发送成功,(总大小:{1}KB)\r\n", fileName, (int)length / 1024);
this.richTextBox2.SelectionStart = this.richTextBox2.Text.Length;
this.richTextBox2.ScrollToCaret();
}
//改变进度条的值
private void ChgProgress(int m_t, int m_p)
{
progressBar1.Maximum = m_t;
progressBar1.Value = m_p;
}