Better than Timer t...
 
Notifications
Clear all

Better than Timer tutorial!

Page 2 / 4

1UP
Posts: 25
 1UP
(@1UP)
Eminent RivalGamer
Joined: 9 years ago

The only problem with this is that you are comparing 2 different controls that do not behave in the same way. A background worker acts as a separate thread where as a timer does not. You will end up with cross threading issues if they are not handled properly.

Reply
Cain532
Posts: 1280
Topic starter
(@cain532)
Noble RivalGamer
Joined: 9 years ago

The only problem with this is that you are comparing 2 different controls that do not behave in the same way. A background worker acts as a separate thread where as a timer does not. You will end up with cross threading issues if they are not handled properly.

What kind of cross threading issues?

Reply
1UP
Posts: 25
 1UP
(@1UP)
Eminent RivalGamer
Joined: 9 years ago

What kind of cross threading issues?

A thread is considered a new process. You can't directly pass data between the two.
Let's say you are using a progress bar to update a status and you don't want it to cause the main thread to hang. You put it in a new thread but you wouldn't be able to access it. At least not in the way you proposed it.

But as I said before both the timer and a thread are completely different in their uses.

Also timers don't really cause lag, It depends on what you set the tick count to be and what you are actually doing in the timer. Threading can cause just as many issues especially if you don't clean up after yourself. However the same can be said for anything.

	
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Threading;
  10. namespace CSharp_multithreading
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18. int Max = 60000;
  19. int i = 0;
  20. private void Form1_Load(object sender, EventArgs e)
  21. {
  22. progressBar1.Maximum = Max;
  23. Thread threadFill = new Thread(Thread_FillProgressBar);
  24. threadFill.Start();
  25. }
  26. private void Thread_FillProgressBar()
  27. {
  28. for (i = 0; i <= Max; i++)
  29. {
  30. accessControl();
  31. }
  32. }
  33. private void accessControl()
  34. {
  35. if (InvokeRequired)
  36. {
  37. Invoke(new MethodInvoker(accessControl));
  38. }
  39. else
  40. {
  41. progressBar1.Value = i;
  42. ShowInTaskbar = true;
  43. }
  44. }
  45. }
  46. }
Reply
Cain532
Posts: 1280
Topic starter
(@cain532)
Noble RivalGamer
Joined: 9 years ago

A thread is considered a new process. You can't directly pass data between the two.
Let's say you are using a progress bar to update a status and you don't want it to cause the main thread to hang. You put it in a new thread but you wouldn't be able to access it. At least not in the way you proposed it.

But as I said before both the timer and a thread are completely different in their uses.

Also timers don't really cause lag, It depends on what you set the tick count to be and what you are actually doing in the timer. Threading can cause just as many issues especially if you don't clean up after yourself. However the same can be said for anything.

	
    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; namespace CSharp_multithreading { public partial class Form1 : Form { public Form1() { InitializeComponent(); } int Max = 60000; int i = 0; private void Form1_Load(object sender, EventArgs e) { progressBar1.Maximum = Max; Thread threadFill = new Thread(Thread_FillProgressBar); threadFill.Start(); } private void Thread_FillProgressBar() { for (i = 0; i <= Max; i++) { accessControl(); } } private void accessControl() { if (InvokeRequired) { Invoke(new MethodInvoker(accessControl)); } else { progressBar1.Value = i; ShowInTaskbar = true; } } } }

Wow, very good stuff here! I'll have to keep tabs on you in case I have questions on C# 😛 Thanks for the lesson man! I'll take this to heart

Reply
Shenmue
Posts: 201
(@Shenmue)
Estimable RivalGamer
Joined: 9 years ago

Ill understand this a bit later on hahahha im learning C# now 😀

Reply
Page 2 / 4