Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require("CLRPackage")
- --
- import "System.ComponentModel"
- import "System.Drawing"
- import "System.Threading"
- import "System.Windows.Forms"
- --
- form = Form()
- --
- numberToCompute = 0
- highestPercentageReached = 0
- --
- numericUpDown1 = NumericUpDown()
- startAsyncButton = Button()
- cancelAsyncButton = Button()
- progressBar1 = ProgressBar()
- resultLabel = Label()
- backgroundWorker1 = BackgroundWorker()
- --
- function InitializeBackgroundWorker()
- --
- backgroundWorker1.DoWork:Add(backgroundWorker1_DoWork)
- backgroundWorker1.RunWorkerCompleted:Add(backgroundWorker1_RunWorkerCompleted)
- backgroundWorker1.ProgressChanged:Add(backgroundWorker1_ProgressChanged)
- --
- end
- --
- function startAsyncButton_Click(sender, e)
- --
- resultLabel.Text = String.Empty
- numericUpDown1.Enabled = false
- startAsyncButton.Enabled = false
- cancelAsyncButton.Enabled = true
- numberToCompute = numericUpDown1.Value
- highestPercentageReached = 0
- backgroundWorker1:RunWorkerAsync(numberToCompute)
- --
- end
- --
- function cancelAsyncButton_Click(sender, e)
- --
- backgroundWorker1:CancelAsync()
- cancelAsyncButton.Enabled = false
- --
- end
- --
- function backgroundWorker1_DoWork(sender, e)
- --
- e.Result = ComputeFibonacci(e.Argument, sender, e)
- --
- end
- --
- function backgroundWorker1_RunWorkerCompleted(sender, e)
- --
- -- if e.Error then MessageBox.Show(e.Error.Message)
- -- else
- if e.Cancelled then resultLabel.Text = "Cancelled"
- else
- resultLabel.Text = tostring(e.Result)
- end
- -- end
- --
- numericUpDown1.Enabled = true
- startAsyncButton.Enabled = true
- cancelAsyncButton.Enabled = false
- --
- end
- --
- function backgroundWorker1_ProgressChanged(sender, e)
- --
- progressBar1.Value = e.ProgressPercentage
- --
- end
- --
- function ComputeFibonacci(n, worker, e)
- --
- if (n >= 0 and n <= 91) then
- local result, percentComplete = 0
- if worker.CancellationPending then e.Cancel = true
- else
- if n < 2 then result = n
- else
- result = ComputeFibonacci(n - 1, worker, e) + ComputeFibonacci(n - 2, worker, e)
- end
- percentComplete = math.floor(n / numberToCompute * 100)
- if percentComplete > highestPercentageReached then
- highestPercentageReached = percentComplete
- worker:ReportProgress(percentComplete)
- end
- end
- return result
- end
- --
- end
- --
- function InitializeComponent()
- --
- numericUpDown1:BeginInit()
- form:SuspendLayout()
- numericUpDown1.Location = Point(16, 16)
- numericUpDown1.Maximum = 91
- numericUpDown1.Minimum = 1
- numericUpDown1.Name = "numericUpDown1"
- numericUpDown1.Size = Size(80, 20)
- numericUpDown1.TabIndex = 0
- numericUpDown1.Value = 1
- --
- startAsyncButton.Location = Point(16, 72)
- startAsyncButton.Name = "startAsyncButton"
- startAsyncButton.Size = Size(120, 23)
- startAsyncButton.TabIndex = 1
- startAsyncButton.Text = "Start Async"
- startAsyncButton.Click:Add(startAsyncButton_Click)
- --
- cancelAsyncButton.Enabled = false
- cancelAsyncButton.Location = Point(153, 72)
- cancelAsyncButton.Name = "cancelAsyncButton"
- cancelAsyncButton.Size = Size(119, 23)
- cancelAsyncButton.TabIndex = 2
- cancelAsyncButton.Text = "Cancel Async"
- cancelAsyncButton.Click:Add(cancelAsyncButton_Click)
- --
- resultLabel.BorderStyle = BorderStyle.Fixed3D
- resultLabel.Location = Point(112, 16)
- resultLabel.Name = "resultLabel"
- resultLabel.Size = Size(160, 23)
- resultLabel.TabIndex = 3
- resultLabel.Text = "(no result)"
- resultLabel.TextAlign = ContentAlignment.MiddleCenter
- --
- progressBar1.Location = Point(18, 48)
- progressBar1.Name = "progressBar1"
- progressBar1.Size = Size(256, 8)
- progressBar1.Step = 2
- progressBar1.TabIndex = 4
- --
- backgroundWorker1.WorkerReportsProgress = true
- backgroundWorker1.WorkerSupportsCancellation = true
- --
- form.ClientSize = Size(292, 118)
- form.Controls:Add(progressBar1)
- form.Controls:Add(resultLabel)
- form.Controls:Add(cancelAsyncButton)
- form.Controls:Add(startAsyncButton)
- form.Controls:Add(numericUpDown1)
- form.Name = "FibonacciForm";
- form.Text = "Fibonacci Calculator";
- form.TopMost = true;
- numericUpDown1:EndInit()
- form:ResumeLayout(false)
- --
- end
- --
- function Init()
- --
- InitializeComponent()
- InitializeBackgroundWorker()
- --
- end
- --
- function run()
- --
- function program()
- Application.EnableVisualStyles()
- form:Show()
- Application.Run()
- end
- Init()
- t = Thread(ThreadStart(program()))
- t:SetApartmentState(ApartmentState.STA)
- t:Start()
- --
- end
- --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement