Delphi: Thread Variables

A somewhat less known feature of Delphi is thread variables. Thread variables are variables that can contain a different value for each thread in the application. They are defined like regular variables, the only difference is that the “var” keyword is replaced with “threadvar”. In the example below I’ll show you how they work in a small console application. Note that I used the TThread class for convenience, however in reality you’ll only ever need this for procedural code as you can add fields to your TThread subclass to store data in for each thread. The example uses the main thread and a separated thread to show you how it works.

program ThreadVarTest;

{$APPTYPE CONSOLE}

uses
  SysUtils, Classes;

type
  TTestThread = class(TThread)
  protected
    procedure Execute; override;
  public
    constructor Create;
  end;

threadvar
  TestVar: string;

var
  i: Integer;

procedure Test;
begin
  WriteLn('Original TestVar: ' + TestVar);
  TestVar := 'Asdf' + IntToStr(i);
  Inc(i);
  WriteLn('New TestVar: ' + TestVar);
end;

{ TTestThread }

constructor TTestThread.Create;
begin
  inherited Create(False);
  FreeOnTerminate := True;
end;

procedure TTestThread.Execute;
begin
  Test;
end;

begin
  i := 0;
  Test;
  WriteLn('Starting thread.');
  TTestThread.Create;
  ReadLn;
  WriteLn(TestVar);
  ReadLn; // Prevent window from closing
end.

As you can see when running it, even though the content of the thread variable was changed, the variable was still empty when accessed by the new thread. I’ve added the incremental number to show you that when after you press a key when the thread has exited and you check the thread variable content for the main thread, it will show Adsf0 as it was stored by the main thread originaly.

Read More

RAD Studio XE Preview #2

The 2nd RAD Studio XE preview has been released today! After viewing it I find myself a bit disappointed as there is still no mentioning of the cross-platform compiling which was the most anticipated feature of the product. Although there has been no video release of the #3 preview yet which is planned for the 24th, they did already release details on what it’s about, still no mentioning of cross-platform though.

Embarcadero has been building a lot of 3rd party applications into RAD Studio XE, in the 1st preview they already showed the integration of 3rd party difference software and now they’ve added Raize CodeSite, a piece of logging software from their technology partner Raize Software which develops Delphi components and tools, the AQTime profiler and FinalBuilder. The upside to doing this integration is the obvious fact that they don’t have to develop their own versions of these products from scratch but instead can use finished products that have already gone through a lot of releases making them very stable en reliable. The downside is that most of these products are somewhat limited in use unless you purchase their extended versions.

What drew my attention mostly in this preview was the addition of FinalBuilder, it allows you to schedule automated builds. As i understand it, it has been integrated into all of the RAD Studio products which will enable you to create automated builds very easily.


Read More

Delphi: Decimal color to RGB and back

Converting a decimal color value (24-bit) to RGB in Delphi is easy using bitshifts, the reverse is true as well. Here’s how it’s done:

procedure ColorToRGB(const Color: Integer; out R, G, B: Byte);
begin
  R := Color and $FF;
  G := (Color shr 8) and $FF;
  B := (Color shr 16) and $FF;
end;

function RGBToColor(const R, G, B: Byte): Integer;
begin
  Result := R or (G shl 8) or (B shl 16);
end;

Take in mind that Delphi TColor is a 24-bit integer decimal color representation.

Read More

CodeRage 5

CodeRage 5 is slowly getting closer. For those of you who are not familiar with CodeRage, it’s a yearly online technical conference for software  development, hosted by Embarcadero Technologies. The conference focuses on all of the Embarcadero products with speakers from all over the world. If you missed CodeRage 4 last year, you can find all sessions from back then on the embarcadero website (http://conferences.embarcadero.com/coderage/sessions). CodeRage 5 is planned for the 4th of October this year and will last 4 days, it will include over 90 free technical sessions. Embarcadero is currently accepting submissions of abstracts for people to be considered as a speaker on the conference. I’ll be keeping a close eye on the conference once it’s start and keep you updated on some of the topics brought forward there

Read More

RemObjects Internet Pack for .NET

RemObjects is the company that provided the Oxygene compiler technology that forms the base of Delphi Prism, the new Delphi.NET which integrates with Microsoft Visual Studio. They also have several products which rely a lot on sockets. To make things easier they built a custom library on top of the .NET socket library which contains a bunch of components which make it very easy to use sockets for various purposes. One of these components for example is the HttpClient component which lets you grab the contents of a webpage by simply calling it’s Get method and passing an URL to it. Next to this there’s also HttpServer, FtpClient, FtpServer and more! The components are included with Delphi Prism by default, but for those of you who use other Visual Studio languages it’s certainly recommended!

Find it here: http://www.remobjects.com/ip.aspx

Read More

Java: Reading and writing text files

A lot of people starting off with Java will often want to read or write text files. Here’s some code to do just that:

public static String readFile(String fileName) throws IOException {
	FileInputStream fis = new FileInputStream(fileName);
	BufferedInputStream bis = new BufferedInputStream(fis, 1024);
	byte[] buffer = new byte[1024];
	String result = "";
	int len;
	while ((len = bis.read(buffer)) != -1) {
		byte[] tmp = new byte[len];
		System.arraycopy(buffer, 0, tmp, 0, len);
		result += new String(tmp);
	}
	bis.close();
	return result;
}

public static void writeFile(String fileName, String content)
		throws IOException {
	FileOutputStream fos = new FileOutputStream(fileName);
	BufferedOutputStream bos = new BufferedOutputStream(fos, 1024);
	bos.write(content.getBytes());
	bos.close();
}

Read More

Embarcadero RAD Studio XE

So like a lot of people out there, I’ve been anxiously waiting for the next Embarcadero RAD Studio. Many will agree that since the 2009 release, the product has made great leaps forward. There’s been a lot of secrecy around the newest version 2011 though. The main component of the product, Delphi was codenamed Fulcrum and a few but barely any details were leaked about the project, however, the intention was to include cross-compiling support for Mac OSX and Linux in this release. I personally would have preferred they had gone for x64 support first, but I suppose cross-platform would be nice as well. However fairly soon they updated their roadmap and they had split off the Linux support from this release, leaving in only support for Mac OSX. On the 13th of August they finally released a first sneak peak of some of the new features in RAD Studio XE, which they had named it rather than 2011. I’m however somewhat disappointed to say that there was not the slightest mention of cross-platform compiling in this sneak peak. On the 17th they will release the next in a series of 3 previews, so I guess we better wait for those.

So what’s new in RAD Studio XE?
I’m not going to include an entire feature list here, you can find most of these on the preview page, however I must say I’m personally very excited about 2 additions to RAD Studio. The first of which is subversion support. A lot of people using other VCS systems like Hg, Git and CVS will probably feel a bit left out here, but subversion is one of the most popular ones out there, it will be quite nice to have it tied into the work environment. The second thing that really got my attention was that they finally included “Delphi for PHP”, now named RadPHP in RAD Studio. Up until now Delphi for PHP has remained a separated product, but this new generation of the product which seems to have made tremendous progress has now been included in RAD Studio, making the RAD Studio package an even sweeter deal. RadPHP’s VCL features a lot of great components with even FaceBook support.  We’ll probably find out more in 2 days when the next preview has been released, but even though most people will be disappointed not to find any details on the cross-platform support, for a first preview it seems like they made a lot of progress since the last release.


Read More