1 | using System;
|
---|
2 | using System.Drawing;
|
---|
3 | using System.Threading;
|
---|
4 | using System.Windows.Forms;
|
---|
5 | using HeuristicLab.Clients.Hive;
|
---|
6 | using HeuristicLab.Common;
|
---|
7 | using HeuristicLab.Core;
|
---|
8 | using HeuristicLab.MainForm;
|
---|
9 | using HeuristicLab.Optimization;
|
---|
10 |
|
---|
11 |
|
---|
12 | namespace HeuristicLab.HiveJobUploader {
|
---|
13 | public partial class HiveJobUploaderForm : Form {
|
---|
14 | Semaphore step = new Semaphore(0, 1);
|
---|
15 |
|
---|
16 | public HiveJobUploaderForm() {
|
---|
17 | InitializeComponent();
|
---|
18 | ContentManager.Initialize(new PersistenceContentManager());
|
---|
19 | }
|
---|
20 |
|
---|
21 | private void btnAdd_Click(object sender, EventArgs e) {
|
---|
22 | if (openFileDialog1.ShowDialog() == DialogResult.OK) {
|
---|
23 | foreach (string name in openFileDialog1.FileNames) {
|
---|
24 | if (!lbxHiveTasks.Items.Contains(name)) {
|
---|
25 | lbxHiveTasks.Items.Add(name);
|
---|
26 | }
|
---|
27 | }
|
---|
28 | }
|
---|
29 | }
|
---|
30 |
|
---|
31 | private void HandleEx(Exception ex) {
|
---|
32 | MessageBox.Show(ex.ToString());
|
---|
33 | }
|
---|
34 |
|
---|
35 | private void HiveJobUploaderForm_Load(object sender, EventArgs e) {
|
---|
36 | uploadProgress.Visible = false;
|
---|
37 | openFileDialog1.Multiselect = true;
|
---|
38 | lblState.BackColor = Color.Transparent;
|
---|
39 | toolTip1.SetToolTip(btnAdd, "Add Files");
|
---|
40 | toolTip1.SetToolTip(btnUpload, "Upload the files added");
|
---|
41 | }
|
---|
42 |
|
---|
43 | private void UpdatelblState(string message) {
|
---|
44 | if (InvokeRequired) {
|
---|
45 | lblState.Invoke(new Action<string>(UpdatelblState), message);
|
---|
46 | } else
|
---|
47 | lblState.Text = message;
|
---|
48 | }
|
---|
49 |
|
---|
50 | private void UpdateProgressbar(int i) {
|
---|
51 | if (InvokeRequired) {
|
---|
52 | uploadProgress.Invoke(new Action<int>(UpdateProgressbar), i);
|
---|
53 | } else {
|
---|
54 | double progressValue = lbxHiveTasks.Items.Count;
|
---|
55 | uploadProgress.Visible = true;
|
---|
56 | uploadProgress.Minimum = 0;
|
---|
57 | uploadProgress.Maximum = 100;
|
---|
58 | uploadProgress.Value = (int)Math.Round(i /( progressValue) * 100);
|
---|
59 | uploadProgress.Step = 1;
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | private void UploadFile(string file) {
|
---|
64 | IStorableContent content = ContentManager.Load(file);
|
---|
65 | IOptimizer opt = (IOptimizer)content;
|
---|
66 | if (opt == null)
|
---|
67 | throw new Exception("File does not contain an IOptimizer!");
|
---|
68 | HiveClient.Instance.Refresh();
|
---|
69 | RefreshableJob refJob = new RefreshableJob();
|
---|
70 | refJob.HiveTasks.Add(new OptimizerHiveTask(opt));
|
---|
71 | HiveClient.StartJob(new Action<Exception>(HandleEx)
|
---|
72 | , refJob, new System.Threading.CancellationToken());
|
---|
73 | step.Release();
|
---|
74 | }
|
---|
75 |
|
---|
76 | private void btnUpload_Click(object sender, EventArgs e) {
|
---|
77 | if (InvokeRequired) Invoke((Action<object, EventArgs>)btnUpload_Click);
|
---|
78 | else {
|
---|
79 | int i = 1;
|
---|
80 | uploadProgress.Visible = true;
|
---|
81 | foreach (string item in lbxHiveTasks.Items) {
|
---|
82 | Thread uploader = new Thread(() => UploadFile(item));
|
---|
83 | uploader.IsBackground = true;
|
---|
84 | uploader.Start();
|
---|
85 | UpdatelblState( "Uploading " + item);
|
---|
86 | UpdateProgressbar(i);
|
---|
87 | step.WaitOne();
|
---|
88 | i++;
|
---|
89 | }
|
---|
90 | UpdatelblState("Uploading Finished");
|
---|
91 | uploadProgress.Visible = false;
|
---|
92 | lbxHiveTasks.Items.Clear();
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | }
|
---|
97 | }
|
---|