形式冻结形式 [英] Form freezing form

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

问题描述

为什么在这段代码中,*在获得焦点/一段时间后不会刷新*


#using< mscorlib.dll>

#using< system.windows.forms.dll>

#using< system.dll>

#using< system.drawing.dll>

#include< windows.h>

#include< cstdlib>

// ==>只是一个带有两个标签的表单,一个进度条,不透明度为90%。

// ==> label1和progressBar是公共的,可以通过main()访问。

// ==>放置了一些刷新但问题仍然存在。

__gc类Form1:public System :: Windows :: Forms :: Form

{

private:

系统:: Windows :: Forms :: Label * label2;


public:

System :: Windows :: Forms :: ProgressBar * progressBar1;

系统:: Windows :: Forms :: Label * label1;


Form1()

{

this-> label2 = new System :: Windows :: Forms :: Label();

this-> progressBar1 = new System :: Windows :: Forms :: ProgressBar();

this-> label1 = new System :: Windows :: Forms :: Label();


this-> ; label2 = new System :: Windows :: Forms :: Label();

this-> progressBar1 = new System :: Windows :: Forms :: ProgressBar();

this-> label1 = new System :: Windows :: Forms :: Label();

this-> SuspendLayout();


this-> label2-> Font = new System :: Drawing :: Font(S" Microsoft Sans Serif",9.75F,

System :: Drawing :: FontStyle :: Regul ar,System :: Drawing :: GraphicsUnit :: Point,(System :: Byte)161);

this-> label2-> Location = System :: Drawing :: Point(14, 16);

this-> label2-> Name = S" label2";

this-> label2-> Size = System :: Drawing ::大小(264,40);

this-> label2-> TabIndex = 1;

this-> label2-> Text = S" Message 1。 .." ;;

this-> label2-> TextAlign = System :: Drawing :: ContentAlignment :: MiddleCenter;


this-> progressBar1-> Location = System :: Drawing :: Point(10,176);

this-> progressBar1-> Name = S" progressBar1";

this-> progressBar1-> Size = System :: Drawing :: Size(272,23);

this-> progressBar1-> TabIndex = 2;


this-> label1-> Font = new System :: Drawing :: Font(S" Microsoft Sans Serif",8.25F,

System :: Drawing :: FontStyle: :Regular,System :: Drawing :: GraphicsUnit :: Point,(System :: Byte)161);

this-> label1-& gt; Location = System :: Drawing :: Point(14,80);

this-> label1-> Name = S" label1";

this- > label1->尺寸=系统::绘图::尺寸(264,64);

this-> label1-> TabIndex = 3;


this-> AutoScaleBaseSize = System :: Drawing :: Size(5,13);

this-> ClientSize = System :: Drawing :: Size(292,271);

this-> Controls-> Add(this-> label1);

this-> Controls-> Add(this-> progressBar1);

this-> Controls-> Add(this-> label2);

this-> FormBorderStyle = System :: Windows :: Forms :: FormBorderStyle :: Fixed3D;


this-> Name = S" Form1";

this-> Opacity = 0.9;

this - > ShowInTaskbar = false;

this-> Text = S" Some Application";

this-> TopMost = true;

this-> ResumeLayout(false);

}

};

int main()试试

{

Form1 * pForm1 = __gc new Form1;


pForm1-> Show();

pForm1-> Refresh();


pForm1-> label1 - > Text =" Diagnostic code";

pForm1-> progressBar1-> Value = 0;

pForm1-> progressBar1-> Maximum = 100 ;


while(true)

{

for(long i = 0; I< 100; ++ i)

{

pForm1-> progressBar1-> Increment(1);

pForm1-> Refresh();

}


pForm1-> progressBar1->值= 0;

}


}


catch(系统::例外* pe)

{

使用命名空间系统;


Console :: WriteLine(" Error:{0}",pe-> Message);


返回EXIT_FAILURE;

}

Why in this code the form *does not refresh* when it gets the focus/after some time?

