当前位置:编程学习 > C#/ASP.NET >>

使用委托或多线程动态更改label值的问题

各位大侠,在winform里想利用委托或多线程动态更改label的text值,实现显示任务执行状态的功能,比如:正在初始化。。。正在更新。。。正在执行第x个任务。。。等,类似这样的功能。本人对委托不是很了解,想了很久 没有实现,忘哪个大侠帮忙,最好有代码,谢谢。 --------------------编程问答-------------------- 参考吧
http://www.cnblogs.com/mokey/articles/2095457.html --------------------编程问答-------------------- this.Invoke(() => { label1.Text = "aaa"; }); --------------------编程问答--------------------
引用 1 楼 bdmh 的回复:
参考吧
http://www.cnblogs.com/mokey/articles/2095457.html

按照此贴的方法还是未能实现 ,小弟不才。。。 --------------------编程问答--------------------
引用 2 楼 caozhy 的回复:
this.Invoke(() => { label1.Text = "aaa"; });

这种语法不会用啊,直接用吗?还是?提示无法转换成委托类型 --------------------编程问答--------------------
引用 4 楼 dengyp2010 的回复:
提示无法转换成委托类型

可以改成明确的Action委托实现
this.Invoke(new Action(() => label1.Text = "aaa"));
--------------------编程问答-------------------- 另外,在你定义委托实现代码的lamda表达式中,如果只有一句,那么就无需写 { .... } 括号。上面的就等价于
this.Invoke(new Action(() => 
   {
         label1.Text = "aaa";
   }));
--------------------编程问答-------------------- 以下是我的代码,哪位前辈帮忙加一下代码,小弟实在不才。。。。


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.IO;
using System.Collections;
using System.Threading;


namespace WindowsFormsApplication1
{
  



    public partial class Form1 : Form
    {


        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

          
            folderBrowserDialog1.ShowNewFolderButton = true;
            folderBrowserDialog1.Description = "请选择文件存储位置:";
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {


                textBox1.Text = folderBrowserDialog1.SelectedPath;

            }else{
            
                MessageBox.Show("请选择文件所在文件夹!","提示",MessageBoxButtons.OK ,MessageBoxIcon.Warning );
              
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {

            
            //点击开始时执行
         
            string FolderPath;
            string FileName;
            string   FileName_Num;

            FolderPath = textBox1.Text.Trim(); 
            FileName = textBox2.Text.Trim();
             FileName_Num = textBox3.Text.Trim();
            

            if (FolderPath !="" & FileName!=""& FileName_Num!=""){

                textBox1.Enabled = false;
                textBox2.Enabled = false;
                textBox3.Enabled = false;
                button1.Enabled = false;
                button2.Enabled = false;

                //想在此加入提示 Label1.Text="正在初始化。。。"
                ReName(FolderPath, FileName, Convert.ToInt32 (FileName_Num));
                       
                
                }else if(FolderPath==""){
                
                       MessageBox.Show("请选择文件所在文件夹!","提示",MessageBoxButtons.OK ,MessageBoxIcon.Warning );
                      
                }else if(FileName==""){
                
                       MessageBox.Show("请输入文件名-文本部分!","提示",MessageBoxButtons.OK ,MessageBoxIcon.Warning );
                       
                }else if(FileName_Num ==""){
                    
                         MessageBox.Show("请输入文件名-起始数字!","提示",MessageBoxButtons.OK ,MessageBoxIcon.Warning );
                
                }
        }
            
              
            public void  ReName (string FoldPath,string FileName_text,Int32 FileName_Num){

               
               

                            DirectoryInfo DI = new DirectoryInfo(FoldPath);
                            FileInfo[] Files = DI.GetFiles();
                            Int32 NowItem;
                            NowItem = 1;
                            foreach (FileInfo File in Files)
                            {
                                renameNow(File, FoldPath, FileName_text, FileName_Num, Files.Length, NowItem);
                                NowItem ++;
                                FileName_Num ++;
                            } //foreach end

                            //想在此加入提示 Label1.Text="操作完成!“
                            MessageBox.Show("操作成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
               
            }//public end


            public void renameNow(FileInfo fi,string FoldPath,string FileName_text, Int32 FileName_Num,Int32 Total,Int32 Nowitem) {


                try
                {

                           //想在此加入提示 Label1.Text="正在操作。。。第x/xx个文件:xxxx.jpg"         其中nowitem是目前正在执行的文件序号,Total是总文件数,file是文件名
                    fi.MoveTo(FoldPath + @"\" + FileName_text + FileName_Num  + fi.Extension);
                    FileName_Num += 1;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message , "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            
            }
 }

 }



--------------------编程问答-------------------- 通篇没找到Thread的影子。。。

void btn1_Click(object sender,EventArgs e)
{
Thread th = new Thread((ThreadStart)delegate()
{
      bool flag = true;
       //干活 出错设置flag==false
       this.Invoke((Action)delegate()
       {
             label1.Text = "正在进行第一步...";
        }
       //干活 出错设置flag==false
        this.Invoke((Action)delegate()
       {
             label1.Text = "正在进行第二步...";
        }
       //干活 出错设置flag==false
        this.Invoke((Action)delegate()
       {
             label1.Text = "正在进行第三步...";
        }
        //干活 出错设置flag==false
        this.Invoke((Action)delegate()
       {
             if(flag) 
             label1.Text = "已完成...";
             else
             label1.Text = "过程执行有错";
        });
     
});
th.Start();
}
以后后台执行任务 更新前台界面  都可以参照这个格式 --------------------编程问答-------------------- 非常感谢,我再琢磨琢磨,我把委托和线程的代码都删了 这样你们看的清楚些。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,