Index: branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis.Views/3.3/Regression/Matlab/RegressionMatlabImportDialog.cs
===================================================================
--- branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis.Views/3.3/Regression/Matlab/RegressionMatlabImportDialog.cs	(revision 15912)
+++ branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis.Views/3.3/Regression/Matlab/RegressionMatlabImportDialog.cs	(revision 15919)
@@ -9,4 +9,5 @@
 using System.Text;
 using System.Threading.Tasks;
+using System.Threading;
 using System.Windows.Forms;
 using HeuristicLab.Common;
@@ -19,4 +20,8 @@
   public partial class RegressionMatlabImportDialog : Form {
     private IMatlabConnector _mlConnector;
+    public event EventHandler<EventArgs<string>> StatusChangedEvent;
+    public event EventHandler<EventArgs<string>> ErrorOccuredEvent;
+
+
 
     /// <summary>
@@ -32,4 +37,5 @@
     /// </summary>
     private Dataset _values;
+
     public RegressionMatlabImportType ImportType {
       get {
@@ -51,5 +57,4 @@
     }
 
-
     public RegressionMatlabImportDialog() {
       InitializeComponent();
@@ -63,5 +68,10 @@
       LoadValuesButton.Click += LoadValuesButtonClick;
       TrainingTestTrackBar.ValueChanged += TrainingTestTrackBarValueChanged;
-
+      StatusChangedEvent += (s, e) => BeginInvoke((MethodInvoker)(() => StatusLabel.Text = e.Value));
+      ErrorOccuredEvent += (s, e) => BeginInvoke((MethodInvoker)(() => {
+        OkButton.Enabled = false;
+        ErrorTextBox.Text = e.Value;
+        ErrorTextBox.Visible = true;
+      }));
     }
 
@@ -78,4 +88,5 @@
     #region Events
 
+
     private void LoadValuesButtonClick(object sender, EventArgs e) {
       LoadValuesAsync();
@@ -87,5 +98,6 @@
       }
       GetVariableNamesAsync();
-    }    
+      ErrorTextBox.Visible = false;
+    }
 
     private void TrainingTestTrackBarValueChanged(object sender, System.EventArgs e) {
@@ -95,4 +107,20 @@
     #endregion
     
+    /// <summary>
+    /// Fires the ErrorOccuredEvent with the given text message.
+    /// </summary>
+    /// <param name="message"></param>
+    private void ErrorOccured(string message) {
+      ErrorOccuredEvent?.Invoke(this, new EventArgs<string>(message));
+    }
+
+    /// <summary>
+    /// Fires the StatusChangedEvent.
+    /// </summary>
+    /// <param name="text"></param>
+    private void UpdateStatus(string text) {
+      StatusChangedEvent?.Invoke(this, new EventArgs<string>(text));
+    }
+
     #region Opening and disposing of the Matlab connector
     private void OpenMatlabConnector() {
@@ -102,12 +130,15 @@
     }
 
-    private void DisposeMatlabConnector() {
-      if (_mlConnector != null) {
-        _mlConnector.Dispose();
-      }
+    private async void DisposeMatlabConnector() {
+      UpdateStatus("Closing Matlab");
+      await Task.Run(() => {
+        if (_mlConnector != null) {
+          _mlConnector.Dispose();
+        }
+      });
     }
     #endregion
 
-    
+
 
     /// <summary>
@@ -115,7 +146,8 @@
     /// </summary>
     private async void GetVariableNamesAsync() {
+      UpdateStatus("Opening Matlab");
+
       var path = openFileDialog.FileName;
       ProblemTextBox.Text = path;
-      
       var variables = await Task.Run(() => {
         OpenMatlabConnector();
@@ -123,4 +155,5 @@
       });
 
+      UpdateStatus("Loading variable names");
       var variablesListBoxItems = VariablesListBox.Items;
       variablesListBoxItems.Clear();
@@ -128,4 +161,5 @@
         variablesListBoxItems.Add(item, false);
       }
+      UpdateStatus("Done");
     }
 
@@ -134,27 +168,23 @@
     /// </summary>
     private async void LoadValuesAsync() {
-      var values = new List<MLTimeseries>();
-      foreach (KeyValuePair<string, MLDatatype> item in VariablesListBox.CheckedItems) {
-        if (item.Value == MLDatatype.Timeseries) {
-          var val = _mlConnector.GetTimeseries(item.Key);
-          if (val != null) {
-            values.Add(val);
+      try {
+        PreviewDatasetMatrix.Content = new Dataset();
+
+        UpdateStatus("Loading values from Matlab");
+        var vals = await Task.Run<List<MLTimeseries>>(() => {
+          var values = new List<MLTimeseries>();
+          foreach (KeyValuePair<string, MLDatatype> item in VariablesListBox.CheckedItems) {
+            if (item.Value == MLDatatype.Timeseries) {
+              var val = _mlConnector.GetTimeseries(item.Key);
+              if (val != null) {
+                values.Add(val);
+              }
+            }
           }
-        }
-      }
-      
-      DisplayLoadedValuesAsync(await Task.Run<MLTimeseries>(() => new MLTimeseries(values)));
-    }
-
-    private async void DisplayLoadedValuesAsync(MLTimeseries timeseries) {
-      PreviewDatasetMatrix.Content = null;
-      try {
-
-        IEnumerable<string> variableNamesWithType = GetVariableNamesWithType(timeseries);
-        var values = await Task.Run<double[,]>(() => GetValuesForDataset(timeseries));
-        _values = new Dataset(variableNamesWithType, values);
-        PreviewDatasetMatrix.Content = _values;
-        SetPossibleTargetVariables(timeseries);
-
+          return values;
+        });
+
+        UpdateStatus("Converting values");
+        DisplayLoadedValuesAsync(await Task.Run<MLTimeseries>(() => new MLTimeseries(vals)));
         ErrorTextBox.Text = String.Empty;
         ErrorTextBox.Visible = false;
@@ -162,63 +192,73 @@
       } catch (Exception ex) {
         if (ex is IOException || ex is InvalidOperationException || ex is ArgumentException) {
-          OkButton.Enabled = false;
-          ErrorTextBox.Text = ex.Message;
-          ErrorTextBox.Visible = true;
+          ErrorOccured(ex.Message);
         } else {
           throw;
         }
       }
-    }
-
+      UpdateStatus("Done");
+    }
+
+    /// <summary>
+    /// Displays the values of a given timeseries at the preview dataset matrix.
+    /// </summary>
+    /// <param name="timeseries"></param>
+    private async void DisplayLoadedValuesAsync(MLTimeseries timeseries) {
+      PreviewDatasetMatrix.Content = null;
+      IEnumerable<string> variableNamesWithType = GetVariableNamesWithType(timeseries);
+      var values = await Task.Run<double[,]>(() => GetValuesForDataset(timeseries));
+      _values = new Dataset(variableNamesWithType, values);
+      PreviewDatasetMatrix.Content = _values;
+      SetPossibleTargetVariables(timeseries);
+    }
+
+    /// <summary>
+    /// Sets the entries of the combobox for the target variable.
+    /// </summary>
+    /// <param name="timeseries"></param>
     protected void SetPossibleTargetVariables(MLTimeseries timeseries) {
       if (timeseries != null) {
-
         TargetVariableComboBox.DataSource = timeseries.DataHeader;
       }
     }
 
+    /// <summary>
+    /// Returns a list with all selected variable names for the preview dataset.
+    /// </summary>
+    /// <param name="timeseries"></param>
+    /// <returns></returns>
     private IEnumerable<string> GetVariableNamesWithType(MLTimeseries timeseries) {
       IList<string> variableNamesWithType = timeseries.DataHeader.ToList();
       variableNamesWithType.Insert(0, "time");
-      /*for (int i = 0; i < variableNamesWithType.Count; i++) {
-        variableNamesWithType[i] += " (Double)";
-      }*/
-     
       return variableNamesWithType;
     }
-
-    private object locker = new object();
 
     /// <summary>
     /// Returns a two dimensional double array which contains the values including the timestamps of the given timeseries.
     /// Array[value, variable] 
+    /// If the amount of the data is too big, a OutOfMemoryException exception is being thrown. 
+    /// This exception is being cought and a message will be shown at the error message box.
     /// </summary>
     /// <param name="timeseries"></param>
     /// <returns></returns>
     private double[,] GetValuesForDataset(MLTimeseries timeseries) {
-      double[,] datasetValues = new double[timeseries.Count, timeseries.DataHeader.Length + 1];
-      var sortedValues = new SortedList<double, double[]>();
-      var times = timeseries.Times;
-      Parallel.For(0, datasetValues.GetLength(0), (idx) => { 
-        var time = times[idx];
-        var values = timeseries.GetValuesByTime(time);
-        lock(locker) {
-          sortedValues.Add(time, values);
-        }
-      });
-
-      int i = 0;
-      foreach (var item in sortedValues) {
-        datasetValues[i, 0] = item.Key;
-        for (int j = 1; j < datasetValues.GetLength(1); j++) {
-          datasetValues[i, j] = item.Value[j - 1];
-        }
-        i++;
-      }
-      return datasetValues;
-    }
-    
-
-
+      try {
+        double[,] datasetValues = new double[timeseries.Count, timeseries.DataHeader.Length + 1];
+
+        int i = 0;
+        foreach (var item in timeseries.Data) {
+          datasetValues[i, 0] = item.Key;
+          for (int j = 1; j < datasetValues.GetLength(1); j++) {
+            datasetValues[i, j] = item.Value[j - 1];
+          }
+          i++;
+        }
+        return datasetValues;
+      } catch (OutOfMemoryException ex) {
+        ErrorOccured(ex.Message + " Amount of the given data is too big.");
+      }
+      return new double[0, timeseries.DataHeader.Length + 1];
+    }
   }
 }
+
