Advertisement
Anaristos

Fibonacci Backgroundworker

Feb 4th, 2013
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.53 KB | None | 0 0
  1. require("CLRPackage")
  2. --
  3. import "System.ComponentModel"
  4. import "System.Drawing"
  5. import "System.Threading"
  6. import "System.Windows.Forms"
  7. --
  8. form = Form()
  9. --
  10. numberToCompute          = 0
  11. highestPercentageReached = 0
  12. --
  13. numericUpDown1    = NumericUpDown()
  14. startAsyncButton  = Button()
  15. cancelAsyncButton = Button()
  16. progressBar1      = ProgressBar()
  17. resultLabel       = Label()
  18. backgroundWorker1 = BackgroundWorker()
  19. --
  20. function InitializeBackgroundWorker()
  21. --
  22.     backgroundWorker1.DoWork:Add(backgroundWorker1_DoWork)
  23.     backgroundWorker1.RunWorkerCompleted:Add(backgroundWorker1_RunWorkerCompleted)
  24.     backgroundWorker1.ProgressChanged:Add(backgroundWorker1_ProgressChanged)
  25. --
  26. end
  27. --
  28. function startAsyncButton_Click(sender, e)
  29. --
  30.     resultLabel.Text          = String.Empty
  31.     numericUpDown1.Enabled    = false
  32.     startAsyncButton.Enabled  = false
  33.     cancelAsyncButton.Enabled = true
  34.    
  35.     numberToCompute           = numericUpDown1.Value
  36.     highestPercentageReached  = 0
  37.    
  38.     backgroundWorker1:RunWorkerAsync(numberToCompute)
  39. --
  40. end
  41. --
  42. function cancelAsyncButton_Click(sender, e)
  43. --
  44.     backgroundWorker1:CancelAsync()
  45.    
  46.     cancelAsyncButton.Enabled = false
  47. --
  48. end
  49. --
  50. function backgroundWorker1_DoWork(sender, e)
  51. --
  52.     e.Result = ComputeFibonacci(e.Argument, sender, e)
  53. --
  54. end
  55. --
  56. function backgroundWorker1_RunWorkerCompleted(sender, e)
  57. --
  58. --  if e.Error then MessageBox.Show(e.Error.Message)
  59. --  else
  60.         if e.Cancelled then resultLabel.Text = "Cancelled"
  61.         else
  62.             resultLabel.Text = tostring(e.Result)
  63.         end
  64. --  end
  65. -- 
  66.     numericUpDown1.Enabled    = true
  67.     startAsyncButton.Enabled  = true
  68.     cancelAsyncButton.Enabled = false
  69. --
  70. end
  71. --
  72. function backgroundWorker1_ProgressChanged(sender, e)
  73. --
  74.     progressBar1.Value = e.ProgressPercentage
  75. --
  76. end
  77. --
  78. function ComputeFibonacci(n, worker, e)
  79. --
  80.     if (n >= 0 and n <= 91) then
  81.         local result, percentComplete = 0  
  82.         if worker.CancellationPending then e.Cancel = true
  83.             else
  84.             if n < 2 then result = n
  85.             else
  86.                 result = ComputeFibonacci(n - 1, worker, e) + ComputeFibonacci(n - 2, worker, e)
  87.             end
  88.             percentComplete = math.floor(n / numberToCompute * 100)
  89.             if percentComplete > highestPercentageReached then
  90.                 highestPercentageReached = percentComplete
  91.                 worker:ReportProgress(percentComplete)
  92.             end
  93.         end
  94.         return result
  95.     end
  96. --
  97. end
  98. --
  99. function InitializeComponent()
  100. --       
  101.     numericUpDown1:BeginInit()
  102.    
  103.     form:SuspendLayout()
  104.    
  105.     numericUpDown1.Location = Point(16, 16)
  106.     numericUpDown1.Maximum  = 91
  107.     numericUpDown1.Minimum  = 1
  108.     numericUpDown1.Name     = "numericUpDown1"
  109.     numericUpDown1.Size     = Size(80, 20)
  110.     numericUpDown1.TabIndex = 0
  111.     numericUpDown1.Value    = 1
  112. --
  113.     startAsyncButton.Location = Point(16, 72)
  114.     startAsyncButton.Name     = "startAsyncButton"
  115.     startAsyncButton.Size     = Size(120, 23)
  116.     startAsyncButton.TabIndex = 1
  117.     startAsyncButton.Text     = "Start Async"
  118.    
  119.     startAsyncButton.Click:Add(startAsyncButton_Click)
  120. --
  121.     cancelAsyncButton.Enabled  = false
  122.     cancelAsyncButton.Location = Point(153, 72)
  123.     cancelAsyncButton.Name     = "cancelAsyncButton"
  124.     cancelAsyncButton.Size     = Size(119, 23)
  125.     cancelAsyncButton.TabIndex = 2
  126.     cancelAsyncButton.Text     = "Cancel Async"
  127.    
  128.     cancelAsyncButton.Click:Add(cancelAsyncButton_Click)
  129. --
  130.     resultLabel.BorderStyle = BorderStyle.Fixed3D
  131.     resultLabel.Location    = Point(112, 16)
  132.     resultLabel.Name        = "resultLabel"
  133.     resultLabel.Size        = Size(160, 23)
  134.     resultLabel.TabIndex    = 3
  135.     resultLabel.Text        = "(no result)"
  136.     resultLabel.TextAlign   = ContentAlignment.MiddleCenter
  137. --
  138.     progressBar1.Location = Point(18, 48)
  139.     progressBar1.Name     = "progressBar1"
  140.     progressBar1.Size     = Size(256, 8)
  141.     progressBar1.Step     = 2
  142.     progressBar1.TabIndex = 4
  143. --
  144.     backgroundWorker1.WorkerReportsProgress      = true
  145.     backgroundWorker1.WorkerSupportsCancellation = true
  146. --
  147.     form.ClientSize = Size(292, 118)
  148.  
  149.     form.Controls:Add(progressBar1)
  150.     form.Controls:Add(resultLabel)
  151.     form.Controls:Add(cancelAsyncButton)
  152.     form.Controls:Add(startAsyncButton)
  153.     form.Controls:Add(numericUpDown1)
  154.  
  155.     form.Name    = "FibonacciForm";
  156.     form.Text    = "Fibonacci Calculator";
  157.     form.TopMost = true;
  158.    
  159.     numericUpDown1:EndInit()
  160.    
  161.     form:ResumeLayout(false)
  162. --
  163. end
  164. --
  165. function Init()
  166. --
  167.     InitializeComponent()
  168.    
  169.     InitializeBackgroundWorker()
  170. --
  171. end
  172. --
  173. function run()
  174. --
  175.     function program()
  176.    
  177.         Application.EnableVisualStyles()
  178.        
  179.         form:Show()
  180.        
  181.         Application.Run()
  182.        
  183.     end
  184.    
  185.     Init()
  186.    
  187.     t = Thread(ThreadStart(program()))
  188.    
  189.     t:SetApartmentState(ApartmentState.STA)
  190.    
  191.     t:Start()
  192. --
  193. end
  194. --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement