我的一个程序需要大量的运算,在此期间我发现程序窗口根本不
响应外部的任何动作,任务管理器也是显示该程序没有响应
但是我把它放在DOS下运行却没有这样的问题存在。
请问是为什么?
一直占用主线程
多线程
在你的循环中加入Application->ProcessMessage();
BCB帮助 ProcessMessage
This example uses two buttons that are long enough to accommodate lengthy captions on a form. When the user clicks the button with the caption Ignore Messages, the code begins to generate a long series of random numbers. If the user tries to resize the form while the handler is running, nothing happens until the handler is finished. When the user clicks the button with the caption Process Messages, more random numbers are generated, but Windows can still respond to a series of mouse events, such as resizing the form.
Note: How quickly these event handlers run depends on the microprocessor of your computer. A message appears on the form informing you when the handler has finished executing.
void __fastcall TForm1::FormCreate(TObject* Sender)
{
Button1->Caption = "Ignore Messages";
Button2->Caption = "Handle Message";
}
void __fastcall TForm1::Button1Click(TObject* Sender)
{
int x, y;
for (int i = 0; i < 64000; i++)
{
Randomize();
for (int j = 0; j < 64000; j++)
y = random(j);
x = random(i);
}
Canvas->TextOut(10, 10, "The Button1Click handler is finished");
}
void __fastcall TForm1::Button2Click(TObject *Sender)
{
int x, y;
for (int i = 0; i < 64000; i++)
{
Randomize();
for (int j = 0; j < 64000; j++)
{
y = random(j);
Application->ProcessMessages();
}
x = random(i);
}
Canvas->TextOut(10, 10, "The Button2Click handler is finished");
}
不要在你的处理过程中响应消息
用多线程是最好的办法,加ProcessMessage太浪费了。
我看不一定,不知道他的东西是不是线程安全的。
还有,象你这样用的大量运算的,可以考虑用matlab的库了。