#using <mscorlib.dll>
#using <system.windows.forms.dll>
#using <system.dll>
#using <system.drawing.dll>
#include <windows.h>
#include <cstdlib>
// ==> Just a Form with two labels, one progress bar, opacity 90%.
// ==> label1, and progressBar are public and accessed through main().
// ==> Placed some refreshes but the problem persists.
__gc class Form1: public System::Windows::Forms::Form
{
private:
System::Windows::Forms::Label * label2;

public:
System::Windows::Forms::ProgressBar * progressBar1;
System::Windows::Forms::Label * label1;

Form1()
{
this->label2 = new System::Windows::Forms::Label();
this->progressBar1 = new System::Windows::Forms::ProgressBar();
this->label1 = new System::Windows::Forms::Label();

this->label2 = new System::Windows::Forms::Label();
this->progressBar1 = new System::Windows::Forms::ProgressBar();
this->label1 = new System::Windows::Forms::Label();
this->SuspendLayout();

this->label2->Font = new System::Drawing::Font(S"Microsoft Sans Serif", 9.75F,
System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, (System::Byte)161);
this->label2->Location = System::Drawing::Point(14, 16);
this->label2->Name = S"label2";
this->label2->Size = System::Drawing::Size(264, 40);
this->label2->TabIndex = 1;
this->label2->Text = S"Message 1...";
this->label2->TextAlign = System::Drawing::ContentAlignment::MiddleCenter;

this->progressBar1->Location = System::Drawing::Point(10, 176);
this->progressBar1->Name = S"progressBar1";
this->progressBar1->Size = System::Drawing::Size(272, 23);
this->progressBar1->TabIndex = 2;

this->label1->Font = new System::Drawing::Font(S"Microsoft Sans Serif", 8.25F,
System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, (System::Byte)161);
this->label1->Location = System::Drawing::Point(14, 80);
this->label1->Name = S"label1";
this->label1->Size = System::Drawing::Size(264, 64);
this->label1->TabIndex = 3;

this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
this->ClientSize = System::Drawing::Size(292, 271);
this->Controls->Add(this->label1);
this->Controls->Add(this->progressBar1);
this->Controls->Add(this->label2);
this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::Fixed3D;

this->Name = S"Form1";
this->Opacity = 0.9;
this->ShowInTaskbar = false;
this->Text = S"Some Application";
this->TopMost = true;
this->ResumeLayout(false);
}
};
int main()try
{
Form1 *pForm1= __gc new Form1;

pForm1->Show();
pForm1->Refresh();

pForm1->label1->Text= "Diagnostic code";
pForm1->progressBar1->Value= 0;
pForm1->progressBar1->Maximum= 100;

while(true)
{
for(long i= 0; i< 100; ++i)
{
pForm1->progressBar1->Increment(1);
pForm1->Refresh();
}

pForm1->progressBar1->Value= 0;
}

}

catch(System::Exception *pe)
{
using namespace System;

Console::WriteLine("Error: {0}", pe->Message);

return EXIT_FAILURE;
}

推荐答案

Ioannis Vranos写道:


如何调用Application :: DoEvents( )而不是pForm1-> Refresh()?

DoEvents处理消息队列中的所有消息并返回。它的价格几乎和多线程一样好,但它非常简单并且可以防止你的应用程序因为你正在处理的东西而冻结了



持续超过100毫秒。定期调用DoEvent,但不要经常调用
,因为它会降低你的应用程序速度。

Ioannis Vranos wrote:

How about calling Application::DoEvents() instead of pForm1->Refresh()?
DoEvents processes all messages in the message queue and returns. It''s
not nearly as good as multithreading, but it''s very simple and prevents
your application from freezing while you are processing something that
lasts more than a 100 milliseconds. Call DoEvents periodically, but not
too often, because it slows down your application.

为什么在这段代码中表单*不会刷新*当它在一段时间后获得焦点/
时(真)
{
for(long i = 0; i< 100; ++ i) {
pForm1-> progressBar1-> Increment(1);
pForm1-> Refresh();


//<<<<<<<<<<<<<<<<<<<< <<<

Application :: DoEvents();

//<<<<<<<<<<<< ;<<<<<<<<<<<

}

pForm1-> progressBar1-> Value = 0;
}

}
Why in this code the form *does not refresh* when it gets the
focus/after some time?
while(true)
{
for(long i= 0; i< 100; ++i)
{
pForm1->progressBar1->Increment(1);
pForm1->Refresh();
// <<<<<<<<<<<<<<<<<<<<<
Application::DoEvents();
// <<<<<<<<<<<<<<<<<<<<<
}

pForm1->progressBar1->Value= 0;
}

}




Tom



Tom


Tamas Demjen写道:
Tamas Demjen wrote:
如何调用Application :: DoEvents()而不是pForm1-> Refresh()?
DoEvents处理消息队列中的所有消息并返回。它不像多线程那么好,但它非常简单并且在你处理的持续时间超过100毫秒的时候会阻止你的应用程序冻结。定期调用DoEvent,但不要经常调用,因为它会减慢你的应用程序。
How about calling Application::DoEvents() instead of pForm1->Refresh()?
DoEvents processes all messages in the message queue and returns. It''s
not nearly as good as multithreading, but it''s very simple and prevents
your application from freezing while you are processing something that
lasts more than a 100 milliseconds. Call DoEvents periodically, but not
too often, because it slows down your application.




感谢小费Tamas。它似乎解决了这个问题,但是存在另一个问题。

当我按下X按钮关闭表格时,申请流程不会终止。任何想法?

这是更正后的代码:

