简体   繁体   中英

Creating a statusfield for users

I need some guidance here on why this isn't working:

So here's the issue, I want to give my users a little status field so they can check how long it will take and get a coffee or two for them.

My Problem is that the statusfield (2 Labels), are not updated during the process.

This is my current code :

    private void Cancel_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void start_change_Click(object sender, EventArgs e)
    {

        DialogResult dr = MessageBox.Show("Start process?", "DateChanger", MessageBoxButtons.OKCancel, MessageBoxIcon.Hand);
        if (dr == DialogResult.OK)
        {
            //get files
            List<String> d = new List<String>();
            label_status_title.Text = "Status: collecting Data, take a coffee while waiting.\nfiles changed: 0 files";
            d = getFiles("H:\\");
            int i = 0;
            double diff = 0.0;

            //modify files
            label_status_title.Text = "Status: changing files.\nfiles changed: 0/" + d.Count + " files.";
            foreach (String s in d)
            {
                    String label = "\nfile: " + s;
                    //create newDate and modify creation and lastwrite
                    DateTime actualDate = Directory.GetLastWriteTime(s).Date;
                    DateTime newDate = new DateTime(2015, 03, 01);
                    diff = (newDate - actualDate).TotalDays;
                    label += "\nactual creation date: " + Directory.GetCreationTime(s).Date;
                    label += "\nnew creation date: " + newDate.Date;
                    label += "\nactual last write date: " + Directory.GetLastWriteTime(s).Date;
                    label += "\nnew last write date: " + newDate.Date;

                    if (diff > 400)
                    {
                        try
                        {
                            //set new timevalues
                            Directory.SetCreationTime(s, newDate);
                            Directory.SetCreationTimeUtc(s, newDate);
                            Directory.SetLastWriteTime(s, newDate);
                            Directory.SetLastWriteTimeUtc(s, newDate);
                        }
                        catch (UnauthorizedAccessException UAE)
                        {
                        }
                        i++;
                        label += "\nchange needed.";
                    }
                    else
                    {
                        label += "\nchange not needed.";
                    }
                    label_status.Text = label;
                    label_status_title.Text = "Status: changing files.\nfiles changed: " + i + "/" + d.Count + " files.";
            }

            MessageBox.Show("Process finished, changed: " + i + "/" + d.Count + " files.");
        }
    }




    private List<String> getFiles(string sDir)
    {
        List<String> files = new List<String>();
        try
        {
            foreach (string f in Directory.GetFiles(sDir))
            {
                files.Add(f);
            }
            foreach (string d in Directory.GetDirectories(sDir))
            {
                files.AddRange(getFiles(d));
            }
        }
        catch (System.Exception excpt)
        {
            MessageBox.Show(excpt.Message);
        }
        return files;
    }

    private void DateChanger_Load(object sender, EventArgs e)
    {
        String label = "";
        label_status_title.Text = "Status: \nfiles changed: 0 files";
        label += "file: ";
        label += "\nactual creation date: ";
        label += "\nnew creation date: ";
        label += "\nactual last write date: ";
        label += "\nnew crealast writetion date: ";
        label_status.Text = label;
    }

I also tried the suggestion of using MethodInvoker , but that also didn't work either. Any guidance or suggestions here are appreciated.

Thanks.

Mirko

ps if there is a better solution than using labels or text boxes for this feel free to tell me. :)

Youre Method start_change_Click(object sender, EventArgs e) is blocking the main thread. To avoid this, use a separate thread to update the labels. Check out this post: Thread freezes main UI

Just refresh the Label after assigning it a new Text value.

label_status_title.Text = "Status: changing files.\nfiles changed: " + i + "/" + d.Count + " files.";
label_status_title.Refresh(); //added

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM