Better than Timer t...
 
Notifications
Clear all

Better than Timer tutorial!

Page 3 / 4

Absolute Zero
Posts: 119
(@Absolute Zero)
Estimable 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; } } } }

BackgroundWorker and ProgressBar demo - CodeProject

But that's not entirely true.

Reply
Absolute Zero
Posts: 119
(@Absolute Zero)
Estimable RivalGamer
Joined: 9 years ago

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

BackgroundWorker and ProgressBar demo - CodeProject

Here's some proof that it does in fact work with a progress bar. 😛

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

BackgroundWorker and ProgressBar demo - CodeProject

But that's not entirely true.

No, what I said is true. You can not directly access it. That is considered cross threading, which is why I was saying a timer and a thread are not the same thing. They do not function the same.

Reply
Absolute Zero
Posts: 119
(@Absolute Zero)
Estimable RivalGamer
Joined: 9 years ago

No, what I said is true. You can not directly access it. That is considered cross threading, which is why I was saying a timer and a thread are not the same thing. They do not function the same.

They do not function the same, but you can pass variables between them easily. There is a lot to learn with .NET, we will never know the true answer to anything.

Reply
KiLLerBoy_001
Posts: 25
(@KiLLerBoy_001)
Eminent RivalGamer
Joined: 8 years ago

backgroundworkers huh , i always prefered an extra thread though.

Though required some inventive coding to get the extra tthread report back to the main one but thats how i like my stuff i guess 😛

Reply
Page 3 / 4