Some Delphi tips
====================================
conversion routines
====================================
Format('There are now %d records in the table %s', [10,'TableA']); -->格式化字符串
function Now(): TDateTime; --> 返回当前的日期和时间, DateTime类型(其实就是double型)
function Date(): TDateTime; --> Returns the current date.
function CompareDateTime(const A, B: TDateTime): int; -->比较2个日期
function EncodeDateTime(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word):
TDateTime; -->拼日期
procedure DecodeDateTime(const AValue: TDateTime; out AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word);
ExtractFilePath(Application.ExeName) -->获取当前程序路径
//简单类型转换
function IntToStr(Value: Integer): string;
function FloatToStr(Value: Extended): string;
Format('%d%s',[100,'hello'])
procedure DateTimeToString(var Result: string; const Format: string; DateTime: TDateTime);
function FormatDateTime(const Format: string; DateTime: TDateTime): string;
格式'yyyymmddhhnnsszzz' 是精确到毫秒的格式
FormatDateTime()和DateTimeToString() 都能很好将日期格式化成String, 但没有那种根据格式解析DateTime的函数, 需要自己写, 或者找开源的rx_library库
//char和string的转换
Char=>String: StringOfChar(char1, 1), 或者 char1+''即可
String=>Char: String使用下标即可转成Char, 比如String123[0]
//PChar和String的互换
String=>PChar
最好使用 PChar('your_string') 强转; 使用StrPCopy(Dest: PChar; const Source: string), 它有时会报内存错误.
function StrPas(const Str: PChar): string;
Converts null-terminated string to an AnsiString (long string)
====================================
how to use Win32 system functions
====================================
function GetTempPath1: string;
var
TempDir: array[0..255] of Char;
//DWORD GetTempPath(DWORD nBufferLength, LPTSTR lpBuffer);
begin
GetTempPath(255, @TempDir);
Result := StrPas(TempDir);
end;
====================================
refresh FORM appearance in a busy loop
====================================
procedure TMainForm.Btn_Stress_Test_LoopClick(Sender: TObject);
var
I: Integer;
begin
for I := 0 to StrToInt(edt_total_count.Text) do
begin
edt_current_index.Text:=IntToStr(I);
self.stressTest();
Application.ProcessMessages();
end;
end;
====================================
解决DCOM中间层报错问题, 报错信息是: Name not unique in this context
====================================
这是一个基于DCOM技术开发的老程序, 包含中间层和客户端, 中间层使用MultiInstance 进程 + Apartment 线程模型, 有一个Remote data module通过BDE技术连接Oracle.
当多个客户端同时连中间层时, 经常能碰到报错: Name not unique in this context.
原因分析: MultiInstance 进程模式的中间层, 也就是一个进程服务所有的RPC调用. 当有多个RPC进来的时候, 启用多线程方式进行服务. 所以需要中间层有一个session管理机制. 默认情况下, BDE中间层仅仅有一个default session, 一旦多个客户端同时连着并作查询, 程序会在同一个session中, 创建数据结果集或其他对象, 就会报"Name not unique in this context."错误.
解决方法是:
首先:Remote data model上, 加 Session1(TSession 组件), 设置AutoSessionName=True, 假设 SessionName为Session1_2 .
接着:确保DBE TDatabase 组件SessionName为Session1的SessionName, 即Session1_2
最后:为Session1 设置PrivateDir. 确保每个连上中间层的session都有一个唯一的PrivateDir, 否则还会报 Directory is busy 错误. 我的做法是在Session1Startup事件中, 为session1动态指定一个临时目录.
测试的策略:
要模拟多个客户端'同时'连接中间层, 需要有点策略. 我的做法是: 随便找一个查询, 循环100次封装成一个方法. 然后启动3份客户端程序, 同时运行这个新的循环程序. 因为有100次循环, 所以必然会有多个客户端同时连接中间层.
增加 TSession 组件,
object Session1: TSession
AutoSessionName = True
KeepConnections = False
PrivateDir = ' '
OnStartup = Session1Startup
SessionName= 'Session1_2'
Left = 32
Top = 104
end
确保DBE TDatabase 组件SessionName为Session1的SessionName, 不是Session1的组件名字.
object your_db: TDatabase
AliasName = 'your_db'
DatabaseName = 'your_db'
KeepConnection = False
LoginPrompt = False
Params.Strings = (
'user name=user1'
'password=pwd')
SessionName = 'Session1_2'
Left = 24
Top = 48
end
procedure TApp.Session1Startup(Sender: TObject);
var
TempDir: array[0..255] of Char;
tempPath:String ;
fullPrivatePath:String;
begin
// set private dir for this new session
GetTempPath(255, @TempDir);
tempPath:= StrPas(TempDir);
fullPrivatePath:=tempPath+guid_str(); //使用guid来保证目录的唯一性
CreateDirectory(PChar(fullPrivatePath),nil);
Session1.PrivateDir:= fullPrivatePath;
end;
Some Delphi tips的更多相关文章
- Delphi Tips
Delphi Tips 函数篇 语法篇 函数篇 StrToDate() function StrToDate(const S: string): TDateTime; function StrToDa ...
- 获取MessageBox按钮本地字符串(OK、Cancel、Yes、No等)
问题仍然由定制MessageBox引发. 定制MessageBox,虽加入自定义些东西,但仍然希望,最大限度接近系统原生框.碰到的问题,就是其钮文本. 即如MessageBox.Show()之Mess ...
- Delphi经验总结(1)
先人的DELPHI基础开发技巧 ◇[DELPHI]网络邻居复制文件 uses shellapi; copyfile(pchar('newfile.txt'),pchar('//computername ...
- delphi 中的浮点数 (转载)
原文地址 Floating point numbers — Sand or dirt Floating point numbers are like piles of sand; every time ...
- Windows XP Manifest in Delphi
Find out how you can include the manifest into a Delphi project to allow your application to share t ...
- [VB.NET Tips]为VB.NET正名
前言 我于2005年毕业,正值全国上上下下如火如荼的开展企业信息化的时代,正是大规模软件开发的年代. 那时.NET 已经发布了2.0,但是仍是VB6,Delphi,PowerBuilder的天下,是E ...
- Mac上MySQL忘记root密码且没有权限的处理办法&workbench的一些tips (转)
忘记Root密码肿么办 Mac上安装MySQL就不多说了,去mysql的官网上下载最新的mysql包以及workbench,先安装哪个影响都不大.如果你是第一次安装,在mysql安装完成之后,会弹出来 ...
- 学习笔记:7z在delphi的应用
最近做个发邮件的功能,需要将日志文件通过邮件发送回来用于分析,但是日志文件可能会超级大,测算下来一天可能会有800M的大小.所以压缩是不可避免了,delphi中的默认压缩算法整了半天不太好使,就看了看 ...
- 【Tips】史上最全H1B问题合辑——保持H1B身份终级篇
[Tips]史上最全H1B问题合辑——保持H1B身份终级篇 2015-04-10留学小助手留学小助手 留学小助手 微信号 liuxue_xiaozhushou 功能介绍 提供最真实全面的留学干货,帮您 ...
随机推荐
- codeforces 613D:Kingdom and its Cities
Description Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. Ho ...
- PowerDesigner给两个表添加reference,中间显示外键信息步骤
如题,我用的是PowerDesigner15,其他的应该也类似. 解决方法: 右击reference,就是那条线,选中format,然后是content->center,在null,name,c ...
- 天气查询SDK
简介: 这是一个用于查询天气的SDK,在很多时候,尤其是对接多而小功能公众账号的时候,天气查询比较使用,此SDK就是这样的用途,使用的是中国天气网的API,已经集成了网上最靠谱的方式来实现,包括里面的 ...
- fbv (FrameBuffer Viewer)编译指南
fbv:FrameBuffer image Viewer,可在控制台下查看jpg,png,gif,bmp等格式的图片,可以结合FBTerm在控制台设置背景图片,也可在编译在嵌入式设备上使用.但是ubu ...
- POJ 2739 Sum of Consecutive Prime Numbers(尺取法)
题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Description S ...
- BZOJ3813: 奇数国
传送门 欧拉函数+线段树 因为只有60个素数,所以把状态压成long long的形式.用线段树维护区间和和区间和中有多少个质数.然后xjb搞搞就行了,具体参见代码. //BZOJ 3813 //by ...
- POJ3207Ikki's Story IV - Panda's Trick(模板题)
题目链接 题意:平面上,一个圆,圆的边上按顺时针放着n个点.现在要连m条边,比如a,b,那么a到b可以从圆的内部连接,也可以从圆的外部连接.给你的信息中,每个点最多只会连接的一条边.问能不能连接这m条 ...
- IT项目管理感悟
做项目的时候,切记做好需求分析,列出需求清单,需要考虑好每一个细节,一个机柜是否需要PDU,KVM,服务器放置位置,放置几台服务器,服务器是几U的,IP段规划都需要事先规划好,并且文档化.---记住这 ...
- JStorm集群的安装和使用
0 JStorm概述 JStorm是一个分布式的实时计算引擎.从应用的角度,JStorm应用是一种遵守某种编程规范的分布式应用:从系统角度, JStorm是一套类似MapReduce的调度系统: 从数 ...
- Java Web之Servlet
Servlet参考文献: 1.http://www.cnblogs.com/luoxn28/p/5460073.html 2.http://www.cnblogs.com/xdp-gacl/p/376 ...