diff --git a/Cancellation.sln b/Cancellation.sln new file mode 100644 index 0000000..c677070 --- /dev/null +++ b/Cancellation.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.572 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cancellation", "Cancellation\Cancellation.csproj", "{AF200868-81B6-45E4-A5CD-3F14AFA403A7}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {AF200868-81B6-45E4-A5CD-3F14AFA403A7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AF200868-81B6-45E4-A5CD-3F14AFA403A7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AF200868-81B6-45E4-A5CD-3F14AFA403A7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AF200868-81B6-45E4-A5CD-3F14AFA403A7}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {3C8ED39C-328D-4152-B606-0824F4785633} + EndGlobalSection +EndGlobal diff --git a/Cancellation/App.config b/Cancellation/App.config new file mode 100644 index 0000000..731f6de --- /dev/null +++ b/Cancellation/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Cancellation/Cancellation.csproj b/Cancellation/Cancellation.csproj new file mode 100644 index 0000000..4122bf4 --- /dev/null +++ b/Cancellation/Cancellation.csproj @@ -0,0 +1,83 @@ + + + + + Debug + AnyCPU + {AF200868-81B6-45E4-A5CD-3F14AFA403A7} + WinExe + Cancellation + Cancellation + v4.6.1 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + Form + + + Form1.cs + + + + + Form1.cs + + + ResXFileCodeGenerator + Resources.Designer.cs + Designer + + + True + Resources.resx + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + True + Settings.settings + True + + + + + + + \ No newline at end of file diff --git a/Cancellation/Form1.Designer.cs b/Cancellation/Form1.Designer.cs new file mode 100644 index 0000000..35d5537 --- /dev/null +++ b/Cancellation/Form1.Designer.cs @@ -0,0 +1,134 @@ +namespace Cancellation +{ + partial class Form1 + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.maxValueTxt = new System.Windows.Forms.TextBox(); + this.label1 = new System.Windows.Forms.Label(); + this.calculateBtn = new System.Windows.Forms.Button(); + this.label2 = new System.Windows.Forms.Label(); + this.largestPrimeTxt = new System.Windows.Forms.TextBox(); + this.calculatingLbl = new System.Windows.Forms.Label(); + this.cancelBtn = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // maxValueTxt + // + this.maxValueTxt.Location = new System.Drawing.Point(150, 33); + this.maxValueTxt.Name = "maxValueTxt"; + this.maxValueTxt.Size = new System.Drawing.Size(227, 26); + this.maxValueTxt.TabIndex = 0; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(92, 36); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(38, 20); + this.label1.TabIndex = 1; + this.label1.Text = "Max"; + // + // calculateBtn + // + this.calculateBtn.Font = new System.Drawing.Font("Microsoft Sans Serif", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.calculateBtn.Location = new System.Drawing.Point(278, 75); + this.calculateBtn.Name = "calculateBtn"; + this.calculateBtn.Size = new System.Drawing.Size(99, 37); + this.calculateBtn.TabIndex = 2; + this.calculateBtn.Text = "Calculate"; + this.calculateBtn.UseVisualStyleBackColor = true; + this.calculateBtn.Click += new System.EventHandler(this.calculateBtn_Click); + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(23, 190); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(107, 20); + this.label2.TabIndex = 3; + this.label2.Text = "Largest Prime"; + // + // largestPrimeTxt + // + this.largestPrimeTxt.Location = new System.Drawing.Point(150, 187); + this.largestPrimeTxt.Name = "largestPrimeTxt"; + this.largestPrimeTxt.Size = new System.Drawing.Size(227, 26); + this.largestPrimeTxt.TabIndex = 4; + // + // calculatingLbl + // + this.calculatingLbl.AutoSize = true; + this.calculatingLbl.Location = new System.Drawing.Point(223, 147); + this.calculatingLbl.Name = "calculatingLbl"; + this.calculatingLbl.Size = new System.Drawing.Size(103, 20); + this.calculatingLbl.TabIndex = 5; + this.calculatingLbl.Text = "Calculating ..."; + this.calculatingLbl.Visible = false; + // + // cancelBtn + // + this.cancelBtn.Enabled = false; + this.cancelBtn.Location = new System.Drawing.Point(183, 75); + this.cancelBtn.Name = "cancelBtn"; + this.cancelBtn.Size = new System.Drawing.Size(89, 37); + this.cancelBtn.TabIndex = 6; + this.cancelBtn.Text = "Cancel"; + this.cancelBtn.UseVisualStyleBackColor = true; + this.cancelBtn.Click += new System.EventHandler(this.cancelBtn_Click); + // + // Form1 + // + this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(410, 241); + this.Controls.Add(this.cancelBtn); + this.Controls.Add(this.calculatingLbl); + this.Controls.Add(this.largestPrimeTxt); + this.Controls.Add(this.label2); + this.Controls.Add(this.calculateBtn); + this.Controls.Add(this.label1); + this.Controls.Add(this.maxValueTxt); + this.Name = "Form1"; + this.Text = "Prime Calculator"; + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.TextBox maxValueTxt; + private System.Windows.Forms.Label label1; + private System.Windows.Forms.Button calculateBtn; + private System.Windows.Forms.Label label2; + private System.Windows.Forms.TextBox largestPrimeTxt; + private System.Windows.Forms.Label calculatingLbl; + private System.Windows.Forms.Button cancelBtn; + } +} + diff --git a/Cancellation/Form1.cs b/Cancellation/Form1.cs new file mode 100644 index 0000000..57e629e --- /dev/null +++ b/Cancellation/Form1.cs @@ -0,0 +1,91 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Cancellation +{ + public partial class Form1 : Form + { + CancellationTokenSource cts; + + public Form1() + { + InitializeComponent(); + } + + private void calculateBtn_Click(object sender, EventArgs e) + { + cts = new CancellationTokenSource(TimeSpan.FromSeconds(30)); + var token = cts.Token; + + Task.Run(() => + { + long max = -1; + this.Invoke((Action)(() => + { + largestPrimeTxt.Clear(); + calculateBtn.Enabled = false; + cancelBtn.Enabled = true; + calculatingLbl.Visible = true; + max = long.Parse(this.maxValueTxt.Text); + })); + + long prime = -1; + try + { + prime = FindLargestPrimeNumber(max, token); + } + catch (OperationCanceledException) + { } + + this.Invoke((Action) (() => + { + calculateBtn.Enabled = true; + cancelBtn.Enabled = false; + calculatingLbl.Visible = false; + + if (prime != -1) + { + largestPrimeTxt.Text = prime.ToString(); + } + })); + + cts.Dispose(); + }, token); + } + + private long FindLargestPrimeNumber(long max, CancellationToken token = default(CancellationToken)) + { + var result = 1; + for (int i = 2; i <= max; i++) + { + token.ThrowIfCancellationRequested(); + if (IsPrime(i, token)) + { + result = i; + } + } + return result; + } + + private bool IsPrime(int num, CancellationToken token = default(CancellationToken)) + { + for (int i = 2; i < num; i++) + { + token.ThrowIfCancellationRequested(); + if (num % i == 0) + { + return false; + } + } + + return true; + } + + private void cancelBtn_Click(object sender, EventArgs e) + { + cts.Cancel(); + } + } +} diff --git a/Cancellation/Form1.resx b/Cancellation/Form1.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/Cancellation/Form1.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Cancellation/Program.cs b/Cancellation/Program.cs new file mode 100644 index 0000000..f76f3a4 --- /dev/null +++ b/Cancellation/Program.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Cancellation +{ + static class Program + { + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main() + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new Form1()); + } + } +} diff --git a/Cancellation/Properties/AssemblyInfo.cs b/Cancellation/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..fad4ca0 --- /dev/null +++ b/Cancellation/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Cancellation")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Cancellation")] +[assembly: AssemblyCopyright("Copyright © 2019")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("af200868-81b6-45e4-a5cd-3f14afa403a7")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Cancellation/Properties/Resources.Designer.cs b/Cancellation/Properties/Resources.Designer.cs new file mode 100644 index 0000000..66da1a9 --- /dev/null +++ b/Cancellation/Properties/Resources.Designer.cs @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Cancellation.Properties +{ + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources + { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() + { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager + { + get + { + if ((resourceMan == null)) + { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Cancellation.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture + { + get + { + return resourceCulture; + } + set + { + resourceCulture = value; + } + } + } +} diff --git a/Cancellation/Properties/Resources.resx b/Cancellation/Properties/Resources.resx new file mode 100644 index 0000000..af7dbeb --- /dev/null +++ b/Cancellation/Properties/Resources.resx @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Cancellation/Properties/Settings.Designer.cs b/Cancellation/Properties/Settings.Designer.cs new file mode 100644 index 0000000..c3a64d9 --- /dev/null +++ b/Cancellation/Properties/Settings.Designer.cs @@ -0,0 +1,30 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Cancellation.Properties +{ + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase + { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default + { + get + { + return defaultInstance; + } + } + } +} diff --git a/Cancellation/Properties/Settings.settings b/Cancellation/Properties/Settings.settings new file mode 100644 index 0000000..3964565 --- /dev/null +++ b/Cancellation/Properties/Settings.settings @@ -0,0 +1,7 @@ + + + + + + +