I have made a WPF application, it has a button to move some files attached to the column cells from one column to another column. The moment when i press button it shows a nice animation and moves all files to the next column cells. But my real problem is once i give my function color_check(),
my application is getting stuck. I really dont know why.. I know its not stuck technically my application is using time to do the job for color_check(), any way to tackle this,, Is there any helps i can get out for this??

Codes:

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(); //file moving function
        //color_check();  if i give this fn, my form stucks and comes to live after 10 - 20 sec
        done = true;
        MessageBox.Show("TEST FINISHED");
        Show();
    }

 public void color_check()  //this is my problem making fn
    {
        dataGridView1.Refresh();
         string strVal = ini.ReadValue("Action", "Doc-Controller");

        bool authenticated = true;

        if (authenticated == UserInCustomRole(strVal))
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                // Application.DoEvents(); 
                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;
                    }
                }
            }

    }

You'd want to execute a long running method in a separate thread to avoid the UI thread becoming unresponsive. Just make sure that there's something for the user to look at or interact with while the method is doing its thing, or it won't matter whether the UI is responsive or not. ;)

I think I see what's going on, it's that while loop.

You are firing of a series of Application.DoEvents(), which, while I don't know a whole lot about this piece of code (other then anywhere I see it mentioned people suggest avoiding it if possible), but I am guessing that while loop is continously firing off that piece of code.

What it's doing exactly, I don't know, but anytime I think of code locking up, the first thing I think about is a loop that is infinite. I think that do event is messing with your code (it might also be keeping your from interacting with your form items which could be the cause).

Do me a favor, that the form you want to display, and disable it, and see if your code works. If so, remove that applidation do events. Instead, within the form, have code that closes the form. So SplashForm, have a piece of code that once done, close itself. You can leave it in the thread if you'd like, just remember that your code will spool right to that color_check as nothing is stopping the code from proceeding

(which if you want it to stop your in luck, since you are using a threading pool, you can use reset events which cause halt parts of the code till a thread is finished)

There are a number of bad design decisions evident here.
1. No real application uses Application.DoEvents.(VB6 is not a real programming language).
2. Peeking and Poking data into ANY control is just plain wrong.
3. Why aren't you using MVVM? DataBinding?
4. Multithreading (ThreadPool.QueueUserWorkItem) to display a dialog on background thread.

Let's fix this.
1. Implement MVVM. View first is fine. Define the DataModel & ViewModel.
ViewModel exposes an ICommand for data binding to button3's Command property. ALL data binding is done in XAML.
This ends up in the ViewModel for CanExecute (authenticated == UserInCustomRole(strVal))
2. Multithreading. Use TPL or async/await to move files
Remember, data binding imposes thread affinity. The ViewModel will need a reference to the SynchronizationContext to marshal updates from the worker thread to the UI thread.
3. Use implement an IValueConverter for setting the DataGridViewCellStyle
4. Instead of Show/Hide main View and Show/Close Form4 (splashForm), try using an Adorner.

Performance has just increased. Correct use of multithreading. File moving is now multithreaded.
ALL code behind is gone!
No more playing window manager (Show/Hide/Close).
Implemented MVVM & Separation-of-Concerns design patterns.

One more change.
ini files are obsolete. You should be using app.config.
Also consider using User/Application Settings. Double click on Settings.settings in Solution Explorer.

Use BackgroundWorker, may be it helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.