This question already has answers here:
Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on
(22 answers)
Closed 3 years ago.
Apologies as I know cross-thread operations have been addressed elsewhere, but all I see are fragments and can't quite figure out where to apply the solutions correctly. So I have distilled this problem to it's most basic essence in the hope that me and anybody else who comes across this can see a complete solution without a lot of support code to wade through or fragments with limited context.
I have looked at various posts such as here, here and here but can't quite place the solutions in context.
I have created a Windows Forms project with a single button and textbox. In the form.cs file here is the complete code to demonstrate the problem:
using System;
using System.Windows.Forms;
using System.Threading;
namespace SampleEventTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
new Thread(() =>
{
Thread.CurrentThread.IsBackground = true;
TestClass();
}).Start();
}
private void TestClass()
{
TestEvent tv = new TestEvent();
tv.OnClsConnect += new TestEvent.clsConnect(OnConnected);
tv.DoSomethingThread();
}
public void OnConnected(string str)
{
textBox1.Text = str;
}
}
public class TestEvent
{
public delegate void clsConnect(string str);
public event clsConnect OnClsConnect;
public void DoSomethingThread()
{
if (OnClsConnect != null)
{
OnClsConnect("Thread run");
}
}
}
}
Click the button and you will get the "Cross thread operation not valid" error. How does one properly fix this code? TIA
Basically you can use the BackgroundWorker Control. here is a quick link. also here is an anther link which helps you.
also please read this:
How to: Make thread-safe calls to Windows Forms controls
H/T to S.A. Parkhid for this link.
Here is the solution: modify the method OnConnected to the following:
public void OnConnected(string str)
{
if (textBox1.InvokeRequired)
{
var d = new TestEvent.clsConnect(OnConnected);
textBox1.Invoke(d, new object[] { str });
}
else
{
textBox1.Text = str;
}
}
Related
I had a winform using a method on another project thought a DLL, test, count and returns 2 values (good files and bad files) and show up on the winforms those 2 results once done.
Ive been asked to improve that winform to show up results in real time, since the work and the test can take up to 30mins, but ive been struggling since i'm beginning in async programmation.
Ive tried to call function with out or ref, without success. As far i tried, i can refresh in real time a local variable, but not one running in the method out of the winform project.
Winform :
public static int goodfiles { get; set; }
public static int badfiles { get; set; }
Task workControl;
Task refreshControl;
private async void Winform_Load(object sender, EventArgs e)
{
myprogressBar.Style = ProgressBarStyle.Marquee;
workControl = Task.Run(() => WorkMethod());
refreshControl = Task.Run(() => RefreshMethod());
await executerControl.ConfigureAwait(true);
}
private void RefreshMethod()
{
while (!workControl.IsCompleted)
{
label1.Invoke(new MethodInvoker(delegate
{
label1.Text = goodfiles.ToString();
label2.Text = badfiles.ToString();
}
}
}
private void WorkMethod()
{
goodfiles = 0;
badfiles = 0;
var Work = new WorkClass();
Work.ControlFiles(goodfiles, badfiles);
}
Class library project
public class WorkClass
{
public void ControlFiles(int goodfiles, int badfiles)
{
//Do stuff
var Test = new TestClass();
Test.TestFiles(goodfiles, badfiles);
}
}
public class TestClass
{
public void TestFiles(int goodfiles, int badfiles)
{
//Test files
if(stuff) goodfiles++;
else badfiles++;
}
}
I know it's maybe far from being the prefect architecture, but I have to deal with it.
Is it technically possible, difficult or just impossible to do? Or am I missing something obvious ?
You need to use the same fields from the worker thread and UI thread. The best way is to put them in a shared object. This might be the work-class, but you could also create a separate object that is given as a parameter to the actual work-method. I recommend against using any mutable static fields.
public class WorkClass
{
public volatile int GoodFiles;
public volatile int BadFiles;
public void ControlFiles()
{
//Test files
if (stuff) GoodFiles++;
else BadFiles++;
}
}
and call it like
WorkClass myWork;
private async void Winform_Load(object sender, EventArgs e)
{
myWork = new WorkClass();
workControl = Task.Run(() => myWork.ControlFiles());
}
To check the progress I would recommend a timer. Set it to run however often you want, and update the labels from the myWork-object when event handler for the Tick-event. You can await the workControl-task and stop the timer when the task is done.
It depends on how coupled or uncoupled you want your code to be.
In most cases, the Progress class is a good choice.
Here's an article from Stephen Cleary on the subject: Reporting Progress from Async Tasks
So, my overall goal with this code is to set the text property of labels, from a different thread (in a safe manner).
namespace csDinger3
{
public delegate void setlblStarted_txt(string text);
public partial class ClientUI : Form
{
public void setlblStarted_txt(string text)
{
var setlblStarted a = new setlblStarted(setlblStarted_txt);
if (this.lblStarted.InvokeRequired)
{
this.Invoke(a, new object[] { text });
}
else
{
this.lblStarted.Text = text;
}
}
}
}
Calling code:
namespace csDinger3
{
public class Program
{
// Some code that's not relevant
public static void updateText(Int32 number)
{
setlblStarted x = new setlblStarted(ClientUI.setlblStarted_txt);
x(number.ToString());
}
}
}
From what I can understand (and please correct me if I'm wrong), I need to create a new instance of setlblStarted_txt, point that new instance at method setlblStarted_txt, but the issue is currently ClientUI.setlblStarted_txt isn't static, and wants an object reference.
I've tried using ClientUI c = new ClientUI();, but that doesn't work (because it's creating a new instance of the form?)
What am I doing wrong, and if possible, can you help me understand why?
In .Net 4.0, you can use actions:
if (InvokeRequired)
{
Invoke(new Action<string>(updateText), "some text");
}
else
{
updateText("some text");
}
Also, void updateText(string text) does not need to be static.
As I understand, you are trying to use MethodInvoker delegate to update your text. I suggest you to change this approach to simplify your code:
namespace csDinger3
{
public class Program
{
static ClientUI aForm;
static void Main()
{
aForm = new ClientUI();
aForm.Show();
}
// Some code that's not relevant
public static void updateText(Int32 number)
{
aForm.setlblStarted_txt(number.ToString());
}
public partial class ClientUI : Form
{
public void setlblStarted_txt(string text)
{
if (lblStarted.InvokeRequired)
{
Invoke(new EventHandler(delegate
{
lblStarted.Text = text
}));
}
else
{
lblStarted.Text = text;
}
}
You can achieve the same behaviour with using the ThreadPool or SynchronizationContext or Dispatcher (in WPF). Please see this tutorial for better understanding:
Beginners Guide to Threading in .NET: Part 5 of n
Understanding SynchronizationContext (Part I)
It's All About the SynchronizationContext
I have been searching for an answer to my particular problem for a while with no success.
I have a task in my program that takes a few seconds and I want to show a new form while that task is being done. The new form has a loadingbar and some text.
I need to show the new form parallel to the task otherwise the task will not start untill I close the new form.
This is the solution I have now:
private void loadingBar()
{
frmLoading frm = new frmLoading("Please wait while the database is being backed up", "This might take several days.");
frm.ShowDialog();
}
public void Backup()
{
Thread load = new Thread(new ThreadStart(loadingBar));
load.Start();
///Execute a task.
load.Abort();
}
So, this works OK but my question is: Wouldn't it be better to close the the form "frm" in the load-thread to make it stop?
You could do this a few ways...
1 - You could do as BendEg suggested and invoke you frmClose once you are ready
Something like;
Invoke(new Action(Close));
or
Invoke(new Action(() => frmMain.Close()));
2 - Or you could simply use a background worker;
The simplest way to demonstrate this would be to add a BackgroundWorker to your form, and use the events provided;
public Form1()
{
InitializeComponent();
backgroundWorker1.RunWorkerAsync();
MessageBox.Show(#"Please wait while the database is being backed up", #"This might take several days.");
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
Debug.WriteLine("Running"); //Execute a task
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Debug.WriteLine("Ended"); //Dispose of any objects you'd like (close yor form etc.)
}
I hope this helps.
You can declare the form on Class-Level and later close it with an invoke.
MSDN-Windows Forms Invoke
Like this:
public class Class1
{
private Form myForm;
public Class1()
{
myForm = new Form();
}
public void DoSomeWork()
{
// ===================================================
// Do Some Work...
// ===================================================
myForm.Invoke(new MethodInvoker(this.Hide));
}
public void Hide()
{
myForm.Hide();
}
public void Backup()
{
myForm.ShowDialog();
Thread load = new Thread(new ThreadStart(DoSomeWork));
load.Start();
}
}
I think this can work for you.
void YourMethod()
{
WaitForm wf = new WaitForm();
Invoke(new PleaseWaitDelegate(Launch),wf);
bool val = BoolMethodDoWork();
Invoke(new PleaseWaitDelegate(Close), wf);
if(val)
{
MessageBox.Show("Success!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
return;
}
MessageBox.Show("Damn!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
delegate void PleaseWaitDelegate(Form form);
void Launch(Form form)
{
new System.Threading.Thread(()=> form. ShowDialog()).Start();
}
void Close(Form form)
{
form.Close();
}
I think this will help you (if i understood you right):
Parallel.Invoke(() => somemethod(), () =>
{
someothertaskmethod();
});
I placed methods as example to demonstrate 2 tasks running.
You nee to use the proper using statement using System.Threading.Tasks;
I am using Telerik's radcontrols for winforms.
Here is a program that can reproduce my problem:
public partial class RadForm1 : Telerik.WinControls.UI.RadForm
{
public RadForm1()
{
InitializeComponent();
}
private void radButton1_Click(object sender, EventArgs e)
{
RadMessageBox.SetThemeName("Office2010Black");
RadMessageBox.Show("Hello World");
//MessageBox.Show("hello world");
run();
}
public void run()
{
var thread = new Thread(() => run2());
thread.IsBackground = true;
thread.Start();
}
public void run2()
{
//MessageBox.Show("hello");
RadMessageBox.Show("Hello");
}
}
Whenever try using Telerik's messagebox I am getting a cross thread exception. However if I use the standard winform messagebox instead then it will work absolutely fine.
Maybe i am missing something here.
UPDATE:
for anyone else having the same problem this is the link to offical reply Click here
The winform MessageBox class is specifically designed to be able to be called from a non-UI thread.
The RadMessageBox simply wasn't. It was designed under the assumption that it would be called from the UI thread.
How do I bind a ProgressBar to a property of a class updated in another thread?
The following code example shows my first naive attempt. It doesn't work because I get runtime errors about cross thread communication. I think I need to use Invoke in some way, but I'm not sure how to do it with the Binding class.
using System;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Threading;
class ProgressForm : Form
{
private ProgressBar pbProgress;
public ProgressForm(ref LongOp lo)
{
Binding b = new Binding("Value", lo, "Progress");
pbProgress = new ProgressBar();
pbProgress.DataBindings.Add(b);
this.Controls.Add(pbProgress);
}
}
class Program : Form
{
private Button btnStart;
private LongOp lo;
public Program()
{
lo = new LongOp();
btnStart = new Button();
btnStart.Text = "Start long operation";
btnStart.Click += new EventHandler(btnStart_Click);
this.Controls.Add(btnStart);
}
private void btnStart_Click(object sender, EventArgs e)
{
ProgressForm pf = new ProgressForm(ref lo);
lo.DoLongOp();
pf.ShowDialog();
}
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Program());
}
}
class LongOp : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private int progress;
public void DoLongOp()
{
Thread thread = new Thread(new ThreadStart(this.run));
thread.Start();
}
public void run()
{
for (int i = 0; i < 10; ++i)
{
Thread.Sleep(1000);
Progress++;
}
}
public int Progress
{
get
{
return progress;
}
set
{
progress = value;
NotifyPropertyChanged("Progress");
}
}
private void NotifyPropertyChanged(String field)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(field));
}
}
}
So how do I bind a ProgressBar to a value updated in another thread?
Thanks in advance
EDIT: I've switched to using the ThreadedBinding implementation Mr. Gravell wrote and linked to. I'm still getting the cross thread exception though. Pressing "Break" in the exception dialog highlights the PropertyChanged(this, new PropertyChangedEventArgs(field)); line as the line causing the exception.
What more do I need to change?
EDIT: Looks like Mr. Gravell's post has been removed. The ThreadedBinding implementation I mentioned can be found at the end of this thread: http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/browse_thread/thread/69d671cd57a2c7ab/2f078656d6f1ee1f?pli=1
I've switched back to plain old Binding in the example for easier compilation by others.
Unfortunately I think the cross-threading issues will make data-binding proper a bit too clumsy to use here, and probably more complexity than you need in any case -- the data only needs to be plumbed one way.
You could just replace the binding with an event handler like this:
private void ProgressPropertyChangedHandler(object sender,
PropertyChangedEventArgs args)
{
// fetch property on event handler thread, stash copy in lambda closure
var progress = LongOp.Progress;
// now update the UI
pbProgress.Invoke(new Action(() => pbProgress.Value = progress));
}