I have a backgroundworker which calls a method in the
DoWork event.
this method accesses a dataset in the UI Thread and it also calls a another method in the UI Thread.
my problem comes in when the method requires access to datasets and methods that exists in the Ui Thread, I get a cross thread operation not valid error.
How can I access Items UI Thread?
Is it possible for me to access it using the backgroundworker or must I use another method of running my method in a background thread
Thanks
You just need to marshal the method call to the UI thread.
On WinForms:
void DoWork(...)
{
YourMethod();
}
void YourMethod()
{
if(yourControl.InvokeRequired)
yourControl.Invoke((Action)(() => YourMethod()));
else
{
//Access controls
}
}
you should use the Dispatcher.Invoke method
for more info have a look at the below link
http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.invoke.aspx
A control created in the UI thread cannot be accessed in another thread in normal fashion. Please create a delegate and invoke the delegate using control.Invoke.
The method sample provided below can be used to enable visibility on a button regardless of the thread context you are in.
private void EnableButtonVisibility( Button btn, bool enable)
{
if ( !btn.InvokeRequired )
{
btn.Visible = enable;
}
else
{
btn.Invoke( new EnableButtonVisibilityHandler( EnableButtonVisibility ), btn, enable );
}
}
delegate void EnableButtonVisibilityHandler( Button btn, bool enable);
You can also achieve the same using Action<Button, bool>
Related
I have code that runs in a different thread than the UI's one, and it has to create a control (windows forms). However, I don't have a reference to any control from the UI (that way, I could use myControl.Invoke( methodThatAddsControlToUI ) ). Is there a way to do it in the .net compact framework?
I would be interested in a solution that doesn't use references to other controls, if possible (tracking all created forms, for example, would not be a good workaround, as my code will be in a library). In the full framework version, there is the Application.OpenForms property, but this doesn't exit in the CF.
EDIT:
The main purpose of this is calling a method on the UI thread:
class Worker
{
public MyMethod()
{
// I need to call a method on the UI (this code doesn't run in the UI thread),
// but I don't have any field in this object holding an UI control
// value. So, I can't write myControlField.Invoke(...),
// but I still need to call a method on the UI thread
}
}
Any suggestions?
From a library there's really no way to guarantee your thread context, so your safest bet is to have the consume provide the invoker and leave it to them to ensure it was created in the proper context. Something like this pattern:
class Foo
{
private Control m_invoker;
public Foo()
: this(null)
{
}
public Foo(Control invoker)
{
if (invoker == null)
{
// assume we are created on the UI thread,
// if not bad things will ensue
m_invoker = new Control();
}
else
{
m_invoker = invoker;
}
}
public void Bar()
{
m_invoker.Invoke(new Action(delegate
{
// do my UI-context stuff here
}));
}
}
I'm sorry if this isn't a real answer, but I think it may help:
The reason why WinForms has this approach -- using a Control or Form reference to access a Invoke method that enables you to run code on the UI Thread -- is that the only reason you should have to run a code in the UI Thread is if you are going to write/change the state of UI components.
Of course, if you are going to do that, you must have a reference to a UI component. So you'd have access to its Invoke method. I cannot think of any other reason you'd have to access the UI thread from a component other than to modify a visual element.
It must be invoke ... But invoke have to wait still main thread i mean you not get error this way but this is not exacly working parallel if you want to go more than one process at same time just create more then one thread
Thread thread = new Thread(new delegate_method(method));
thread.start ();
Thread thread2 = new Thread(new delegate_method(method2));
thread.start ();
handle two process same time
void method ()
{
//do something here -- working background Remember can not control any UI control from here
finish_thread()
}
void method2 ()
{
//do something here -- working background Remember can not control any UI control from here
finish_thread()
}
void finish_thread()
{
if(invoke.Required)
{
//Here you have to call delegate method here with UI
BeginInvoke(new delegate_method(finish_thread));
}
else
{
//Now you can control UI thread from here and also you finished background work
//Do something working with UI thread
textBox.Text = "";
}
}
//Declare this in class
public delegate void delege();
//Write this lines when you want to background thread start
Thread thread = new Thread(new ThreadStart(() => {
//Do what you what with backgorund threading , can not use any interface comand here
BeginInvoke(new delege(() => {
//Do here any main thread thread job,this can do interface and control jobs without any error
}));
}));
thread.Start();
Let's say I have the following code:
public void Inject(Form subform)
{
this.tabControl1.TabPages[1].Controls.Add(subform);
this.Refresh();
}
How can I convert the Controls.Add() call to a thread-safe call, using Control.Invoke?
The only way to make Control.Add thread safe is to make sure it's called from the UI thread. This also implies that the Control being added is usable from the UI thread.
Here is a function though which produces a delegate which can add to a Control from any thread (presuming the added Control is OK on the UI thread).
public Action<Control> GetAddControl(this Control c)
{
var context = SynchronizationContext.Current;
return (control) =>
{
context.Send(_ => c.Controls.Add(control), null);
};
}
Then for a given Control you can pass the resulting delegate to any thread.
// From UI thread
Action<Control> addControl = c.GetAddControl();
// From background thread
addControl(subForm);
For what reasons would this.InvokeRequired equal False within InitUIState(), as this new thread is being created via a delegate?
My problem is that my label is never being set and this.BeginInvoke() is never executing, I imagine it's due to the fact InvokeRequired = False.
private delegate void BackgroundOperationDelegate(ViewMode mode);
private BackgroundOperationDelegate backgroundOperationDelegate;
private void FormControlPanel_Load(object sender, EventArgs e)
{
Init();
}
private void Init() {
this.backgroundOperationDelegate = this.InitUIState;
this.backgroundOperationDelegate.BeginInvoke(mode, null, null);
}
private void InitUIState(ViewMode mode)
{
// .. other business logic only here relevant
// to the worker process ..
this.BeginInvoke((MethodInvoker)delegate
{
this.labelProgramStatus.Text = CONSOLE_IDLE_STATUS;
});
}
I use this pattern time and time again, but for some reason, this time it's not executing :P
(and yes there is only one instance of InitUIState() ever being called, that being from the delegate)
Thanks guys.
Images verifying two distinct threads:
http://imgur.com/mq12Wl&X5R7G
http://imgur.com/mq12W&X5R7Gl
Follow up question: Is this an unpreferred way of creating threads? I've just always found it so simple and lightweight. Perhaps I should be using thread.Start() and I will avoid these issues?
Your 2nd BeginInvoke will throw an Exception.
Try
private void InitUIState(ViewMode mode)
{
if (this.InvokeRequired)
{
this.BeginInvoke((MethodInvoker)delegate
{
InitUIState(mode);
});
}
else
{
this.labelProgramStatus.Text = CONSOLE_IDLE_STATUS;
}
}
You are mixing BeginInvoke of Form and Delegate, as both of them have same method name.
Form's method, BeginInvoke calls the method you are requested in the same UI thread, but on a later stage, after processing its own pending UI operations. This is the reason, InvokeRequired will always be false within the Form's BeginInvoke's method.
Delegate's method, BeginInvoke calls the method on a new thread asynchronously in thread pool. And InvokeRequired in delegate's BeginInvoke will always be true.
Invoke and BeginInvoke on delegates are not the same as ISynchronizeInvoke.
Also you need to call EndInvoke when dealing with a delegate.
I hava a Background Worker and a DataGrid in my c# Application. In do work of my Backgroundworker which will call an Api in my dlls which will enter some Data in a SQLite Database. After the Completion of my Api call I report a progress and In progress event of my Backgroundworker I get the contents from Db and assign it as a DataSource to my grid. I call same API in same backgroundworker. In the middle of processing my application crashes. But If I dont assign the dataSource in ProgressChanged my application doesnt crashes.
I am assuming you must be accessing UI object using Invoke method.
If not try using following approach (Executes the specified delegate, on the thread that owns the control's underlying window handle, with the specified list of arguments.):
//In Form.Designer.cs
Label myLabel = new Label();
//In code behind under Background worker method
LabelVlaueSetter SetLabelTextDel = SetLabelText;
if (myLabel .InvokeRequired)
{
myLabel.Invoke(SetLabelTextDel, "Some Value");
}
private delegate void LabelVlaueSetter(string value);
//Set method invoked by background thread
private void SetLabelText(string value)
{
myLabel.Text = value;
}
As Johnathan Allen mentions, it should not matter. Unless something else is going on. I have had two cases where I could not interact with certain controls in the events generated by the BackgroundWorker. The only thing that worked was using the Invoke method.
Try assigning the DataSource on the same thread that created the DataGridView control. You do this through the control's Invoke method. Use the following code. (I have not tested, but this is the standard pattern.)
If this does not work then try Jonathan Allen's suggestion.
Actually, do whichever suggestion is easiest to try first.
private delegate void SetDataSourceDelegate(object value);
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) {
DataTable oData = null; //'assign data source
if (dataGridView1.InvokeRequired) {
dataGridView1.Invoke(new SetDataSourceDelegate(SetDataSource), new Object[] {oData});
}else{
SetDataSource(oData);
}
}
private void SetDataSource(object value) {
dataGridView1.DataSource = value;
}
It shouldn't matter, but why are you using ProgressChanged instead of RunWorkerCompleted?
Also, try doing everything on the GUI thread without the BackgroundWorker. That will let you know if the problem is in your code or how your code interacts with the GUI.
I have a UserControl with a tree on it. It uses multithreading to add nodes to it. I have a function called Expand which I need to execute after filtering completed and since I'm a newbie with multithreading I'm not sure how to do that. Here's my code:
class MyClass : UserControl
{
private Thread nThread;
private bool searchLoadCompleted = false;
private void Filter()
{
ClearTree();
this.nThread = new Thread(new ParameterizedThreadStart(AddFilteredResultsToTree));
this.nThread.IsBackground = true;
this.nThread.Start(someParameter);
}
private void AddFilteredResultsToTree(int someParameter)
{
myTree.Invoke(new MethodInvoker( ()=> this.searchLoadCompleted = false ));
myTree.Invoke(new MethodInvoker( ()=> AppendNode(......) ));
myTree.Invoke(new MethodInvoker( ()=> this.searchLoadCompleted = true ));
}
private void Expand()
{
}
}
I tried to add nThread.Join() into Expand() but it got stuck indefinitely. What should I do?
If the singlethreaded version of this is:
ClearTree();
AddFilteredResultsToTree(someparameter);
Expand();
Don't bother going multithreading, just do it on the same thread. The point of using multithreading there would be to let the main thread handle UI events, if you join the thread then you're basically just launching a background thread while freezing (not doing any work) in the main thread. Note that by calling Invoke you're actually delegating the execution of AddFilteredResultsToTree to the main thread anyway.
I'd suggest you simply call Expand from AddFilteredResult and use the Dispatcher to update the UI if needed or.
Another way to go (best in my opinion) would be to use the Async Pattern for this (example and tutorial here), and then on the AsyncCallback update the UI.
Calling Invoke will block both the GUI thread and your worker thread so there won't be any performance improvement over code without a worker thread.