You are browsing the archive for Carlo Kok.

Oxygene by Example – Threading

January 2, 2012 in Uncategorized

Threads are ways to run multiple things in parallel, at the same time. Each program in Oxygene has 1 or more threads, by default just one, which is the main thread, which is the one in which the gui code runs. Using multi-threading allows you to run something in the background, while keeping your main thread responding to actions from the user (in a gui application). It’s also useful when dealing with blocking operations, like sockets and data access. There’s no practical limit to the number of threads that run at once, it depends on the system resources (memory, cpu) how many will work at once.

Threading

Thread Class

The thread class represents the basis for all the other ways of working with threads. The thread API is simple to use. The constructor takes a delegate to run as the method for a thread, the “Start” method makes the thread start for the first time. “Join” waits for the thread to finish.

var lThread := new Thread(method begin
  var lClient := HttpClient;
  try
    var lResponse := lClient.Get('http://blogs.remobjects.com/feed/rss').Content.ReadAsString;
    Invoke(method begin // synchronize to the main thread
      LoadResponseData(lResponse);
    end); 
  except
    on e: Exception do  
      Invoke(method begin
        MessageBox.Show('Failed: '+e);
      end);
  end);
lThread.Start;

ThreadPool Class

The ThreadPool class offers static methods to run something when a CPU is available. It will create up to a certain number of threads (configurable, defaults to the number of CPU cores) and execute any task it gets given to it in order. Using the Thread Pool is simple. The QueueUserWorkItem method takes a delegate with an optional “state” arguments that gets passed to this delegate.

method MainForm.UpdateRssFeed;
begin
  ThreadPool.QueueUserWorkItem(method begin
    var lClient := HttpClient;
    try
      var lResponse := lClient.Get('http://blogs.remobjects.com/feed/rss').Content.ReadAsString;
      Invoke(method begin // synchronize to the main thread
        LoadResponseData(lResponse);
      end); 
    except
      on e: Exception do  
        Invoke(method begin
          MessageBox.Show('Failed: '+e);
        end);
    end;
  end);
end;

Task Class

The Task class was introduced with .NET 4.0 and introduces a new way to deal with threading. A task is an asynchronous operation. There are two classes, the Task class has no return value, while the Task<T> class does. Using the Task class is simple:

var lTask := new Task<String>(method begin
      exit lClient.Get('http://blogs.remobjects.com/feed/rss').Content.ReadAsString;
  end);
lTask.Start; // add to the default task thread pool and run on another thread.
 
lTask.ContinueWith(method (arg: Task<String>) begin
    try
      var lResponse := arg.Result;
      Invoke(method begin // synchronize to the main thread
        LoadResponseData(lResponse);
      end); 
    except
      on e: Exception do  
        Invoke(method begin
          MessageBox.Show('Failed: '+e);
        end);
    end;
  end);

Tasks can, in addition to this, also be waited upon with the “Wait” call, and the result can be read with the “Result” property.

Timers

There are 3 timer classes in .NET. One is in System.Windows.Forms.Timer, which does not run in a thread but runs at an interval in the main thread. The other two are about the same in features, except that the System.Timers.Timer has a synchronization object to talk back to the main form, while the System.Threading.Timer is a bit more lightweight. These classes offer a way to run code at an interval, for example every 5 seconds, or every 100 msec. In addition to thread, the version in the System.Threaded namespace allows specifying an initial interval.

var lTimer := new Timer(method begin
    Console.WriteLine('Timeout!');
  end, nil, 5000, 250);

This timer will keep running until the application closes, or the Dispose method is called. It first triggers after 5 seconds (5000 msec), then after that every 0.25 second.

Futures

The Oxygene language offers Futures which can optionally return and run either asynchronous, like a task, or delayed on the caller thread. Futures have the advantage that they’re a language element that are easily portable. Internally asynchronous futures use Task if it’s available, or the Theadpool class if it’s not. Futures are assignment compatible with Task/Task<T> on .NET 4, and Func/Action on 3.5.

var lAsync := new async lClient.Get('http://blogs.remobjects.com/feed/rss').Content.ReadAsString;
 
...
 
MessageBox.Show(lAsync);

When to use what