#using< mscorlib.dll>

#using< system.windows.forms .dll>

#using< system.dll>

#using< system.drawing.dll>

#include< windows .h>

#include< cstdlib>

// ==>只是一个带有两个标签的表单,一个进度条,不透明度为90%。

// ==> label1和progressBar是公共的,可以通过main()访问。

// ==>放置了一些刷新但问题仍然存在。

__gc类Form1:public System :: Windows :: Forms :: Form

{

private:

系统:: Windows :: Forms :: Label * label2;


public:

System :: Windows :: Forms :: ProgressBar * progressBar1;

系统:: Windows :: Forms :: Label * label1;


Form1()

{

this-> label2 = new System :: Windows :: Forms :: Label();

this-> progressBar1 = new System :: Windows :: Forms :: ProgressBar();

this-> label1 = new System :: Windows :: Forms :: Label();


this-> ; label2 = new System :: Windows :: Forms :: Label();

this-> progressBar1 = new System :: Windows :: Forms :: ProgressBar();

this-> label1 = new System :: Windows :: Forms :: Label();

this-> SuspendLayout();


this-> label2-> Font = new System :: Drawing :: Font(S" Microsoft Sans Serif",

9.75F,System :: Drawing :: FontStyle :: Regul ar,System :: Drawing :: GraphicsUnit :: Point,

(System :: Byte)161);

this-> label2-> Location = System: :Drawing :: Point(14,16);

this-> label2-> Name = S" label2";

this-> label2-> Size = System :: Drawing :: Size(264,40);

this-> label2-> TabIndex = 1;

this-> label2-> ; Text = S" Message 1 ..." ;;

this-> label2-> TextAlign = System :: Drawing :: ContentAlignment :: MiddleCenter;


this-> progressBar1-> Location = System :: Drawing :: Point(10,176);

this-> progressBar1-> Name = S" progressBar1" ;;

this-> progressBar1-> Size = System :: Drawing :: Size(272,23);

this-> progressBar1-> TabIndex = 2 ;


this-> label1-> Font = new System :: Drawing :: Font(S" Microsoft Sans Serif",

8.25F, System :: Drawing :: FontStyle :: Regular,System :: Drawing :: GraphicsUnit :: Point,

(System :: Byte)161);

this-> label1-> Location = System :: Drawing :: Point(14,80);

this-> label1-> Name = S" label1" ;;

this-> label1-> Size = System :: Drawing :: Size(264,64);

this-> label1-> TabIndex = 3 ;


this-> AutoScaleBaseSize = System :: Drawing :: Size(5,13);

this-> ClientSize = System :: Drawing ::大小(292,271);

this-> Controls-> Add(this-> label1);

this-> Controls->添加(this-> progressBar1);

this-> Controls-> Add(this-> label2);

this-> FormBorderStyle = System: :Windows :: Forms :: FormBorderStyle :: Fixed3D;


this-> Name = S" Form1";

this-> Opacity = 0.9 ;

this-> ShowInTaskbar = false;

this-> Text = S" Some Application";

this-> TopMost = true;

this-> ResumeLayout(false);

}

};

int main( )使用namespa尝试

{

ce系统;

使用命名空间System :: Windows :: Forms;

使用命名空间System :: Threading;


Form1 * pForm1 = __gc new Form1;


pForm1-> Show();


pForm1-> label1-> Text ="诊断代码;

pForm1-> progressBar1->值= 0;

pForm1-> progressBar1->最大值= 100;


while(true)

{

for(long i = 0; I< 100; ++ i)

{

pForm1-> progressBar1->增量(1);


if(i% 10 == 0)

Application :: DoEvents();


Thread :: Sleep(100);

}


pForm1-> progressBar1->值= 0;

}


}


catch(系统::异常* pe)

{

使用命名空间系统;


控制台:: WriteLine(" Error:{0}",pe-> Message);


返回EXIT_FAILURE;

}



