在 Delphi VCL 应用程序中,我想在执行一个耗时的过程时创建一个“等待”消息窗口(本例中是一个大循环)。
在开始耗时的过程之前,我已经尝试了以下要执行的事情。
- 创建一个包含消息的简单窗口的新形式。
- 使用 messagedlg 创建消息。
- 甚至更改主窗体上的 TLabel.Caption (执行耗时过程的那个)。
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,
popUpMessage;
type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
end;
var
Form1: TForm1;
dialog : TForm;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var i, j, k :LongInt;
begin
{1}
popUpMessage.Form2 := TForm2.Create(nil); //also tried with Create(self)
Form2.show;
{2}
dialog := CreateMessageDialog ('Wait',TMsgDlgType.mtWarning, mbYesNoCancel);
dialog.Show;
{3}
messagedlg('Wait',mtError, mbOKCancel, 0); …Run Code Online (Sandbox Code Playgroud) 我在 Visual Studio 中使用以下代码来存储构造函数参数 char* 并在我的类的解构中将其删除。
#include "pch.h"
#include <iostream>
class A
{
public:
A(const char *fn) {
myChar = _strdup(fn);
}
~A() {
delete[] myChar;
}
char *myChar;
};
int main()
{
A myA("lala");
myA.~A();
_CrtDumpMemoryLeaks(); //leak detector
}
Run Code Online (Sandbox Code Playgroud)
如果我不使用 delete[] myChar,则会造成内存泄漏。如果我使用它,我会在调试时遇到这个未知错误。
这样做的正确方法是什么?
更改delete[]为 后free:
我需要读取未知类型和大小的文件的内容,并将其临时保存(以某种变量),因此稍后将其用于通过串行端口进行传输。据我了解,TFileStream是正确的方法。
我确实尝试从http://docwiki.embarcadero.com/CodeExamples/Tokyo/en/TReader_(Delphi)实现以下教程
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Utils;
type
TForm1 = class(TForm)
procedure OnCreate(Sender: TObject);
private
selectedFile: string;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.OnCreate(Sender: TObject);
function ReadFileContent(fileName: String): String;
var
FileStream: TFileStream;
Reader: TReader;
tByte :byte;
begin
FileStream := TFileStream.Create(fileName, fmOpenRead);
Reader := TReader.Create(FileStream, $FF);
Reader.ReadListBegin; //I get 'Invalid property Value' error
//in this line raised from the Reader object
while not Reader.EndOfList do
begin
Reader.ReadVar(tByte, …Run Code Online (Sandbox Code Playgroud) delphi ×2
asynchronous ×1
c++ ×1
char ×1
memory ×1
memory-leaks ×1
non-modal ×1
tfilestream ×1