While opinions may vary on the subject, I tend to use the System.Threading.Timer for anything that runs multiple times at a constant interval. The futures map to Task or the thread pool depending on the .NET version, which I would use over those two. If for some reason I would not want to use a future, then I’d use the Task class when I’m developing against .NET 4 or above and have a short running thing to do, the Threadpool for short running things on versions that lack the Task class, and Thread for anything else.

Synchronization

Main Thread

When running in a thread, sometimes you have to tell the GUI that data is available. Updating the GUI from the thread is not safe, and usually leads to hard to find errors. The way to deal with this is using Windows Forms “Control.(Begin)Invoke”, or WPF/Silverlights’ “Dispatcher.(Begin)Invoke”. These apis take a delegate and will execute this delegate on the main thread. The Invoke methods will wait for the main thread to be done with the given task, the BeginInvoke methods will not and let the thread continue right away.

Object Access

Accessing a complex object is not generally a safe operation. Most objects in .NET are not thread safe, and will fail with strange errors when accessed from multiple threads at once. Do deal with this, you can use locking or interlocked access to simple fields.

Locking can be done with the “locking” statement. Locking is done on any object instance, generally this is an object not used for anything else.

type
  MySafeStringList = class
  private
    fLock: Object := new Object;
    fList: List<string> := new List<String>;
  public
    method Delete(s: string);
    method Add(s: string); 
    method GetCopy: List<string>;
  end;
 
method MySafeStringList.Delete(s: string);
begin
  locking fLock do fList.Remove(s);
end;
 
method MySafeStringList.Add(s: string); 
begin
  locking fLock do begin
    if not fList.Contains(s) then fList.Add(s);
  end;
end;
 
method MySafeStringList.GetCopy: List<string>;
begin
  locking fLock do begin
    exit new List<String>(fList);
  end;
end;

The locking statement will make sure only 1 thread at any given time have a lock on the object during the time the statement after it runs.

The other way to deal with values safely is with the Interlocked class. This class is limited to a few types and a few operations and makes it possible to exchange, compare, increment and decrement values safely.

Waiting for threads or resources

Sometimes it’s required to wait until some data is available, or to have the thread idle for a specific amount of time, but still make it possible to terminate it safely. An “EventHandle” is useful for this. There are two different event handles, one resets automatically, the other does not. These classes are called AutoResetEvent and ManualResetEvent. An event can have two states: Set and Unset. When it’s unset, the Wait operation will either timeout after a while, if a timeout is given, or wait for it to be set. The Set operation can be called from any thread and lets any possible waiting thread return with reset. At this point it will reset to Unset if it’s an AutoResetEvent, or stay set if it’s a ManualResetEvent. A useful use of this is to terminate a thread:

CheckForUpdatesThread = class
private
  fEvent: ManualResetEvent := new ManualResetEvent(false);
  fThread: Thread;
 
  method Work;
public
  constructor;
  method Stop;
  method CheckForUpdates;
end;
 
method CheckForUpdatesThread.CheckForUpdates;
begin
  ...
end;
 
 
method CheckForUpdatesThread.Work;
begin
  loop begin
    if fEvent.WaitOne(10000) then  // Wait for 10 seconds, returns true if it was set.
      exit;
    CheckForUpdates;
  end;
end;
 
method CheckForUpdatesThread.Stop;
begin
  fEvent.Set;
  fThread.Join; // wait for it to finish
end;
 
constructor CheckForUpdatesThread;
begin
  fThread := new Thread(@Work);
  fThread.Start;
end;

Concurrent Structures

.NET 4.0 introduces several new collections that are safe access from multiple threads. Among these structures are ConcurrentDictionary and ConcurrentStack. These have slightly different methods for adding and retrieving members than their non-thread safe counterparts but provide a fast way to work concurrently with shared data.

Original article

Oxygene by Example – Command line parsing

December 23, 2011 in .NET, Cooper, Mono, Oxygene, Prism, Uncategorized, Wiki

This is second article in my blog post series “Oxygene by Example”.

While working with command line arguments in Prism is quite simple, the “Main” method has an array of string that holds the value, it can become quite complex when having to parse options too. When having to work with those, it’s easiest to use the NDesk for Oxygene file. It’s a single file that can be added to any project (no need for a separate dll library). It was originally written in C# by Jonathan Pryor, we ported it to Oxygene so it can be embedded in an Oxygene project too.

The first step is to add NDesk.Options to the uses list, this is where the classes are defined. A general console application entry point looks like:

