Running own class on separate thread - c#

I want to run an own class in another thread, but if I do that I can't use my, for example, labels inside of an EventHandler, how can I avoid that?
That's how my code looks:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Ts3_Movearound
{
public partial class Form1 : Form
{
TS3_Connector conn = new TS3_Connector();
Thread workerThread = null;
public Form1()
{
InitializeComponent();
conn.runningHandle += new EventHandler(started);
conn.stoppedHandle += new EventHandler(stopped);
}
private void button1_Click(object sender, EventArgs e)
{
//System.Threading.Thread connw = new System.Threading.Thread(conn);
workerThread = new Thread(conn.Main);
workerThread.Start();
}
public void started(Object sender, EventArgs e)
{
label1.Text = "Status: Running!";
}
public void stopped(Object sender, EventArgs e)
{
label1.Text = "Status: Stopped!";
}
}
}
And that's the error:
InvalidOperationExpetion in Line "label1.Text = "Status: Running!";"

You can only update the control via the UI thread. Use label1.Invoke() to do that:
label1.Invoke((MethodInvoker)delegate {
label1.Text = "Status: Running!";"
});

I would look at using a BackgroundWorker for this. You then use the following:
1) Before you call RunWorkerAsync you set the label to running as there are no thread issues.
2) After calling RunWorkerAsync if you set any control use:
label1.Invoke(new Action(() => label1.Text = #"Status: Running!"));
3) Once the process has finished you can then set the label to stopped by assigning a method to the RunWorkerCompleted event. There should be no thread issues in this method as it runs on the main thread.

SO has a lot of data about it. I've found some for you:
Can you access UI elements from another thread? (get not set)
How do I access GUI-Elements in another thread?
How to directly access the UI thread from the BackgroundWorker thread in WPF?

Related

How to show gif while loading user control?

Hello I have a button and when i click it it loads user control and it tooks a little time. I want to show gif while it is loading. How can i do it?
Edit: I want add usercontrol to panel on background worker.
panel3.Controls.Add(hesaplar.Instance);
hesaplar.Instance.Dock = DockStyle.Fill;
hesaplar.Instance.BringToFront();
If you want to show the user a .gif animation while loading you can use Multithreading for parallel executing and Action Delegate to invoke safe GUI updates. please see my comments inside the code example:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication21
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// show gif on a different thread until LongTask finish executing
System.Threading.Thread waitThread = new System.Threading.Thread(ShowGif);
waitThread.Start();
// start a new thread to execute LongTask() parallel to the waitThread
System.Threading.Thread longTaskThread = new System.Threading.Thread(LongTask);
longTaskThread.Start();
}
// put the code of your task that you want to execute in the background in that method
private void LongTask()
{
// long load...
for (int i = 0; i < 10; i++)
{
System.Threading.Thread.Sleep(1000);
}
// the "long task" inside the method finished to execute - dispose safely the picturebox from the GUI
panel3.Invoke(new Action(pictureBox1.Dispose));
// add user control to panel3
panel3.Invoke(new Action(AddUserControl));
// promt to screen
label1.Invoke(new Action(NotifyTaskHasFinished));
}
private void AddUserControl()
{
UserControl1 uc = new UserControl1();
panel3.Controls.Add(uc);
}
private void ShowGif()
{
// use action delegate to update GUI changes from another thread
panel3.Invoke(new Action(AddGif));
}
private void AddGif()
{
// show .gif animation inside picturebox1
pictureBox1.Image = WindowsFormsApplication21.Properties.Resources.raw_demo;
}
private void NotifyTaskHasFinished()
{
label1.Text = "LongTask() method has finished and user control was loaded";
}
}
}

Cross-thread operation not valid for invalidating graphics object

I'm trying to create graphics that will redraw every 500ms using a Timer, but I keep running into a cross-thread operation. Can somebody please tell me why this is happening?
Error:
Cross-thread operation not valid: Control 'GraphicsBox' accessed from a thread other than the thread it was created on.
I'm using WinForms, and have a PictureBox called "GraphicsBox" in the main form:
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.Diagnostics;
using System.Threading;
namespace NamespaceName
{
public partial class FormName : Form
{
Graphics g;
public FormName()
{
InitializeComponent();
System.Timers.Timer t = new System.Timers.Timer();
t.Interval = 500;
t.Enabled = true;
t.Elapsed += (s, e) => this.GraphicsBox.Invalidate(true);
}
private void FormName_Load(object sender, EventArgs e)
{
this.GraphicsBox.Paint += new PaintEventHandler(OnPaint);
}
protected void OnPaint(object sender, PaintEventArgs e)
{
g = e.Graphics;
//Draw things
}
}
}
Is there any way I can fire the OnPaint event I have from the timer's 'tick' (or 'elapsed')? I believe that will do the trick. All I'm trying to do is redraw the graphics object, and I will change things in the code to cause it to be drawn differently.
The main problem here is that there are at least 3 classes named Timer and probably more (in different namespaces, but with different behaviours). You are using one that calls back on a worker thread, and UI controls don't like that due to thread affinity.
If you switch to System.Windows.Forms.Timer it will invoke the callback on the UI thread (presumably via sync-context, but I guess it might be implemented using the message loop directly). This is then not a cross-thread operation, and will work fine.
You're calling the GraphicsBox object on the wrong thread, System.Timers.Timer.Elapsed is called on a different (Background) thread.
You can either -
a) Switch to using System.Windows.Forms.Timer, which will run on the same thread as GraphicsBox
or
b) Quick and nasty -
t.Elapsed += (s, e) => this.Invoke(new MethodInvoker(delegate(){ this.GraphicsBox.Invalidate(true); }));

