Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/09/10 02:57:16 (12 years ago)
Author:
swagner
Message:

Loaded samples asynchronously in start page (#964).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Optimizer/3.3/StartPage.cs

    r3202 r3291  
    2929using HeuristicLab.MainForm;
    3030using HeuristicLab.Persistence.Default.Xml;
     31using System.Threading;
    3132
    3233namespace HeuristicLab.Optimizer {
     
    5455      catch (Exception) { }
    5556
    56       samples = new List<INamedItem>();
     57      samplesListView.Enabled = false;
     58      showStartPageCheckBox.Checked = Properties.Settings.Default.ShowStartPage;
     59      Cursor = Cursors.AppStarting;
     60
     61      ThreadPool.QueueUserWorkItem(new WaitCallback(LoadSamples));
     62    }
     63
     64    protected override void OnClosing(FormClosingEventArgs e) {
     65      base.OnClosing(e);
     66      if (e.CloseReason == CloseReason.UserClosing) {
     67        e.Cancel = true;
     68        this.Hide();
     69      }
     70    }
     71
     72    private void LoadSamples(object state) {
     73      Assembly assembly = Assembly.GetExecutingAssembly();
     74      var samples = assembly.GetManifestResourceNames().Where(x => x.EndsWith(".hl"));
    5775      string path = Path.GetTempFileName();
    58       foreach (string name in assembly.GetManifestResourceNames()) {
    59         if (name.EndsWith(".hl")) {
    60           try {
    61             using (Stream stream = assembly.GetManifestResourceStream(name)) {
    62               WriteStreamToTempFile(stream, path);
    63               IItem item = XmlParser.Deserialize<IItem>(path);
    64               if (item is INamedItem) samples.Add((INamedItem)item);
    65             }
     76      int progress = loadingProgressBar.Maximum / samples.Count();
     77
     78      foreach (string name in samples) {
     79        try {
     80          using (Stream stream = assembly.GetManifestResourceStream(name)) {
     81            WriteStreamToTempFile(stream, path);
     82            IItem item = XmlParser.Deserialize<IItem>(path);
     83            OnSampleLoaded(item as INamedItem, progress);
    6684          }
    67           catch (Exception) { }
     85        }
     86        catch (Exception) { }
     87      }
     88      OnAllSamplesLoaded();
     89    }
     90    private void OnSampleLoaded(INamedItem sample, int progress) {
     91      if (sample != null) {
     92        if (InvokeRequired)
     93          Invoke(new Action<INamedItem, int>(OnSampleLoaded), sample, progress);
     94        else {
     95          ListViewItem item = new ListViewItem(new string[] { sample.Name, sample.Description });
     96          item.ToolTipText = sample.ItemName + " (" + sample.ItemDescription + ")";
     97          samplesListView.SmallImageList.Images.Add(sample.ItemImage);
     98          item.ImageIndex = samplesListView.SmallImageList.Images.Count - 1;
     99          item.Tag = sample;
     100          samplesListView.Items.Add(item);
     101          loadingProgressBar.Value += progress;
    68102        }
    69103      }
    70       foreach (INamedItem sample in samples) {
    71         ListViewItem item = new ListViewItem(new string[] { sample.Name, sample.Description });
    72         item.ToolTipText = sample.ItemName + " (" + sample.ItemDescription + ")";
    73         samplesListView.SmallImageList.Images.Add(sample.ItemImage);
    74         item.ImageIndex = samplesListView.SmallImageList.Images.Count - 1;
    75         item.Tag = sample;
    76         samplesListView.Items.Add(item);
     104    }
     105    private void OnAllSamplesLoaded() {
     106      if (InvokeRequired)
     107        Invoke(new Action(OnAllSamplesLoaded));
     108      else {
     109        samplesListView.Enabled = samplesListView.Items.Count > 0;
     110        if (samplesListView.Items.Count > 0) {
     111          for (int i = 0; i < samplesListView.Columns.Count; i++)
     112            samplesListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
     113        }
     114        loadingPanel.Visible = false;
     115        Cursor = Cursors.Default;
    77116      }
    78       samplesListView.Enabled = samplesListView.Items.Count > 0;
    79       if (samplesListView.Items.Count > 0) {
    80         for (int i = 0; i < samplesListView.Columns.Count; i++)
    81           samplesListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
    82       }
    83 
    84       showStartPageCheckBox.Checked = Properties.Settings.Default.ShowStartPage;
    85117    }
    86118
Note: See TracChangeset for help on using the changeset viewer.