class method ConsoleApp.Main(arguments: array of String): Integer;
begin
  var lSourceUri: String;
  var lDestinationUri: String;
  var lSomeOption: Boolean;
  var lShowHelp: Boolean;
  var lOptionSet := new OptionSet(); 
  var lFiles: sequence of String;
 
  lOptionSet.Add("s|source=", "{filename or url} of source object", v -> begin lSourceUri := v end );
  lOptionSet.Add("d|destination=", "{filename or url} of destination object", v -> begin lDestinationUri := v end );
  lOptionSet.Add("o|someoption", "some binary option", v -> begin lSomeOption := assigned(v) end );
  lOptionSet.Add("h|?", "show help", v -> begin lShowHelp := assigned(v); end );
  try
    lFiles := lOptionSet.Parse(arguments);
  except
    on  Exception  do
      lShowHelp := true;
  end;
  if  (lShowHelp)  then begin
    lOptionSet.WriteOptionDescriptions(Console.Out);
    exit 1;
  end else  begin
    Console.WriteLine('Options supplied:');
    Console.WriteLine('Source: ' + lSourceUri);
    Console.WriteLine('Destination: ' + lDestinationUri);
    Console.WriteLine('Some option: ' + iif(lSomeOption,'ON','OFF'));
    for each el in lFiles do Console.WriteLine('Argument: '+el);
  end;
  Console.ReadLine();
end;

The OptionSet class is the main class for the options parser, first you have to define the different options by using the “Add” method. The first parameter defines what option it maps to. in “s|source=”, there are two bindings for this option, “s” and “source”, the | just separates them. The “=” is used to denote that the option takes a parameter. “s” is a single letter option, to use it one can use “MyApp -ssourcefile.text”, or “MyApp –source sourcefile.text”, the short options do not have a space, and a single dash, the long options have a space and two dashes.

The second parameter for Add is the description, when writing out the option list it will print this (see below), the last parameter is a delegate that gets called when it’s triggered. This delegate (in the cases above we use anonymous methods) has one parameter with the value passed. From these anonymous methods we set the options we define in the variables above it, that way we can read them later on.

The last step is lOptionSet.Parse(arguments), this will return a list of “rest” parameters, anything that’s not an option (or parameter to an option) will be in this list. It will raise an exception when there’s an error parsing arguments. We set the lShowHelp to true in this case, so we can later write the options to the console with WriteOptionDescriptions.

Using the Ndesk.Options classes makes parsing parameters a lot easier than doing it manually. The OptionSet classes have a lot more features, all of which are documented at the top of the source file Options.pas.

The original version of this article can be found in our wiki.

Oxygene by Example – Singleton

December 16, 2011 in Uncategorized

This is the first item in my series “Oxygene by Example”, this time about singletons.

Singleton classes are used when there’s only ever one instance of a class needed. They differ from Static Classes in that they have an actual instance, can implement an interface and you can use them as parameters to a method. There are different ways of writing singletons, but the easiest way is by using a readonly class variable:

type
  IObjectCache = interface
    method GetOrCreate(aName: string; aNeedNewValue: Func): T;
  end;
  GlobalObjectCache = class(IObjectCache)
  private
    fCache: Dictionary := new Dictionary;
 
    constructor; empty; // required to be private; so nobody can instantiate it
  public
    class property Instance: MySingleton := new MyObjectCache; readonly;
 
    method GetOrCreate(aName: string; aNeedNewValue: Func): T;
  end;
 
method GlobalObjectCache.GetOrCreate(aName: string; aNeedNewValue: Func): T;
begin
  locking fCache do begin
    var lValue: Object;
    if fCache.TryGetValue(aName, out lValue) then exit T(lValue);
    result := aNeedNewValue();
    fCache.Add(aName, result);
  end;
end;

Using the singleton is simple, GlobalObjectCache.Instance gives access to all instance members of the class:

  lblSystemName.Text := GlobalObjectCache.Instance.GetOrCreate(
    'ComputerName', -> Environment.MachineName);

The latest version of this article can be found Here.

Oxygene by Example

November 23, 2011 in Uncategorized

