Loading...
Searching...
No Matches
Form1.cs
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Linq;
7using System.Text;
8using System.Windows.Forms;
9using System.Runtime.InteropServices;
10using System.Diagnostics;
11using System.Threading.Tasks;
12using GAMS;
13using System.IO;
14
15namespace InterruptGui
16{
25 public partial class Form1 : Form
26 {
27 GAMSJob currentJob = null;
28 public Form1()
29 {
30 InitializeComponent();
31 this.ControlBox = false;
32 }
33
34 private void RunGams()
35 {
36 MethodInvoker action = delegate
37 {
38 bu_run.Enabled = false;
39 bu_close.Enabled = false;
40 richTextBox1.Clear();
41 };
42 this.BeginInvoke(action);
43
44 try
45 {
46 TextBoxBaseWriter tbw = new TextBoxBaseWriter(this.richTextBox1, this);
47
49 ws.GamsLib("lop");
50 currentJob = ws.AddJobFromFile("lop.gms");
51 GAMSOptions opt = ws.AddOptions();
52 opt.AllModelTypes = "soplex";
53 opt.SolveLink = GAMSOptions.ESolveLink.CallModule;
54
55 action = delegate
56 {
57 bu_cancel.Enabled = true;
58 };
59 this.BeginInvoke(action);
60
61 currentJob.Run(opt, tbw);
62 }
63 catch (Exception e)
64 {
65 action = delegate
66 {
67 richTextBox1.AppendText(e.Message);
68 };
69 this.BeginInvoke(action);
70 }
71 finally
72 {
73 currentJob = null;
74 }
75
76 action = delegate
77 {
78 bu_run.Enabled = true;
79 bu_close.Enabled = true;
80 bu_cancel.Enabled = false;
81 };
82 this.BeginInvoke(action);
83 }
84
85 private void bu_run_Click(object sender, EventArgs e)
86 {
87 Task.Factory.StartNew(() => RunGams());
88 }
89
90 private void bu_cancel_Click(object sender, EventArgs e)
91 {
92 currentJob?.Interrupt();
93 }
94
95 private void bu_close_Click(object sender, EventArgs e)
96 {
97 Close();
98 }
99 }
100}
bool Interrupt()
void Run(GAMSOptions gamsOptions=null, GAMSCheckpoint checkpoint=null, TextWriter output=null, Boolean createOutDB=true)
void GamsLib(string model)
GAMSJob AddJobFromFile(string fileName, GAMSCheckpoint checkpoint=null, string jobName=null)
GAMSOptions AddOptions(GAMSOptions optFrom=null)
This small example demonstrates how to run a GAMS model in a graphical user interface....
Definition Form1.cs:26