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.
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?
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.
- 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;
- }
- }
- }
- }
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
Ill understand this a bit later on hahahha im learning C# now 😀