Click here to Skip to main content
15,911,360 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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.. 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;
                    }
                }
            }

    }
Posted

1 solution

The application actually doesn't get stuck in real, it is just the UI of your WPF that gets frozen when the processes to be executed a complex. In your code, this function can be simply just minimized, or put on to an asynchronous thread, which would execute apart from the UI thread.

Since you're a newbie to this async programming, I would like to forward you to MSDN to get a brief overview of the async programming[^]; writing an article to explain the concept of async programming in the QandA forums won't be a good idea.

Once you've created the async functions, each time a function that would be taking time executing can be put on a seperate async thread, where it will keep executing or working as a background thread. This way, you UI won't stop and the WPF application won't "freeze". This problem really is a major issue in WPF framework, because each and every programming; even me, has to go through this process. Because WPF has a single thread by default, which would not only perform the business logic but in turn is also responsible for the UI updates, such as Button state change and other UI related tasks. So, that is why when the thread is performing a function or executing a method, it doesn't perform anything for the UI, which causes a sense of "freezing" in the UI and it looks like the application got stuck, or the processing stopped! No, that is not a reason. Program (application) is running but there is another process that is taking time.

The major time consuming processes include, working with the HTTP resources, such as HTML documents or other files from the internet, other than these, the I/O resources like fetching data from Hard disk etc might consume some time. In these cases such problem occurs. It is better to perform such tasks asynchronously.

In your code, a System.IO code is also present and the arrays and other data processes are also time consuming, until they are executed the thread will be frozen.

Good luck! :-)
 
Share this answer
 
v2
Comments
Member 11078756 12-Dec-14 15:29pm    
@Afzaal Ahmad Zeeshan excatly ur correct.. i really appriciate for your answer..
What u said is correct.. the form is got stuck because its taking time to do the color_check.. thats why my form stuck. I heard this async programming.. But its kind of tricky for me.. may be can you just help me with this issue.. that would be great.. First please unrar this file// to C folder.
http://www.mediafire.com/ download/o310bn1cvt9f7oi/ ouput.rar

Then unrar this project some where >> http://www.mediafire.com/download/6i0wjv5h5amjel2/final.rar

After that open my project//debug folder in that setting.ini file.. change the Doc-Controller= to your groupe name

then please run the project. Would be able too see a number of files populated on the gird.
> check the check box of the first columns, so that it will select all the files at a single time

Now my application is working fine once i select all files and moved from draft column to release column.
When i give the function Datagrid_bind(), my application is getting stuck.

Only this i need to sort out.
Afzaal Ahmad Zeeshan 13-Dec-14 8:15am    
Yes, it will take some time but you should pay attention to learn the concept of async programming. You will get to the depth of the async, and you will be able to develop it yourself.
BillWoodruff 12-Dec-14 22:13pm    
+5 excellent, well-written, answer !
Afzaal Ahmad Zeeshan 13-Dec-14 8:14am    
Thanks alot, Bill. :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900