WinForm窗体中发短信要实现一个进度条,找了一个是用循环做的,进度条的速度太快了,效果不好,怎么实现效果会好一些?用backgroundWorker,具体怎么实现,能否附上一些代码?

解决方案 »

  1.   

    后台类:
    public class PublishHelper
        {
            private static PublishHelper helper;
            public static PublishHelper Instance
            {
                get
                {
                    if ( helper == null )
                    {
                        helper = new PublishHelper();
                    }
                    return helper;
                }
            }
            /// <summary>
            /// 获取文件列表
            /// </summary>
            /// <param name="BaseDir">APP地址</param>
            /// <returns>List列表</returns>
            public AssemblyInfoFileList GetFileList( string BaseDir )
            {
                AssemblyInfoFileList afl = GetClientUpdateList( BaseDir );
                return afl;
            }        /// <summary>
            /// 私有方法,递归数据给上面客户端列表
            /// </summary>
            /// <param name="BaseDir">APP地址</param>
            /// <returns>List列表</returns>
            private AssemblyInfoFileList GetClientUpdateList( string BaseDir )
            {
                if ( String.IsNullOrEmpty( BaseDir ) )
                {
                    BaseDir = Path.Combine( AppDomain.CurrentDomain.BaseDirectory, "UpdateClient" );
                }            return AssemblyHelper.GetAssemblyList( BaseDir );
            }
            /// <summary>
            /// 进行文件COPY的方法[线程回调]
            /// </summary>
            /// <param name="filelist">文件列表</param>
            /// <param name="UserPath">用户选择的路径</param>
            /// <param name="bar">进度条</param>
            /// <param name="lab">标签条</param>
            public void ShareSession( AssemblyInfoFileList filelist, string UserPath, string publishPath,ProgressBar bar, Label lab )
            {
                int cm = 100 / filelist.Count;            if ( File.Exists( AssemblyHelper.AssemblyListFileName ) ) File.Delete( AssemblyHelper.AssemblyListFileName );
                //string AppPath =Path.Combine( System.AppDomain.CurrentDomain.BaseDirectory, "UpdateClient" );
                string AppPath = Path.Combine(publishPath, "UpdateClient");
                if (!Directory.Exists(AppPath))
                {
                    Directory.CreateDirectory(AppPath);
                }
                foreach ( AssemblyInfoFile temp in filelist )
                {
                    if ( bar.Value < 100 ) { bar.Value += cm; }
                    lab.Text = "正在下载:" + temp.RelativePath;
                    string remoteFileName = temp.RelativePath;
                    if ( remoteFileName.IndexOf( "\\" ) > 0 )
                    {
                        string file = remoteFileName.Substring( remoteFileName.LastIndexOf( "\\" ) + 1, remoteFileName.Length - remoteFileName.LastIndexOf( "\\" ) - 1 );
                        string[] spli = { "\\" + file };
                        string path = remoteFileName.Split( spli, StringSplitOptions.RemoveEmptyEntries )[ 0 ];
                        if ( !Directory.Exists( Path.Combine( AppPath, path ) ) )
                        {
                            Directory.CreateDirectory( Path.Combine( AppPath, path ) );
                        }
                    }
                    File.Copy( Path.Combine( UserPath, remoteFileName ), Path.Combine( AppPath, remoteFileName ), true );
                }
                bar.Value = 100;
                lab.Text = "文件更新成功完成";
            }    }
    界面调用:string strAppCode = this.tslAppCode.Text;            if ( this.UpdatedFile.Count == 0 || String.IsNullOrEmpty( strAppCode ) || strAppCode == "应用系统" )
                {
                    return;
                }            try
                {
                    if ( MessageBox.Show( String.Format( "您确定要更新文件夹{0}下的{1}个文件到{2}系统么? ", this.LocalRootDirectory.FullName,
                        this.UpdatedFile.Count, strAppCode ), "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question )
                        == DialogResult.No )
                        return;                tsbUpdate.Enabled = false;
                    ProgressBarStatus.Visible = true;
                    ProgressBarStatus.Maximum = UpdatedFile.Count;
                    ProgressBarStatus.Value = 0;                helper.UploadWholeDir = false;
                    helper.UploadFile( this.UpdatedFile, strAppCode );                MessageBox.Show( "更新服务完成", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information );
                }
                catch ( Exception ex )
                {
                    MessageBox.Show( "更新失败" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error );
                    LogManager.WriteError( ex );
                }
                finally
                {
                    ProgressBarStatus.Visible = false;
                    this.labelInfo.Text = "就绪";
                    tsbUpdate.Enabled = true;
                }
      

  2.   

    http://www.chenjiliang.com/Article/Default.aspx?TypeID=98这里。都是多线程的,看看吧
      

  3.   

    winform用BackgroudWoker异步显示进度条
      

  4.   

    例子参考:http://blog.csdn.net/gisfarmer/archive/2009/01/09/3742019.aspx
      

  5.   

    这个效果是从100线程减到0,进度条的显示。public Thread th1;delegate void SetValues(int i);
    delegate void ShowProBar(int i);private void button1_Click(object sender, EventArgs e)
    {
      th1 = new Thread(new ThreadStart(test1));
      th1.Start();
    }
    private void test1()
    {
      int i = 100;
      while (i > -1)
      {
      setvalue1(i);
      ShowProgress1(i);
      i--;
      Thread.Sleep(1000);
      }
    }
    private void setvalue1(int i)
    {
      if (this.InvokeRequired)
      {
      this.Invoke(new SetValues(setvalue1), new object[] {i});
      }
      else
      {
      自己的操作
      }
    }
    private void ShowProgress1(int i)
      {
      if (this.progressBar1.InvokeRequired)
      {
      this.Invoke(new ShowProBar(ShowProgress1), new object[] { i });
      }
      else
      {
      progressBar1.Value = 100-i;
      progressBar1.Visible=true;
      }
      }
      

  6.   

    额,大家可能想复杂了哦!!lz在创体力拖一个Lable,一个button,一个progressBar1具体code如下:去不铁给你,直接用using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;namespace WinTestUrl
    {
        public delegate void SetPos(int ipos);
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }        public void SetTextMessage(int ipos)
            {
                if (this.InvokeRequired)
                {
                    SetPos setpos = new SetPos(SetTextMessage);
                    this.Invoke(setpos, new object[] { ipos });
                }
                else
                {
                    this.label1.Text = ipos.ToString() + "/10";
                    this.progressBar1.Value = Convert.ToInt32(ipos);
                }
            }        public void button2_Click(object sender, EventArgs e)
            {
                Thread fThread = new Thread(new ThreadStart(SleepT));//开辟一个新的线程 
                fThread.Start();
            }        public void SleepT()
            {
                for (int i = 1; i <= 100; i++)
                {
                    System.Threading.Thread.Sleep(100);//没什么意思,单纯的执行延时 
                    SetTextMessage(10 * i / 100);
                }
            }       
        }
    }