Cross Thread Operation - Invoked function calls another function C#

I have a program which uses many threads, when one of the threads found an answer (I think the context doesn't really matter) - it announces it, and then the first thread I created calles a function in a user control class using Invoke.
I checked - and if I change any attributes in this function, I do not get the cross-thread operation. But this function starts a timer (System.Timers.Timer) -> so the function of the "Elapsed" event is called. Inside it I am trying to change an attribute, and that causes cross-thread operation. What am I doing wrong? Isn't it possible to have the invoked function calling another function and then change the attributes in there?
By the way, is it wrong to invoke functions using the delegate? I mean, having the delegate as an attribute of the class I need it in, and then using delegAttributeName.Invoke(parameters) - and not this.Invoke(new Delegate(), parameters);
Heres part of the code:
Thats is where I invoke the function:
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 Nim_Assignment_3
{
public delegate void drawDeleg(Color c, int amount, int rowNumber);
public partial class Nim : Form
{
private event drawDeleg myDrawDeleg;
private void CheckXor()
{
if (this.foundToPaint)
{
this.myDrawDeleg.Invoke(this.currentTurnColor, this.amountToPaint, this.rowToPaint);
this.drawWait.WaitOne();
this.foundToPaint = false;
if (this.currentTurnColor == Color.Blue)
this.currentTurnColor = Color.Red;
else
this.currentTurnColor = Color.Blue;
}
}
// the invoked function:
private void callFillPencils(Color c, int amount, int rowNumber)
{
this.rows[rowNumber].fillPencils(c, amount);
}
}
}
And this is the function that the invoked function is calling - and the one it calls (the timer-elapsed event function):
(fillPencils - the function that the invoked function in the Form class (Nim) is calling):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Timers;
namespace Nim_Assignment_3
{
public partial class PencilsUC : UserControl
{
private PictureBox[] pencils;
public static Image grayPencil = new Bitmap("GrayPen.bmp"), bluePencil = new Bitmap("BluePen.bmp"), redPencil = new Bitmap("RedPen.bmp");
private int amountOfPencils, amountOfPencilsLeft, currIndex, currAmount;
private System.Timers.Timer timer;
private Color currColor;
public event FinishedDrawing drawFinishedDeleg;
public PencilsUC()
{
// intializing things in the constructor...
this.timer = new System.Timers.Timer();
this.timer.Interval = 100;
this.timer.Elapsed += new ElapsedEventHandler(timer_Tick);
}
public void timer_Tick(object sender, EventArgs e)
{
// THE THING THAT MAKES THE CROSS THREAD-OPERATION: THE LINE INSIDE THE "if"
if (this.currColor == Color.Blue)
pencils[currIndex--].Image = bluePencil;
else
pencils[currIndex--].Image = redPencil;
this.currAmount--;
if (this.currAmount == 0)
{
this.timer.Stop();
if (this.drawFinishedDeleg != null)
this.drawFinishedDeleg.Invoke(this, new EventArgs());
}
}
public void fillPencils(Color c, int amount)
{
MessageBox.Show("Hello");
this.currColor = c;
this.currAmount = amount;
this.timer.Start();
}
}
}
(THE CROSS THREAD OPERATION HAPPENS INSIDE THE TIMER_TICK FUNCTION)
I used the windows forms timer at first but for some reason it didn't get to the tick-event function (timer.Start() was called but I put a message box in the tick function and it didnt get in there so I changed it - I saw some answers that said it was better)
I would love some help, I am sorry for the long post, I just wanted to be as clear as I can...
Thanks a lot in advance! :)
Use a Windows.Forms.Timer instead of a System.Timers.Timer. (You'll need to change the names of a few properties/events, i.e. Tick instead of Elapsed, but it's straightforward enough.)
The Timer in the Form's namespace marshals the Tick event into the UI thread, unlike the systems timer which executes the event in a thread pool thread.
If you really prefer to use the system's timer, then you can set the SynchronizingObject to have it marshall it's event to the UI thread:
timer.SynchronizingObject = this;
Note that the UserControl is a synchronizable object.
You need to .Invoke onto the main thread to change any controls.
Image image;
if (this.currColor == Color.Blue)
image = bluePencil;
else
image = redPencil;
this.Invoke(new MethodInvoker(() => pencils[currIndex--].Image = image));
The => is the syntax for a lambda (called anonymous method in other languages). Think about it as a one-line function.
() => pencils[currIndex--].Image = image
is the same as:
void SetImage(Image image, ref int currIndex) {
pencils[currIndex--].Image = image;
}
MethodInvoker provides a simple delegate that is used to invoke a method with a void parameter list
The easiest fix as you already made code would be to set SynchronizingObject of the Timer to Form, so timer would run on UI thread.