Last week I started on a series of articles called Oxygene by Example. They’re stored in the wiki at wiki.oxygenelanguage.com/en/Oxygene_by_Example and go over different problems while providing a solution written in Prism. So far I’ve written articles about:

  • Singleton – Single instance class
  • CommandLine – Parsing command line arguments and options
  • Threading – Working with threading in Prism
  • XML – Reading and writing XML files
  • Parsing – A simple expression tokenizer and parser
  • Visitor – A Visitor/Mutator pattern that can be used to do anything with a tree

Suggestions for other things I should write about are welcome, my ideas for further articles are the activator pattern, iterators, the observer pattern, working with linq, working with reflection, sockets and binary trees.

RemObjects (ECMA) Script

May 13, 2011 in .NET, Linux, Mac, Mono, ROFX, Windows

We recently finished a new version of RemObjects Script (Old version, New version), our open source implementation of the ECMAScript (JavaScript) language.

This new version is a complete rewrite that follows the exact rules of ECMAScript 5 to the letter, meaning it conforms to the ECMAScript 5 conformance test suite.

The dependency on the Microsoft Dynamic Language Runtime (DLR) has been dropped and thus speed has improved a lot. The engine is written in Delphi Prism with the Oxygene language, and is now licensed under the BSD license.

The engine was rewritten for two reasons: The original was written loosely based on EcmaScript 3, so the rewrite gives us full V5 support. The second reason was that the DLR was slowing it down a lot, and the abstractions and optimizations in the DLR are overkill for a language as simple as ECMAScript, with only a few internal types. The new version is built based on DynamicMethod and emits IL directly, making it very fast to run.

The RemObjects Script project is a key part of the Business Rules Scripting support in our Data Abstract for .NET product. Just as we released our Internet Pack library as open source, we do the same with RemObjects Script. This means you can freely use RemObjects Script in your Delphi Prism or any .NET project to add automation to your projects, too.

The usage instructions can be found at Using RemObjects Script.

MonoDevelop templates for the Mac

November 4, 2010 in iOS, Mac, MonoDevelop, MonoTouch, Oxygene, Prism, short, Uncategorized

For our MonoDevelop/Mac users we’ve created some new templates for MonoMac and MonoTouch. These are up to date templates that match the C# versions and let you write MonoMac (free) and MonoTouch (commercial) applications.

To install them, open MonoDevelop, select the Tools/Add-in Manager menu, click Repositories and add

https://secure.remobjects.com/api/monodevelop/mac/

to the list of repositories. Then select the Install Add-Ins… button and select the new templates from the update list.

Complete instructions and other extras for Prism can be found at http://remobjects.com/prismextras

Accessing a Data Abstract Server from Javascript

June 17, 2010 in .NET, ROFX

One of the client platforms for Data Abstract that has so far received little attention are JavaScript based web clients.

This morning I wrote a simple JavaScript (dojo toolkit based) web application that talks to an expanded PCTrade Sample Server. It leverages the JsonMessage to talk to the server, using JavaScript code for the simple service generated with Service Builder. The Dojo Toolkit btw is a very nice toolkit for writing javascript web applications.

 

Click on the image to start the video:

Start the video

Details

The Data Abstract Server sample contains a custom ExtendedHttpDispatcher implementation that serves the files in the javascript/ directory part of the sample, so acts like a regular web server when accessing http://127.0.0.1:8099/javascript, sending the files as-is, like a regular web server. The index.html file contains the JavaScript code, part of which is created with Service Builder, using the Tools/CodeGen/JavaScript menu on the Data Abstract Simple Data Service RODL.

Note that due to JavaScript security, the sample only works when accessing it from the same URL as the service itself.

The service provides 2 methods interesting for this web interface:

  • GetSchema: Returns the schema as an xml, used to populate the tree view on the left
  • SimpleGetData: GetData implementation that returns the data in an array of array of string with some Schema information.

The JavaScript application first asks for the schema, and uses XML DOM to find the names of all DataTables and their columns. When clicking a data table it changes the text area to the DA SQL select statment for that table. The “Execute Query” button calls the SimpleGetData service api, and when the callback for the response occurs, fills the grid with the returned data.

Help us Test our new Public Chat Room

April 23, 2010 in non-tech, RemObjects

We’re currently experimenting with a public chat room for RemObjects personnel and customers. This channel is accessible through any Jabber/XMPP client (such as Google Talk, Pidgin, Psi, iChat, Adium, and many more) that lets you join chat rooms. To join, connect to your own jabber server, and find the “Join group chat” (or similar) option in your client. Enter these details:

  • Room: public
  • Server: conf.remobjects.com
  • Handle: (pick a nickname)
  • Password: (none required)

For example in the pidgin client, this looks like this:

Join a Chat.png

Note that you don’t need a special account on our server for this. You can use your existing Google Chat, jabber.com, jabber.org or any other XMPP account.

Please also keep in mind that this channel is not an official support channel. It is a place to hang out and chat with fellow customers and RemObjects employees, but we won’t be there all the time, and we will not guarantee to have time to help you with technical problems or questions. But that doesn’t mean you won’t get answers.

Please let us know what you think.

DA/.NET Client-Side Scripting

April 19, 2010 in .NET, Linux, Mono, Relativity, ROFX

We just finished our dataset scripting support for Data Abstract for .NET. DA now lets you define scripts on the server side (inside the schema), that the client will download and hook up to the events of the dataset. Scripting is supported for both the server and the client, and uses the RemObjects Script engine, which provides the ECMAScript (a.k.a. JavaScript) syntax.

An example, take this script for a “Customers” table:

function beforePost(row) {
  if (row["Discount"] > 50) 
    fail('Discount has to be less than 50');
  if (row["Discount"] < 0) 
    fail('Discount cannot be negative');
}
function onNewRow(row) {
  row["Discount"] = 20;
}

Now any time you fill a DataTable, on a remote data adapter with the script engine property set will receive this JavaScript from the server, and assign them to the datatable:

    var lDs := new System.Data.DataTable();
    lDs.TableName := "Customers";
    RemoteDataAdapter1.Fill(lDs, WhereExpression(nil), true);
 
    var rows := lDs.Select;
 
    rows[0].BeginEdit;
    rows[0]['Discount'] := 60;
    rows[0].EndEdit; // will fail here with a ScriptingException "Discount has to be less than 50"
 
    var lNewRec := lDs.NewRow;
    // lNewRec is preinitialized with a discount of 20 here.

Unit tests for this new feature have been written and all is working nicely. Next will be the same feature for the Linq Remote (and Local) Data Adapter.

Of course, client side scripts are there only to provide immediate user feedback, not to enforce business rules. For this reason, DA will run this business rules script not only on the client, but (where applicable) also on the server. DA also provides a range of additional server-only script events that can implement more extensive business logic – something i will talk more about, soon.

Intercepting RO/.NET HttpServer requests

March 13, 2010 in .NET, RemObjects, ROFX

With the Spring 2010 release, RemObjects SDK for .NET lets you intercept any configurable path on any of the non-super http Server channels and do something completely different with the http request. This feature was added to support REST in Data Abstract, but can also easily be used to host custom pages or whole websites from within any of the http components (IpHttpServerChannel, HttpSysServerChannel, WebProcessor).

How it Works

Each of the HTTP servers implements IHttpServer and exposes a Register and Unregister method. An Extended Http Dispatcher passes it’s implementation of IHttpDispatcher that gets called for each request, returns if it can handle a specific url, and if so, is asked to process the request. Else the next dispatcher from the list gets called, until none are left and the default helpers get called.

Creating Your Own Dispatcher

Writing a custom dispatcher for a given url is quite simple. You simply need to write a new class descending from the ExtendedHttpDispatcher class and override the Process method.

class MyDispatcher: ExtendedHttpDispatcher 
{
  public override void Process(
    IHttpRequest aRequest,
    IHttpResponse aResponse,
    Stream aRequestData,
    Stream aResponseData)
  {
    StreamWriter sw = new StreamWriter(aResponseData);
    aResponse.ContentType = "text/html";
    sw.WriteLine(@"
<html>
  <head>
    <title>Hello World</title>
  </head>
  <body>
    <font size='7'>Hello World</font>
  </body>
</html>
");
    sw.Flush();
  }
}
 
  //...
MyDispatcher disp = new MyDispatcher();
disp.Path = "/hello";
disp.Server = ipHttpServer1;

Given this code, all requests to /hello or ones that start with /hello/ will get a nice “Hello World” page.

REST

The RestSchemaDispatcher class that ships with Data Abstract essentially does the same as above. It intercepts all requests to a given path. the first identifier after the path url is the table name for that request, and anything after that is used to find a record by primary key. More on the features of the RestSchemaDispatcher will be in one of my next blog posts.