我的加载屏幕没有关闭

时间:2014-12-09 14:50:09

标签: c# loading splash-screen formclosing

我正在尝试在我的工作正在进行时显示一些加载屏幕。 我的工作就是一次将一个文件从一个单元格移动到另一个单元格,代码运行完美。但when i am trying to move a group of files the loading screen is coming and after job its stuck for 20-30 seconds

我找到了解决方案:its because after all the files are moved to the next column, my control is going to dataGridView1_DataBindingComplete 所以最新发生的是网格单元根据我的条件给出颜色 当我完全删除dataGridView1_DataBindingComplete工作时... 但我也需要提供颜色..请告诉我如何处理这种情况。

代码:

    private void button3_Click(object sender, EventArgs e)
    {
        Hide();
        bool done = false;
        ThreadPool.QueueUserWorkItem((x) =>
        {
            using (var splashForm = new Form4())
            {
                splashForm.Show();
                while (!done)
                    Application.DoEvents();
                splashForm.Close();
            }
        });

        move(); // this is my function to move all files from one column to another
        done = true;
        Show();

    }

           private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {
        string strVal = ini.ReadValue("Action", "Doc-Controller");

        bool authenticated = true;
        string textboxGroupName1 = ini.ReadValue("Action", "Fabricator");
        if (authenticated == UserInCustomRole(textboxGroupName1))
        {

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                string fName1 = System.IO.Path.GetFileNameWithoutExtension(row.Cells[2].Value.ToString());
                string fName2 = System.IO.Path.GetFileNameWithoutExtension(row.Cells[3].Value.ToString());
                if (!string.IsNullOrEmpty(fName1) && !string.IsNullOrEmpty(fName2))
                {
                    var f1 = GetValue(fName1.ToCharArray()[fName1.Length - 2]) * 16 + GetValue(fName1.ToCharArray()[fName1.Length - 1]);
                    var f2 = GetValue(fName2.ToCharArray()[fName2.Length - 2]) * 16 + GetValue(fName2.ToCharArray()[fName2.Length - 1]);
                    //if (System.IO.Path.GetFileName(fName1) != System.IO.Path.GetFileName(fName2))
                    if (f1 > f2)
                    {
                        //MessageBox.Show(fName1);
                        DataGridViewCellStyle style = new DataGridViewCellStyle();
                        style.BackColor = Color.Yellow;
                        row.Cells[2].Style = style;
                    }
                    else if (f2 > f1)
                    {
                        //MessageBox.Show(fName1);
                        DataGridViewCellStyle style = new DataGridViewCellStyle();
                        style.BackColor = Color.Yellow;
                        row.Cells[3].Style = style;
                    }

                    if (f1 == f2)
                    {
                        DataGridViewCellStyle style = new DataGridViewCellStyle();
                        style.BackColor = Color.Plum;
                        row.Cells[3].Style = style;
                        row.Cells[2].Style = style;
                    }
                }
            }

        }

        if (authenticated == UserInCustomRole(strVal))
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                string fName1 = System.IO.Path.GetFileNameWithoutExtension(row.Cells[3].Value.ToString());
                string fName2 = System.IO.Path.GetFileNameWithoutExtension(row.Cells[4].Value.ToString());
                if (!string.IsNullOrEmpty(fName1) && !string.IsNullOrEmpty(fName2))
                {
                    var f1 = GetValue(fName1.ToCharArray()[fName1.Length - 2]) * 16 + GetValue(fName1.ToCharArray()[fName1.Length - 1]);
                    var f2 = GetValue(fName2.ToCharArray()[fName2.Length - 2]) * 16 + GetValue(fName2.ToCharArray()[fName2.Length - 1]);
                    //if (System.IO.Path.GetFileName(fName1) != System.IO.Path.GetFileName(fName2))
                    if (f1 > f2)
                    {
                        //MessageBox.Show(fName1);
                        DataGridViewCellStyle style = new DataGridViewCellStyle();
                        style.BackColor = Color.Yellow;
                        row.Cells[3].Style = style;
                    }
                    else if (f2 > f1)
                    {
                        //MessageBox.Show(fName1);
                        DataGridViewCellStyle style = new DataGridViewCellStyle();
                        style.BackColor = Color.Yellow;
                        row.Cells[4].Style = style;
                    }

                    if (f1 == f2)
                    {
                        DataGridViewCellStyle style = new DataGridViewCellStyle();
                        style.BackColor = Color.Plum;
                        row.Cells[4].Style = style;
                        row.Cells[3].Style = style;
                    }
                }
            }
        }
    }

    private int GetValue(char ch)
    {
        if (ch >= 48 && ch <= 57)
            return ch - 48;
        else if (ch >= 65 && ch <= 90)
            return ch - 65 + 10;
        else
            return 0;
    }

2 个答案:

答案 0 :(得分:0)

为了帮助您了解全局,我们将在此处概述一种解决问题的方法。这仅使用一个BackgroundWorker,并注意主对UI线程正在显示该对话框,因为它是从ProgressChanged()事件显示的:

Button Click Handler
    Disable the Button
    Build a List of the Work to be done
    Pass List to RunWorkerAsync

DoWork Handler
    Cast e.Argument back to your List
    Call ReportProgress() with -1 as the param
    Iterate over the List and Do the Work
        ...after you move each file...
        Call ReportProgress() and pass the percentage complete as the first parameter,
            with the data to update the grid with in the second parameter
            *You can pass anything you want out in the second parameter, like a Custom Class

ProgressChanged Handler
    If e.ProgressPercent is -1 Then
        Display your Dialog
    Else
        Cast e.UserState to your progress structure and update the grid accordingly

RunWorkerCompleted Handler
    Close your dialog
    Re-enable the Button

答案 1 :(得分:-1)

当他的一个孩子活着时,你不能关闭一个线程。 对于我们的案例,这是你的Form4。

尝试将IsBackGround设置为true。即使他的一个孩子还活着,它也会让你的线程死掉:

private void button3_Click(object sender, EventArgs e)
{
    Thread thd = new Thread(new ThreadStart(SomeThread));
    thd.IsBackground = true;
    thd.Start();
    movement();
    dataGridView1.Refresh();
    thd.Abort();
}
相关问题