| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1471 人关注过本帖
标题:多线程对应多进度条问题!
只看楼主 加入收藏
女精灵
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2008-2-27
收藏
 问题点数:0 回复次数:1 
多线程对应多进度条问题!
本人现在做一个FTP上传软件用WINFORM,利用多线程上传每个线程对应一个进度条!上传完成了也可以多线程但是利用的THREADING类,如果现在想时时得到进度返回给进度条,就不能用线程类了!不知道大家还有什么其他的办法!代码如下:
//主窗体类
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using
using
using System.Configuration;
using System.Threading;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        delegate void uploaddelegate(string filename);
        uploaddelegate upload;
        //private FtpWebRequest reqFtp;
        private static string webUri = System.Configuration.ConfigurationManager.AppSettings["serverurl"].ToString();
        private static string uid = System.Configuration.ConfigurationManager.AppSettings["uid"].ToString();
        private static string password = System.Configuration.ConfigurationManager.AppSettings["password"].ToString();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ofd.ShowDialog();
            ofd.InitialDirectory = @"c\:";
            ofd.ReadOnlyChecked = true;
            ofd.Filter = "所有文件(*.*)|*.*";
            textBox2.Text = ofd.FileName.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Trim() == string.Empty || textBox2.Text.Trim() == string.Empty)
            {
                MessageBox.Show("请输入你要上载的文件名字!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                string fileNamePath = textBox2.Text.Trim();
                string urlString = textBox1.Text.Trim();
                string fileName = fileNamePath.Substring(fileNamePath.LastIndexOf("\\") + 1);
                string fileNameExt = fileName.Substring(fileName.LastIndexOf(".")+1);

                if (urlString.EndsWith("/") == false)
                {
                    urlString = urlString + "/";
                }
                urlString = urlString + fileName;

                FTPAccess ftp = new FTPAccess();

                Thread ff = new Thread(new ParameterizedThreadStart(ftp.Upload));
                ff.Start(fileName);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string[] ftpList = new FTPAccess().getFtpList(textBox1.Text);
            for (int i = 0; i < ftpList.Length; i++)
            {
                if(ftpList[i].ToString().Equals("."))
                {

                }
                listView1.Items.Add(ftpList[i]);
            }
        }

        //public void ThreadProc(FTPAccess ftp)
        //{
        //    this.Invoke(ftp.Upload);
        //}

     }



}
//文件上传类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using
using
using System.Windows.Forms;
using

namespace WindowsFormsApplication2
{
    class FTPAccess
    {
        public Boolean success = false;
        private FtpWebRequest reqFtp;
        private static string webUri = System.Configuration.ConfigurationManager.AppSettings["serverurl"].ToString();
        private static string uid = System.Configuration.ConfigurationManager.AppSettings["uid"].ToString();
        private static string password = System.Configuration.ConfigurationManager.AppSettings["password"].ToString();

        public delegate void uploaddelegate(object filename);

        public FTPAccess()
        {
            reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(webUri));
            reqFtp.Credentials = new NetworkCredential(uid, password);
            reqFtp.UseBinary = true;
            reqFtp.KeepAlive = false;
            //uploaddelegate upload = new uploaddelegate(Upload);
        }

        /*<summary>
         * 得到FTP服务器的文件列表
         * </summary>
         * <param name="ftpUri">文件地址</param>
         */
        public string[] getFtpList(string ftpUri)
        {
            string[] downloadFiles = null;
            //FileInfo fileInfo = new FileInfo(ftpUri);
            StringBuilder result = new StringBuilder();
            try{

                reqFtp.Method = WebRequestMethods.Ftp.ListDirectory;
                FtpWebResponse repsonse = (FtpWebResponse)reqFtp.GetResponse();
                StreamReader reader = new StreamReader(repsonse.GetResponseStream());
                  
                string line = reader.ReadLine();
                //fileLength = (int)response.ContentLength;
            while (line != null)
            {
                result.Append(line);
                result.Append( "\n ");
                line   =   reader.ReadLine();
            }
                //   to   remove   the   trailing   '\n '
                result.Remove(result.ToString().LastIndexOf( "\n "),   1);
                reader.Close();
                repsonse.Close();
                return result.ToString().Split('\n');
                //return fileLength;
            }
            catch   (Exception   ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                downloadFiles   =   null;
                return   downloadFiles;
            }

        }

