Interrupt.cs
1using System;
2 using System.Threading;
3 using GAMS;
4 
5 namespace Interrupt
6 {
14  class Interrupt
15  {
16  static void interruptGams(GAMSJob job)
17  {
18  Thread.Sleep(2000);
19  job.Interrupt();
20  }
21 
22 
23  static void Main(string[] args)
24  {
25  Boolean nonInteractive = false;
26  string sysDir = string.Empty;
27 
28  GAMSWorkspace ws;
29  if (2 == Environment.GetCommandLineArgs().Length)
30  {
31  nonInteractive = String.Equals(Environment.GetCommandLineArgs()[1], "-nonInteractive", StringComparison.OrdinalIgnoreCase);
32  if (!nonInteractive)
33  sysDir = Environment.GetCommandLineArgs()[1];
34  }
35  else if (3 == Environment.GetCommandLineArgs().Length)
36  {
37  nonInteractive = String.Equals(Environment.GetCommandLineArgs()[1], "-nonInteractive", StringComparison.OrdinalIgnoreCase);
38  if (!nonInteractive)
39  {
40  sysDir = Environment.GetCommandLineArgs()[1];
41  nonInteractive = String.Equals(Environment.GetCommandLineArgs()[2], "-nonInteractive", StringComparison.OrdinalIgnoreCase);
42  }
43  else
44  sysDir = Environment.GetCommandLineArgs()[2];
45  }
46 
47  if(string.Empty != sysDir)
48  ws = new GAMSWorkspace(systemDirectory: sysDir);
49  else
50  ws = new GAMSWorkspace();
51 
52  // Use a model-solver combination that needs some time to solve
53  GAMSJob job = ws.AddJobFromGamsLib("circpack");
54  GAMSOptions opt = ws.AddOptions();
55  opt.AllModelTypes = "Lindo";
56 
57  if (nonInteractive)
58  {
59  // Start thread asynchronously that interrupts the GamsJob after 2 seconds
60  Thread waitAndInterrupt = new Thread(new ThreadStart(delegate() { interruptGams(job); }));
61  waitAndInterrupt.Start();
62  }
63  else
64  // Change the default behaviour of pressing Ctrl+C to send that signal to a running job
65  Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs a) { job.Interrupt(); a.Cancel = true; };
66 
67  // Run the job, Pressing Ctrl+C will interrupt the GAMS job, but not this whole application
68  job.Run(opt, output: Console.Out);
69  }
70  }
71 }
bool Interrupt()
GAMSOptions AddOptions(GAMSOptions optFrom=null)
GAMSJob AddJobFromGamsLib(string model, GAMSCheckpoint checkpoint=null, string jobName=null)
void Run(GAMSOptions gamsOptions=null, GAMSCheckpoint checkpoint=null, TextWriter output=null, Boolean createOutDB=true)