Thanks for the tip Tamas. It appears that it solves the problem, but another one exists.
When I close the Form by pressing the X button, the application process does not
terminate. Any ideas?
Here is the corrected code:
#using <mscorlib.dll>
#using <system.windows.forms.dll>
#using <system.dll>
#using <system.drawing.dll>
#include <windows.h>
#include <cstdlib>
// ==> Just a Form with two labels, one progress bar, opacity 90%.
// ==> label1, and progressBar are public and accessed through main().
// ==> Placed some refreshes but the problem persists.
__gc class Form1: public System::Windows::Forms::Form
{
private:
System::Windows::Forms::Label * label2;

public:
System::Windows::Forms::ProgressBar * progressBar1;
System::Windows::Forms::Label * label1;

Form1()
{
this->label2 = new System::Windows::Forms::Label();
this->progressBar1 = new System::Windows::Forms::ProgressBar();
this->label1 = new System::Windows::Forms::Label();

this->label2 = new System::Windows::Forms::Label();
this->progressBar1 = new System::Windows::Forms::ProgressBar();
this->label1 = new System::Windows::Forms::Label();
this->SuspendLayout();

this->label2->Font = new System::Drawing::Font(S"Microsoft Sans Serif",
9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
(System::Byte)161);
this->label2->Location = System::Drawing::Point(14, 16);
this->label2->Name = S"label2";
this->label2->Size = System::Drawing::Size(264, 40);
this->label2->TabIndex = 1;
this->label2->Text = S"Message 1...";
this->label2->TextAlign = System::Drawing::ContentAlignment::MiddleCenter;

this->progressBar1->Location = System::Drawing::Point(10, 176);
this->progressBar1->Name = S"progressBar1";
this->progressBar1->Size = System::Drawing::Size(272, 23);
this->progressBar1->TabIndex = 2;

this->label1->Font = new System::Drawing::Font(S"Microsoft Sans Serif",
8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
(System::Byte)161);
this->label1->Location = System::Drawing::Point(14, 80);
this->label1->Name = S"label1";
this->label1->Size = System::Drawing::Size(264, 64);
this->label1->TabIndex = 3;

this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
this->ClientSize = System::Drawing::Size(292, 271);
this->Controls->Add(this->label1);
this->Controls->Add(this->progressBar1);
this->Controls->Add(this->label2);
this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::Fixed3D;

this->Name = S"Form1";
this->Opacity = 0.9;
this->ShowInTaskbar = false;
this->Text = S"Some Application";
this->TopMost = true;
this->ResumeLayout(false);
}
};
int main()try
{
using namespace System;
using namespace System::Windows::Forms;
using namespace System::Threading;

Form1 *pForm1= __gc new Form1;

pForm1->Show();

pForm1->label1->Text= "Diagnostic code";
pForm1->progressBar1->Value= 0;
pForm1->progressBar1->Maximum= 100;

while(true)
{
for(long i= 0; i< 100; ++i)
{
pForm1->progressBar1->Increment(1);

if(i%10== 0)
Application::DoEvents();

Thread::Sleep(100);
}

pForm1->progressBar1->Value= 0;
}

}

catch(System::Exception *pe)
{
using namespace System;

Console::WriteLine("Error: {0}", pe->Message);

return EXIT_FAILURE;
}


Ioannis Vranos写道:
Ioannis Vranos wrote:
感谢小费Tamas。它似乎解决了这个问题,但是存在另一个问题。当我按下X按钮关闭表单时,
申请过程不会终止。任何想法?
Thanks for the tip Tamas. It appears that it solves the problem, but
another one exists. When I close the Form by pressing the X button, the
application process does not terminate. Any ideas?




如果我没有弄错,你的主要功能是永远循环。这是实际应用程序中不常见的
。通常你的处理迟早会结束

。如果你想取消一个持久的

操作,你可以有一个取消按钮,它会在点击时设置一个

IsCancelled标志。然后你的主循环可以检查这个

标志。这就是典型应用程序的工作方式。


我几乎总是使用线程,而不是DoEvents,因为

线程具有自动负载平衡,你不要我们必须弄清楚每次调用DoEvents的频率是多少。调用

DoEvents也是危险的,因为它可能会调用你没准备好的其他函数

(它可能会产生严重的副作用,不需要的递归等等。 )。


使用DoEvents时,你的应用程序不会像使用线程一样顺畅和敏感。

。你经常调用DoEvents,这会减慢你的处理速度,或者你很少调用它,这会让GUI在处理过程中变得迟钝。线程没有这样的问题,因为系统

调度程序非常智能,你甚至可以为线程分配优先级。

分配低优先级你可以指示系统如果你有

时间就行,否则不要。


Tom



If I''m not mistaken, your main function if in a forever loop. This is
not typical in real-world applications. Usually your processing finishes
sooner or later. If you want to be able to cancel a long lasting
operation, you could have a Cancel button, which would set an
IsCancelled flag on click. Your main loop could then check against this
flag. This is how typical applications work.

I almost always perfer using threads, as opposed to DoEvents, because
threads have automatic load balancing, and you don''t have to figure out
how often DoEvents should be called. It''s also dangerous to call
DoEvents, because it may call other functions you are not prepared for
(it can have serious side effects, unwanted recursion, etc.).

When using DoEvents, your application won''t be as smooth and responsive
as using threads. You either call DoEvents too often, which slows down
your processing, or you call it too rarely, which makes the GUI sluggish
during processing. There is no such problem with threads, as the system
scheduler is very smart, and you can even assign priorities to threads.
Assigning low priority you can instruct the system to "do it if you have
time, otherwise don''t".

Tom


这篇关于形式冻结形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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