        /*<summary>
         * 判断文件是否存在
         * </summary>
         * <param name="fileName">FTP文件地址</param>
         */
        public Boolean isFile(string fileName)
        {
            FileInfo fileinfo = new FileInfo(fileName);
            string[] pathList = this.getFtpList(webUri);
            for (int i = 0; i < pathList.Length; i++)
            {
                string filell = pathList[i].ToString().Trim();
                if (pathList[i].ToString().Trim() == fileName.Trim())
                {
                    return true;
                }
            }
            return false;
        }

        

        /*<summary>
         * 上传文件
         * </summary>
         * <param name="fileName">文件地址</param>
         */
        public void Upload(object filename/*, BackgroundWorker worker, DoWorkEventArgs e*/)
        {
            // 缓冲大小设置为200kb
            int buffLength = 204800;

            //string filename = @"F:\Xunlei\Install_WLMessenger.zip";

            FileInfo fileInfo = new FileInfo(filename.ToString());
            if (isFile(filename.ToString()))
            {

                byte[] buff = new byte[buffLength];
                int contentLen;

                reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(webUri + fileInfo.Name));
                reqFtp.Credentials = new NetworkCredential(uid, password);
                reqFtp.Method = WebRequestMethods.Ftp.AppendFile;
                reqFtp.Timeout = -1;

                // 打开一个文件流 () 去读上传的文件
                FileStream fs = fileInfo.OpenRead();
                try
                {
                    // 把上传的文件写入流
                    Stream strm = reqFtp.GetRequestStream();

                    // 每次读文件流的2kb
                    contentLen = fs.Read(buff, 0, buffLength);

                    // 流内容没有结束
                    while (contentLen != 0)
                    {
                        // 把内容从file stream 写入 upload stream
                        strm.Write(buff, 0, contentLen);

                        contentLen = fs.Read(buff, 0, buffLength);
                    }

                    // 关闭两个流
                    strm.Close();
                    fs.Close();
                    this.success = true;
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message, "Upload Error");
                    
                }
                finally
                {
                    System.Windows.Forms.MessageBox.Show("AppendFile success!");
                }
            }
            else
            {
                reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(webUri + fileInfo.Name));
                reqFtp.Credentials = new NetworkCredential(uid, password);
                reqFtp.Method = WebRequestMethods.Ftp.UploadFile;
                reqFtp.Timeout = -1;
                reqFtp.ContentLength = fileInfo.Length;


                byte[] buff = new byte[buffLength];
                int contentLen;

                // 打开一个文件流 () 去读上传的文件
                FileStream fs = fileInfo.OpenRead();
                try
                {
                    // 把上传的文件写入流
                    Stream strm = reqFtp.GetRequestStream();

                    // 每次读文件流的2kb
                    contentLen = fs.Read(buff, 0, buffLength);

                    // 流内容没有结束
                    while (contentLen != 0)
                    {
                        // 把内容从file stream 写入 upload stream
                        strm.Write(buff, 0, contentLen);

                        contentLen = fs.Read(buff, 0, buffLength);
                    }

                    // 关闭两个流
                    strm.Close();
                    fs.Close();
                    this.success = true;
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message, "Upload Error");
                }
                finally
                {
                    System.Windows.Forms.MessageBox.Show("File upLoad success!");
                    
                }
            }
        }
    }
}
还有怎么才能知道线程已经完成!
搜索更多相关主题的帖子: 多线程对应多进度条 
2008-06-25 11:14
女精灵
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2008-2-27
收藏
得分:0 
没人知道?
2008-06-26 09:28
快速回复:多线程对应多进度条问题!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.036833 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved