Index: trunk/sources/HeuristicLab.CEDMA.DB.Interfaces/AgentEntry.cs
===================================================================
--- trunk/sources/HeuristicLab.CEDMA.DB.Interfaces/AgentEntry.cs	(revision 391)
+++ trunk/sources/HeuristicLab.CEDMA.DB.Interfaces/AgentEntry.cs	(revision 392)
@@ -37,4 +37,6 @@
     public string Name { get; set; }
     [DataMember]
+    public DateTime CreationTime { get; set; }
+    [DataMember]
     public ProcessStatus Status { get; set; }
     [DataMember]
Index: trunk/sources/HeuristicLab.CEDMA.DB/Database.cs
===================================================================
--- trunk/sources/HeuristicLab.CEDMA.DB/Database.cs	(revision 391)
+++ trunk/sources/HeuristicLab.CEDMA.DB/Database.cs	(revision 392)
@@ -55,5 +55,5 @@
             }
             using(DbCommand cmd = cnn.CreateCommand()) {
-              cmd.CommandText = "CREATE TABLE Agent (ID integer primary key autoincrement, ProjectId integer, ParentAgentId integer, Name text, Status text default 'Unknown', ControllerAgent integer, RawData Blob)";
+              cmd.CommandText = "CREATE TABLE Agent (ID integer primary key autoincrement, ProjectId integer, ParentAgentId integer, Name text, Status text default "+ProcessStatus.Unknown+", ControllerAgent integer, CreationTime DateTime, RawData Blob)";
               cmd.Transaction = t;
               cmd.ExecuteNonQuery();
@@ -83,5 +83,5 @@
             using(DbCommand c = cnn.CreateCommand()) {
               c.Transaction = t;
-              c.CommandText = "Insert into Agent (Name, ParentAgentId, ControllerAgent, RawData) values (@Name, @ParentAgentId, @ControllerAgent, @RawData); select last_insert_rowid()";
+              c.CommandText = "Insert into Agent (Name, ParentAgentId, ControllerAgent, CreationTime, RawData) values (@Name, @ParentAgentId, @ControllerAgent, @CreationTime, @RawData); select last_insert_rowid()";
               DbParameter nameParam = c.CreateParameter();
               nameParam.ParameterName = "@Name";
@@ -96,4 +96,8 @@
               controllerParam.Value = controllerAgent;
               c.Parameters.Add(controllerParam);
+              DbParameter creationTimeParam = c.CreateParameter();
+              creationTimeParam.ParameterName = "@CreationTime";
+              creationTimeParam.Value = DateTime.Now;
+              c.Parameters.Add(creationTimeParam);
               DbParameter dataParam = c.CreateParameter();
               dataParam.ParameterName = "@RawData";
@@ -236,5 +240,5 @@
               DbParameter idParam = c.CreateParameter();
               statusParam.ParameterName = "@Status";
-              statusParam.Value = status;
+              statusParam.Value = status.ToString();
               idParam.ParameterName = "@Id";
               idParam.Value = id;
@@ -278,84 +282,4 @@
     }
 
-    //public void UpdateRunStart(long runId, DateTime startTime) {
-    //  rwLock.EnterWriteLock();
-    //  try {
-    //    using(SQLiteConnection cnn = new SQLiteConnection(connectionString)) {
-    //      cnn.Open();
-    //      using(SQLiteTransaction t = cnn.BeginTransaction()) {
-    //        using(SQLiteCommand c = cnn.CreateCommand()) {
-    //          c.Transaction = t;
-    //          c.CommandText = "Update Run set StartTime=@StartTime where id=@Id";
-    //          DbParameter startTimeParam = c.CreateParameter();
-    //          DbParameter idParam = c.CreateParameter();
-    //          startTimeParam.ParameterName = "@StartTime";
-    //          startTimeParam.Value = startTime;
-    //          idParam.ParameterName = "@Id";
-    //          idParam.Value = runId;
-    //          c.Parameters.Add(startTimeParam);
-    //          c.Parameters.Add(idParam);
-    //          c.ExecuteNonQuery();
-    //        }
-    //        t.Commit();
-    //      }
-    //    }
-    //  } finally {
-    //    rwLock.ExitWriteLock();
-    //  }
-    //}
-
-    //public void UpdateRunFinished(long runId, DateTime finishedTime) {
-    //  rwLock.EnterWriteLock();
-    //  try {
-    //    using(SQLiteConnection cnn = new SQLiteConnection(connectionString)) {
-    //      cnn.Open();
-    //      using(SQLiteTransaction t = cnn.BeginTransaction()) {
-    //        using(SQLiteCommand c = cnn.CreateCommand()) {
-    //          c.Transaction = t;
-    //          c.CommandText = "Update Run set FinishedTime=@FinishedTime where id=@Id";
-    //          DbParameter finishedTimeParam = c.CreateParameter();
-    //          DbParameter idParam = c.CreateParameter();
-    //          finishedTimeParam.ParameterName = "@FinishedTime";
-    //          finishedTimeParam.Value = finishedTime;
-    //          idParam.ParameterName = "@Id";
-    //          idParam.Value = runId;
-    //          c.Parameters.Add(finishedTimeParam);
-    //          c.Parameters.Add(idParam);
-    //          c.ExecuteNonQuery();
-    //        }
-    //        t.Commit();
-    //      }
-    //    }
-    //  } finally {
-    //    rwLock.ExitWriteLock();
-    //  }
-    //}
-
-    //public void UpdateRunStatus(long runId, ProcessStatus status) {
-    //  rwLock.EnterWriteLock();
-    //  try {
-    //    using(SQLiteConnection cnn = new SQLiteConnection(connectionString)) {
-    //      cnn.Open();
-    //      using(SQLiteTransaction t = cnn.BeginTransaction()) {
-    //        using(SQLiteCommand c = cnn.CreateCommand()) {
-    //          c.Transaction = t;
-    //          c.CommandText = "Update Run set Status=@Status where id=@Id";
-    //          DbParameter statusParam = c.CreateParameter();
-    //          DbParameter idParam = c.CreateParameter();
-    //          statusParam.ParameterName = "@Status";
-    //          statusParam.Value = status;
-    //          idParam.ParameterName = "@Id";
-    //          idParam.Value = runId;
-    //          c.Parameters.Add(statusParam);
-    //          c.Parameters.Add(idParam);
-    //          c.ExecuteNonQuery();
-    //        }
-    //        t.Commit();
-    //      }
-    //    }
-    //  } finally {
-    //    rwLock.ExitWriteLock();
-    //  }
-    //}
     #endregion
 
@@ -372,5 +296,5 @@
           DbParameter statusParameter = c.CreateParameter();
           statusParameter.ParameterName = "@Status";
-          statusParameter.Value = status;
+          statusParameter.Value = status.ToString();
           c.Parameters.Add(statusParameter);
 
@@ -448,68 +372,4 @@
       return agents;
     }
-
-    //public ICollection<RunEntry> GetRuns(long agentId) {
-    //  List<RunEntry> runs = new List<RunEntry>();
-    //  rwLock.EnterReadLock();
-    //  try {
-    //    using(DbConnection cnn = new SQLiteConnection(connectionString)) {
-    //      cnn.Open();
-    //      using(DbCommand c = cnn.CreateCommand()) {
-    //        c.CommandText = "Select Id, AgentId, CreationTime, Status, Rawdata from Run where AgentId=@AgentId";
-    //        DbParameter agentParameter = c.CreateParameter();
-    //        agentParameter.ParameterName = "@AgentId";
-    //        agentParameter.Value = agentId;
-    //        c.Parameters.Add(agentParameter);
-
-    //        using(DbDataReader r = c.ExecuteReader()) {
-    //          while(r.Read()) {
-    //            RunEntry run = new RunEntry();
-    //            run.Id = r.GetInt32(0);
-    //            run.AgentId = r.GetInt32(1);
-    //            run.CreationTime = r.GetDateTime(2);
-    //            run.Status = (ProcessStatus)Enum.Parse(typeof(ProcessStatus), r.GetString(3));
-    //            run.RawData = (byte[])r.GetValue(4);
-    //            runs.Add(run);
-    //          }
-    //        }
-    //      }
-    //    }
-    //  } finally {
-    //    rwLock.ExitReadLock();
-    //  }
-    //  return runs;
-    //}
-
-    //public ICollection<RunEntry> GetRuns(ProcessStatus status) {
-    //  List<RunEntry> runs = new List<RunEntry>();
-    //  rwLock.EnterReadLock();
-    //  try {
-    //    using(DbConnection cnn = new SQLiteConnection(connectionString)) {
-    //      cnn.Open();
-    //      using(DbCommand c = cnn.CreateCommand()) {
-    //        c.CommandText = "Select Id, AgentId, CreationTime, Status, Rawdata from Run where Status=@Status";
-    //        DbParameter statusParameter = c.CreateParameter();
-    //        statusParameter.ParameterName = "@Status";
-    //        statusParameter.Value = status;
-    //        c.Parameters.Add(statusParameter);
-
-    //        using(DbDataReader r = c.ExecuteReader()) {
-    //          while(r.Read()) {
-    //            RunEntry run = new RunEntry();
-    //            run.Id = r.GetInt32(0);
-    //            run.AgentId = r.GetInt32(1);
-    //            run.CreationTime = r.GetDateTime(2);
-    //            run.Status = (ProcessStatus)Enum.Parse(typeof(ProcessStatus), r.GetString(3));
-    //            run.RawData = (byte[])r.GetValue(4);
-    //            runs.Add(run);
-    //          }
-    //        }
-    //      }
-    //    }
-    //  } finally {
-    //    rwLock.ExitReadLock();
-    //  }
-    //  return runs;
-    //}
 
     public ICollection<ResultEntry> GetResults(long agentId) {
Index: trunk/sources/HeuristicLab.CEDMA.Server/RunScheduler.cs
===================================================================
--- trunk/sources/HeuristicLab.CEDMA.Server/RunScheduler.cs	(revision 391)
+++ trunk/sources/HeuristicLab.CEDMA.Server/RunScheduler.cs	(revision 392)
@@ -45,4 +45,5 @@
     private object queueLock = new object();
     private Queue<Job> jobQueue;
+    private AutoResetEvent runningJobs = new AutoResetEvent(false);
 
     public RunScheduler(Database database, JobManager jobManager) {
@@ -80,4 +81,5 @@
         lock(queueLock) {
           jobQueue.Enqueue(job);
+          runningJobs.Set();
         }
       }
@@ -87,17 +89,17 @@
       try {
         while(true) {
-          int runningJobs;
-          lock(queueLock) runningJobs = jobQueue.Count;
-          if(runningJobs==0) Thread.Sleep(1000); // TASK: replace with waithandle
+          Job job = null;
+          lock(queueLock) if(jobQueue.Count > 0) job = jobQueue.Dequeue();
+          if(job == null) runningJobs.WaitOne();
           else {
-            Job job;
-            lock(queueLock) {
-              job = jobQueue.Dequeue();
-            }
             job.WaitHandle.WaitOne();
             job.WaitHandle.Close();
             lock(remoteCommLock) {
-              jobManager.EndExecuteOperation(job.Operation);
-              database.UpdateAgent(job.AgentId, ProcessStatus.Finished);
+              try {
+                jobManager.EndExecuteOperation(job.Operation);
+                database.UpdateAgent(job.AgentId, ProcessStatus.Finished);
+              } catch(JobExecutionException ex) {
+                database.UpdateAgent(job.AgentId, ProcessStatus.Error);
+              }
             }
           }