How do I access variables from a different thread?

Getting error: Cross-thread operation not valid: Control 'label1' accessed from a thread other than the thread it was created on.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
string CONNECTING = "Connecting to server...";
string GETTING_DATA = "Getting data...";
string CONNECT_FAIL = "Failed to connect!";
string CONNECT_SUCCESS = "Connection established!";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Thread t1 = new Thread(run);
t1.Start();
}
public void run() {
label1.Text = CONNECTING;
}
}
}
How do I properly fix this? I've tried using CheckForIllegalCrossThreadCalls = false; but that obviously causes errors.
I'd also like to know how I can stop the thread, since it can't be accessed anymore outside of that function.
Thanks in advance!
Try using BeginInvoke:
public void run()
{
label1.BeginInvoke(new Action(() =>
{
label1.Text = CONNECTING;
}));
}
Only the UI thread can update UI elements in a Windows Forms app. Other threads need to use BeginInvoke to update the UI.
ORGINAL: i assumed this was a WPF app and said to use this.Dispatcher, but Dispatcher isn't in Windows Forms apps.
Accessing a control from different threads
In WinForms App you can ony access directly a Control from the thread it was created.
To do such a task you will need to use InvokeRequired property of a control to see if you must use Invoke inorder to force a call of the action from the original thread.
A public method that might be accessed from any thread including the original would look like this:
public void run() {
if (label1.InvokeRequired) //Is this method being called from a different thread
this.Invoke(new MethodInvoker(()=> label1.Text = CONNECTING));
else //it's cool, this is the original thread, procceed
label1.Text = CONNECTING;
}
But if you are absolutly sure that run() method will be called only from the thread, consider not even checking if InvokeRequired and immediatly call Invoke
Further information: http://msdn.microsoft.com/en-us/library/ms171728(v=vs.80).aspx
Stopping a thread in progress
Simples is to use t1.Abort(); method of a Thread. This will throw and exception forcing it to stop where ever it was. this is great for threads that do not do any long processing so stopping it won't cause any problems.
If you do do proccesing in your thread, which means you can't just stop it in the middle then I suggest you to use a boolean that will indicate that the thread must cancel ASAP.
private bool isCancelRequired = false;
public void run() {
while(true) {
//do long processing..
if (isCancelRequired)
break;
}
}
More advanced methods: http://www.yoda.arachsys.com/csharp/threads/shutdown.shtml

Thread error in C# Windows Form

I have gotten this error:
An unhandled exception of type 'System.InvalidOperationException'
occurred in System.Windows.Forms.dll
Additional information: Cross-thread operation not valid: Control
'Redlight' accessed from a thread other than the thread it was created
on.
Redlight and Greenlight are pictureBoxes.
Basically, all I want it to be able to do is alternate between each picture every second.
I searched on this website for similar errors, I see it has to do with "Invoking", but I don't even know what that is, can someone enlighten me?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace EMCTool
{
public partial class EMCTool_MainForm : Form
{
bool offOn = false;
public EMCTool_MainForm()
{
InitializeComponent();
}
private void EMCTool_MainForm_Load(object sender, EventArgs e)
{
System.Threading.Timer timer = new System.Threading.Timer(new System.Threading.TimerCallback(timerCallback), null, 0, 1000);
}
private void timerCallback(object obj)
{
if (offOn == false)
{
Redlight.Show();
offOn = true;
}
else
{
Greenlight.Show();
offOn = false;
}
}
}
}
You get the Cross-thread error when you try to update a UI element from any thread it was not created on.
Controls in Windows Forms are bound to a specific thread and are not thread safe. Therefore, if you are calling a control's method from a different thread, you must use one of the control's invoke methods to marshal the call to the proper thread. This property can be used to determine if you must call an invoke method, which can be useful if you do not know what thread owns a control.
Refer here for more
Try this .This works fine for me
if (pictureBoxname.InvokeRequired)
pictureBoxname.Invoke(new MethodInvoker(delegate
{
//access picturebox here
}));
else
{
//access picturebox here
}
In WinForms projects better use the System.Windows.Forms.Timer as it calls the Tick event on the UI-thread automatically:
private System.Windows.Forms.Timer _timer;
private void EMCTool_MainForm_Load(object sender, EventArgs e)
{
_timer = new System.Windows.Forms.Timer { Interval = 1000 };
_timer.Tick += Timer_Tick;
_timer.Start();
}
private void Timer_Tick(object sender, EventArgs e)
{
if (offOn) {
Greenlight.Show();
} else {
Redlight.Show();
}
offOn = !offOn;
}
Alternative solution would be to use System.Timers.Timer which has SynchronizingObject property so set this, and it will work:
timer.SynchronizingObject = This
Or to use System.Windows.Forms.Timer as it won't raise exception (it raises Tick event on UI thread).

Categories

Resources