如何处理应用程序GUI的冻结 [英] How to handle freezing of application GUI

查看:78
本文介绍了如何处理应用程序GUI的冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用我在线下载的SDK,SourceAFIS编写指纹应用程序。我注意到模板提取需要一段时间才能完成,导致GUI冻结,有时会显示''''Not Responding''''所以我认为使用backgroundworker可以解决我的问题,但它仍然没有帮助。每个指纹样本的提取大约需要3秒。请问我怎么能停止这种挂起,它会使应用程序看起来很糟糕。谢谢

这是一个摘录。

I''m writting a fingerprint application using an SDK i downloaded online, SourceAFIS. I noticed that the template extraction took a while to complete, causing the GUI to freeze and sometimes shows ''''Not Responding'''' so i thought using a backgroundworker would solve my problem but it still didn''t help out. The extraction takes approximately 3 seconds per fingerprint sample. Please how can i get that hangging to stop, it makes the application look bad. Thanks
This is an extract.

<pre>private void enroll_Click(object sender, EventArgs e)
        {
            if (enrollmentWorkerBW.IsBusy == false)
            {
                enroll.Enabled = false;
                enroll.Text = "Wait !";
                progressPB.Visible = true;
                enrollmentWorkerBW.RunWorkerAsync();
            }
            else
            {
                MessageBox.Show("Enrollment is already Running","Enrollment Message",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }


private void enrollmentWorkerBW_DoWork(object sender, DoWorkEventArgs e)
        {
            this.Invoke(new Function(delegate()
            {
                AfisEngine AFIS = new AfisEngine();
                

                    Bitmap bitmap = null;
                    DPFP.Capture.SampleConversion Convertor = new DPFP.Capture.SampleConversion();	// Create a sample convertor.
                    Fingerprint fingerprint;

                    for (int i = 0; i < printTemplates.Length; i++)
                    {
                        switch (i + 1)
                            {
                                case 1:
                                    getTemplate(printTemplates[i], ref T_thumbR, (i + 1));
                                    //getPartialPrintsTemplate(printSamples[i], ref partial_T_thumbR, (i + 1));
                                    getSample(printSamples[i], ref S_thumbR, (i + 1));
                                    bitmap = null;
                                    Convertor.ConvertToPicture(printSamples[i], ref bitmap);
                                    fingerprint = new Fingerprint();
                                    fingerprint.BitmapImage = bitmap;
                                    //st.Start();
                                    AFIS.Extract(fingerprint);
                                    if (fingerprint.Template != null)
                                        partial_T_thumbR = fingerprint.Template;
                                    else
                                        partial_T_thumbR = null;

                                    break;
                                case 2:
                                    getTemplate(printTemplates[i], ref T_indexR, (i + 1));
                                    //getPartialPrintsTemplate(printSamples[i], ref partial_T_indexR, (i + 1));
                                    getSample(printSamples[i], ref S_indexR, (i + 1));
                                    bitmap = null;
                                    Convertor.ConvertToPicture(printSamples[i], ref bitmap);
                                    fingerprint = new Fingerprint();
                                    fingerprint.BitmapImage = bitmap;
                                    //st.Start();
                                    AFIS.Extract(fingerprint);
                                    if (fingerprint.Template != null)
                                        partial_T_indexR = fingerprint.Template;
                                    else
                                        partial_T_indexR = null;

                                    break;
                                case 3:
                                    getTemplate(printTemplates[i], ref T_middleR, (i + 1));
                                    //getPartialPrintsTemplate(printSamples[i], ref partial_T_middleR, (i + 1));
                                    getSample(printSamples[i], ref S_middleR, (i + 1));
                                    bitmap = null;
                                    Convertor.ConvertToPicture(printSamples[i], ref bitmap);
                                    fingerprint = new Fingerprint();
                                    fingerprint.BitmapImage = bitmap;
                                    //st.Start();
                                    AFIS.Extract(fingerprint);
                                    if (fingerprint.Template != null)
                                        partial_T_middleR = fingerprint.Template;
                                    else
                                        partial_T_middleR = null;
                                    break;
                                case 4:
                                    getTemplate(printTemplates[i], ref T_ringR, (i + 1));
                                    //getPartialPrintsTemplate(printSamples[i], ref partial_T_ringR, (i + 1));
                                    getSample(printSamples[i], ref S_ringR, (i + 1));
                                    bitmap = null;
                                    Convertor.ConvertToPicture(printSamples[i], ref bitmap);
                                    fingerprint = new Fingerprint();
                                    fingerprint.BitmapImage = bitmap;
                                    //st.Start();
                                    AFIS.Extract(fingerprint);
                                    if (fingerprint.Template != null)
                                        partial_T_ringR = fingerprint.Template;
                                    else
                                        partial_T_ringR = null;
                                    break;
                                case 5:
                                    getTemplate(printTemplates[i], ref T_smallR, (i + 1));
                                    //getPartialPrintsTemplate(printSamples[i], ref partial_T_smallR, (i + 1));
                                    getSample(printSamples[i], ref S_smallR, (i + 1));
                                    bitmap = null;
                                    Convertor.ConvertToPicture(printSamples[i], ref bitmap);
                                    fingerprint = new Fingerprint();
                                    fingerprint.BitmapImage = bitmap;
                                    //st.Start();
                                    AFIS.Extract(fingerprint);
                                    if (fingerprint.Template != null)
                                        partial_T_smallR = fingerprint.Template;
                                    else
                                        partial_T_smallR = null;
                                    break;
                                case 6:
                                    getTemplate(printTemplates[i], ref T_thumbL, (i + 1));
                                    //getPartialPrintsTemplate(printSamples[i], ref partial_T_thumbL, (i + 1));
                                    getSample(printSamples[i], ref S_thumbL, (i + 1));
                                    bitmap = null;
                                    Convertor.ConvertToPicture(printSamples[i], ref bitmap);
                                    fingerprint = new Fingerprint();
                                    fingerprint.BitmapImage = bitmap;
                                    //st.Start();
                                    AFIS.Extract(fingerprint);
                                    if (fingerprint.Template != null)
                                        partial_T_thumbL = fingerprint.Template;
                                    else
                                        partial_T_thumbL = null;
                                    break;
                                case 7:
                                    getTemplate(printTemplates[i], ref T_indexL, (i + 1));
                                    //getPartialPrintsTemplate(printSamples[i], ref partial_T_indexL, (i + 1));
                                    getSample(printSamples[i], ref S_indexL, (i + 1));
                                    bitmap = null;
                                    Convertor.ConvertToPicture(printSamples[i], ref bitmap);
                                    fingerprint = new Fingerprint();
                                    fingerprint.BitmapImage = bitmap;
                                    //st.Start();
                                    AFIS.Extract(fingerprint);
                                    if (fingerprint.Template != null)
                                        partial_T_indexL = fingerprint.Template;
                                    else
                                        partial_T_indexL = null;
                                    break;
                                case 8:
                                    getTemplate(printTemplates[i], ref T_middleL, (i + 1));
                                    //getPartialPrintsTemplate(printSamples[i], ref partial_T_middleL, (i + 1));
                                    getSample(printSamples[i], ref S_middleL, (i + 1));
                                    bitmap = null;
                                    Convertor.ConvertToPicture(printSamples[i], ref bitmap);
                                    fingerprint = new Fingerprint();
                                    fingerprint.BitmapImage = bitmap;
                                    //st.Start();
                                    AFIS.Extract(fingerprint);
                                    if (fingerprint.Template != null)
                                        partial_T_middleL = fingerprint.Template;
                                    else
                                        partial_T_middleL = null;
                                    break;
                                case 9:
                                    getTemplate(printTemplates[i], ref T_ringL, (i + 1));
                                    //getPartialPrintsTemplate(printSamples[i], ref partial_T_ringL, (i + 1));
                                    getSample(printSamples[i], ref S_ringL, (i + 1));
                                    bitmap = null;
                                    Convertor.ConvertToPicture(printSamples[i], ref bitmap);
                                    fingerprint = new Fingerprint();
                                    fingerprint.BitmapImage = bitmap;
                                    //st.Start();
                                    AFIS.Extract(fingerprint);
                                    if (fingerprint.Template != null)
                                        partial_T_ringL = fingerprint.Template;
                                    else
                                        partial_T_ringL = null;
                                    break;
                                case 10:
                                    getTemplate(printTemplates[i], ref T_smallL, (i + 1));
                                    //getPartialPrintsTemplate(printSamples[i], ref partial_T_smallL, (i + 1));
                                    getSample(printSamples[i], ref S_smallL, (i + 1));
                                    bitmap = null;
                                    Convertor.ConvertToPicture(printSamples[i], ref bitmap);
                                    fingerprint = new Fingerprint();
                                    fingerprint.BitmapImage = bitmap;
                                    //st.Start();
                                    AFIS.Extract(fingerprint);
                                    if (fingerprint.Template != null)
                                        partial_T_smallL = fingerprint.Template;
                                    else
                                        partial_T_smallL = null;
                                    break;
                            }
                        }
                    }
		}

推荐答案

你不应该处理冻结, 这没有道理。您需要在单独的线程中执行所有耗时的操作。并且线程需要与UI线程交互。怎么做?



你不能从非UI线程调用与UI相关的任何东西。相反,您需要使用 Invoke System.Windows.Threading的方法。 Dispatcher (对于Forms或WPF)或 System.Windows.Forms.Control (仅限表单)。



您将在我过去的答案中找到有关其工作原理和代码示例的详细说明:

Control.Invoke()与Control.BeginInvoke() [ ^ ],

使用Treeview扫描仪和MD5的问题 [ ^ ]。



另请参阅有关线程的更多参考资料:

如何让keydown事件在不同的操作上运行vb.net中的线程 [ ^ ],

在启用禁用+多线程后控制事件未触发 [ ^ ]。



-SA
You should not "handle freezing", it makes no sense. You need to perform all time-consuming operations in a separate thread(s). And the thread would need to interact with the UI thread. How to do it?

You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


不要更改应用程序以处理处理..用户必须等待指纹的评估..如果您使用后台处理程序将动画gif设置为等待..否则,如果使用vs2012,请检查您的代码。尝试分析代码,看看是否有任何问题专门挂着应用程序。
Dont Change the app to handle the processing.. The user has to wait for the evaluation of the fingerprint.. if your using the background handler put a animated gif up "Wait".. Otherwise check your code if using vs2012.. try the "Analyse code" and see if there is anything specifically hanging the app.


这篇关于如何处理应用程序GUI的冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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