如何在不使主表单卡住的情况下给我的功能 [英] How to safley give my function without making my main form stuck

查看:69
本文介绍了如何在不使主表单卡住的情况下给我的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个WPF应用程序,它有一个按钮,可以将附加到列单元格的一些文件从一列移动到另一列。当我按下按钮它显示一个很好的动画并将所有文件移动到下一个列单元格的那一刻。但我真正的问题是一旦我给我的函数color_check(),我的应用程序就会卡住。我真的不知道为什么..有什么帮助我可以出去吗?



代码:



 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(); //文件移动函数
// color_check();如果我给这个fn,我的表格卡住并在10 - 20秒后生效
done = true;
MessageBox.Show(TEST FINISHED);
Show();
}

public void color_check()//这是我的问题fn
{
dataGridView1.Refresh();
string strVal = ini.ReadValue(Action,Doc-Controller);

bool authenticated = true;

if(authenticated == UserInCustomRole(strVal))
{
foreach(dataGridViewRow 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;
}
}
}

}

解决方案

应用程序实际上并没有陷入困境,只是你的WPF的UI在执行复杂的进程时被冻结。在您的代码中,此函数可以简单地最小化,或者放在异步线程上,该线程将在UI线程之外执行。



既然你是这个异步编程的新手,我想转发你到MSDN,以获得异步编程 [ ^ ];写一篇文章来解释QandA论坛中异步编程的概念不是一个好主意。



一旦你创建了异步函数,每次执行时间的函数都可以放在一个单独的异步线程上,它将继续执行或者作为后台线程工作。这样,UI就不会停止,WPF应用程序也不会冻结。这个问题确实是WPF框架中的一个主要问题,因为每一个编程都是如此;即使是我,也要经历这个过程。因为默认情况下WPF有一个单独的线程,它不仅会执行业务逻辑,而且还负责UI更新,例如Button状态更改和其他UI相关任务。所以,这就是为什么当线程执行一个函数或执行一个方法时,它不会为UI执行任何操作,这会导致UI中出现冻结感,看起来应用程序卡住了,或者处理过程中停!不,这不是原因。程序(应用程序)正在运行,但还有另一个需要时间的过程。



主要耗时的过程包括使用HTTP资源,如HTML文档或来自互联网的其他文件,除此之外的I / O资源,如从硬盘等获取数据可能会消耗一些时间。在这些情况下会出现这种问题。最好以异步方式执行此类任务。



在你的代码中,还存在一个System.IO代码,并且数组和其他数据进程也很耗时,直到执行它们为止冻结。



祝你好运! : - )

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;
                    }
                }
            }

    }

解决方案

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! :-)


这篇关于如何在不使主表单卡住的情况下给我的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