本文主要是介绍界面卡死咋办?多线程来帮忙!,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
写了个超级简单的软件,界面如下:
1. 单线程:
//---------------------------------------------------------------------------#include <vcl.h>
#pragma hdrstop#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner): TForm(Owner)
{
}
//---------------------------------------------------------------------------void __fastcall TForm1::Button1Click(TObject *Sender)
{int i, j;int n = 1;for(i = 0; i < 30000; i++){for(j = 0; j < 30000; j++){n = -n;}}ShowMessage(IntToStr(n));
}
//---------------------------------------------------------------------------
点击按钮后,好卡。
2. 多线程:
//---------------------------------------------------------------------------#include <vcl.h>
#pragma hdrstop#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner): TForm(Owner)
{
}DWORD WINAPI fun(LPVOID lp)
{int i, j;int n = 1;for(i = 0; i < 30000; i++){for(j = 0; j < 30000; j++){n = -n;}}ShowMessage(IntToStr(n));return 0;
}//---------------------------------------------------------------------------void __fastcall TForm1::Button1Click(TObject *Sender)
{HANDLE h = CreateThread(NULL, 0, fun, NULL, 0, NULL);CloseHandle(h);
}
//---------------------------------------------------------------------------
点击按钮后,不卡。
有多线程就是好啊!睡觉去。
这篇关于界面卡死咋办?多线程来帮忙!的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!