WPF invoke a control - c#

How can I invoke a control with parameters? I've googled this up, but nowhere to find!
invoke ui thread
This is the error i get:
Additional information: Parameter count mismatch.
And this happens when i do a simple check whether the text property of a textbox control is empty or not. This works in WinForms:
if (this.textboxlink.Text == string.Empty)
SleepThreadThatIsntNavigating(5000);
It jumps from this if the line to the catch block and shows me that message.
This is how i try to invoke the control:
// the delegate:
private delegate void TBXTextChanger(string text);
private void WriteToTextBox(string text)
{
if (this.textboxlink.Dispatcher.CheckAccess())
{
this.textboxlink.Text = text;
}
else
{
this.textboxlink.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.Normal,
new TBXTextChanger(this.WriteToTextBox));
}
}
What am I doing wrong? And since when do i have to invoke a control when i just want to read its content?

When you call Invoke, you're not specifying your argument (text). When the Dispatcher tries to run your method, it doesn't have a parameter to supply, and you get an exception.
Try:
this.textboxlink.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.Normal,
new TBXTextChanger(this.WriteToTextBox), text);
If you want to read the value from a text box, one option is to use a lambda:
string textBoxValue = string.Empty;
this.textboxlink.Dispatcher.Invoke(DispatcherPriority.Normal,
new Action( () => { textBoxValue = this.textboxlink.Text; } ));
if (textBoxValue == string.Empty)
Thread.Sleep(5000);

Reed is correct, but the reason you need to do this is that GUI elements are not thread safe and so all GUI operations have to be done on the GUI thread to ensure that the content is being read correctly. Its less obvious why this is necessary with a read operation like this but it is very necessary with writes and so the .NET framework just requires all access to the GUI to be done in the GUI thread.

Related

Alternative to InvokeRequired

I searched and got that Dispatcher CheckAccess can be used in place of InvokeRequired in wpf.
This is the code want to convert in wpf
private void ChangeTextBox(string txt)
{
if (msg_log.InvokeRequired)
{
Invoke(new UpdateText(ChangeTextBox), new object[] { txt });
}
else
{
msg_log.Text += txt + "\r\n";
}
}
I tried out this ---->
private void ChangeTextBox(string txt)
{
if (msg_log.Dispatcher.CheckAccess())
{
Dispatcher.Invoke(new UpdateText(ChangeTextBox), new object[] { txt });
}
else
{
msg_log.Text += txt + "\r\n";
}
}
But while running i am getting Error [InvalidOperationException] "The calling thread cannot access this object because a different thread owns it."
What i am doing wrong ? Please Help ?
Your problem is not because of the CheckAccess method... it is fine to use that to check whether the call to Invoke is required or not. When you call the Dispatcher, it is important to ensure that you are calling the correct instance of the Dispatcher class. From the Dispatcher Class page on MSDN:
In WPF, a DispatcherObject can only be accessed by the Dispatcher it is associated with. For example, a background thread cannot update the contents of a Button that is associated with the Dispatcher on the UI thread. In order for the background thread to access the Content property of the Button, the background thread must delegate the work to the Dispatcher associated with the UI thread. This is accomplished by using either Invoke or BeginInvoke. Invoke is synchronous and BeginInvoke is asynchronous.
So in your case, if you can access the correct Dispatcher instance using the following:
msg_log.Dispatcher.CheckAccess()
Then as #SriramSakthivel mentioned in the comments, you should access the same instance when calling Invoke:
msg_log.Dispatcher.Invoke(new UpdateText(ChangeTextBox), new object[] { txt });
OP problem solved but just for record a useful helper for dispatcher check is:
public void DispatchIfNecessary(Action action) {
if (!Dispatcher.CheckAccess())
Dispatcher.Invoke(action);
else
action.Invoke();
}
which can then be called as:
DispatchIfNecessary(() => { myUIcontrol.Update(...); });

C# thread safe text update on dynamically created text boxes

I know how to do a thread safe update on a text box that was already defined http://msdn.microsoft.com/en-us/library/ms171728.aspx .... how can i do this on text boxes that were generated later on in the program? You advice is much appreciated.
Given some TextBox object, just invoke on it:
TextBox foo = new TextBox(...);
// Code to add the new box to the form has been omitted; presumably
// you do this already.
Action update = delegate { foo.Text = "Changed!"; };
if (foo.InvokeRequired) {
foo.Invoke(update);
} else {
update();
}
If you're using this pattern a lot, this extension method might be helpful:
public static void AutoInvoke(
this System.ComponentModel.ISynchronizeInvoke self,
Action action)
{
if (self == null) throw new ArgumentNullException("self");
if (action == null) throw new ArgumentNullException("action");
if (self.InvokeRequired) {
self.Invoke(action);
} else {
action();
}
}
Then you can reduce your code to:
foo.AutoInvoke(() => foo.Text = "Changed!");
This will just do the right thing, executing the delegate on the main GUI thread whether or not you are currently executing on it.
We definitely need more information here, but from what I can gather, you're lamenting the fact that the thread's main function doesn't take any arguments. You could make the textbox(es) members of the surrounding class, and access them that way. If you go this route though, be sure to use a mutex or some other locking device for threads.

Help with understanding C# syntax while Invoking a new Action

I am new to c# and do not understand the syntax of invoking a new action or even what an action is. From my understanding in Port1_DataReceived, I have to create an action because I am in a new tread... Can anyone elaborate on why I need to do this?
public Form1()
{
InitializeComponent();
SerialPort Port1 = new SerialPort("COM11", 57600, Parity.None, 8, StopBits.One);
Port1.DataReceived += new SerialDataReceivedEventHandler(Port1_DataReceived);
Port1.Open();
}
private void Port1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
SerialPort Port = (SerialPort)sender;
string Line = "";
int BytestoRead = Port.BytesToRead;
Line = Port.ReadLine();
label1.Invoke(new Action(() =>
{
label1.Text = Line;
}));
}
The code snip that I am really having trouble understanding is:
label1.Invoke(new Action(() =>
{
label1.Text = Line;
}));
Can someone break down what this is doing.. I am sure it is nothing to complicated, just that I have never seen anything like it before. The syntax that is really holding me up is ()=> the new action is pointing to the code below or something??
This uses something known as a "lambda expression" to create an anonymous delegate that matches the signature expected by the Action constructor.
You could achieve the same effect like this:
label1.Invoke(SetText);
...
public void SetText() { label1.Text = Line; }
or like this:
label1.Invoke(new Action(SetText));
...
public void SetText() { label1.Text = Line; }
or like this:
label1.Invoke(new Action(delegate() { label1.Text = Line; }));
or like this:
label1.Invoke(delegate() { label1.Text = Line; });
or like this:
label1.Invoke(() => label1.Text = Line);
These are mostly just syntactic shortcuts to make it easier to represent an action.
Note that lambda expressions often have parameters. When there is only one parameter, the parentheses are optional:
list.ToDictionary(i => i.Key);
When there are no parameters or multiple parameters, the parentheses are necessary to make it obvious what you're doing. Hence, the () =>.
Let's break it down piece by piece.
label1.Invoke(
This is the Control.Invoke method. Here's how it's defined:
public Object Invoke(Delegate method);
Executes the specified delegate on the thread that owns the control's underlying window handle.
What that means is that you give it a reference to a method to call, and Control.Invoke will make sure it gets called on the UI thread (which will prevent cross-threading exceptions while updating the UI.) It takes a default Delegate as a parameter, which means you need to pass it a method that takes no parameters and has no return value. That's where the System.Action delegate type comes in:
public delegate void Action();
Using lambda expressions, we can create an Action delegate inline. First, we specify the delegate type:
label1.Invoke(new Action(
Then, we will begin the lambda syntax. An empty set of parenthesis will denote that the lambda function takes no parameters, and an "arrow" afterwards shows that we want to start the method:
label1.Invoke(new Action(() =>
Now, because the lambda method has no return value (but must execute a statement) we need to surround the code we want to execute on the UI thread in curly braces:
label1.Invoke(new Action(() =>
{
label1.Text = Line;
}
Close up the remaining parenthesis, and you have the full, finished statement.
label1.Invoke(new Action(() =>
{
label1.Text = Line;
}));
Generally when you want to add something to you GUI and you are working from another thread you need to do something called Invocation.
To make an invocation you use either a Controls Invoke method or the something like an Application Dispatcher, these methods generally take an Action. An Action is just what it sounds like, something that is to be performed.
In your case what you are doing is that you want to add a line of text to an element that lives on your GUI, so what you need to do is to create an Action ( anonymouse method ) and in this action you just say "Add this to my Control". And then you Invoke this to avoid cross-threading problems.
()=> is just a "shortcut"(lambda way) to create a method, that is anonymous. This means that you can't call this from anywhere but the context of where you created the anonymous method.
You can also Invoke a "global" method, it doesn't have to be an anonymous method.
An Action is a delegate type, in other words it encapsulates a function. Specifically an Action encapsulates a function that returns void, whereas for instance a Func would encapsulate a function with a return value. These are alot like a function pointers in C++ -- essentially a reference to a function ie a way to encapsulate behavior.
The .Invoke() method takes the Action delegate and runs the function it points to. In this case the function it points to is the lambda expression:
() => { label1.Text = Line }
The initial parentheses denote any parameters being passed into the function. In this case there are no parameters so the parentheses are empty. For example if you wanted to pass in two strings, you would do:
var action = new Action<string, string>( (x, y) => { // use x and y }
Whatever follows the '=>' expression is essentially the body of the function. You have access to the variables specified in the parentheses inside the scope of this body.
Altogether this is a quick way to create an anonymous function on the fly that essentially is equivalent to the following:
public void SetLine()
{
label1.Text = Line;
}
As such you could also create that Action object by doing:
var action = new Action(SetLine)
where you are passing in the name of the method to encapsulate instead of passing in a lambda. Whats passed in is known as a 'Method Group'.
Action is a delegate. Label1.Invoke() is being used to execute the code label1.Text = line to avoid Cross Threading Operation. the event handler for DataReceived event is executing on a different thread other than UI thread. label1.Invoke() will execute the code in UI thread.
This is generating an anonymous method (a lambda, precisely) and passing that to the invoke method. Lambdas are a great way to have code you only need once so you don't need a lot of helper methods doing one thing only.
This is ensuring that the label's text is running in the UI thread. The Port1_DataReceived event will likely run in a background thread, and the Label's text value should not be set from background threads. This prevents that from happening.
I don't know what the label1 is, but the could could be read as:
label1 is an Action, that recieves another action as parameter. It does something and when it calls action recieved in argument.
Now, I've read that and I could be a problem - label1 could not be an Action. As it is just a control which set here: label1.Text = Line;
You have an error in your app;
EDIT
Sorry, just read that:
http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx
Executes the specified delegate on the thread that owns the control's underlying window handle.
Code is correct.

Accessing UI Control from BackgroundWorker Thread

I have a button on my windows form that calls the RunWorkerAsync() method, this in turn performs an action which then updates a ListBox on the same form.
After the DoWork event has finished I assign the Result for the event (Which is a list), I process the RunWorkerCompleted() event and then perform the following code to update my Listbox
which calls this:
(Apologies, code formatting won't work)
Now when I run the application and press the refresh button the following exception appears:
How would I get around this?
Edit:
The exception is thrown on the folowing statement, this occurs in the DoWork method where I clear the contents to keep the list up to date;
listBoxServers.Items.Clear();
You may not call Invoke on the list box, but on the form. For WinForms applications I use something like:
...
this.Invoke((MethodInvoker)delegate()
{
// Do stuff on ANY control on the form.
});
...
Depending on the .NET version, you may have to declare a delegate for MethodInvoker yourself as
public delegate void MethodInvoker();
However, you might also consider using the ReportProgress feature of the Background Worker. The respective event handler should be called in the context of the form's thread.
Here's a snippet which I find very handy:
public static void ThreadSafe(Action action)
{
Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Normal,
new MethodInvoker(action));
}
You can pass it any delegate of Action type or simply a lambda like this:
ThreadSafe(() =>
{
[your code here]
});
or
ThreadSafe(listBoxServers.Items.Clear);
What I've done is something like this every time you need to run something across threads:
listBoxServers.BeginInvoke(
(Action)
(() => listBoxServers.Items.Clear()));
Background threads are not allowed to update the UI in Windows applications, so you have to revert the control back to the UI thread for the actual update.
Create a method that will call UpdateServerDetails on the main thread, like this:
private void DispatchServerDetails(List<ServerDetails> details)
{
Action<List<ServerDetails>> action = UpdateServerDetails;
Dispatcher.Invoke(action)
}
and then call DispatchServerDetails instead of UpdateServerDetails.
Some caveats:
-This works best in WPF applications, for WinForms, you'll need to jump through some hoops, or you can use InvokeRequired
-The UI update is still synchronous, so if UpdateServerDetails does a lot of work, it will block the UI thread (not your case, just to be on the safe side).
Using Invoke in a windows forms project can be a little tricky, there're some pitfalls that are documented but easy to miss. I recommend using something like you'll find in this question:
Is it appropriate to extend Control to provide consistently safe Invoke/BeginInvoke functionality?
It handles cases where invoke is not required, is called from different threads, handle is or isn't created, etcetcetc. It could be easily modified to be SafeInvoke() and SafeBeginInvoke() if you're not a fan of the bool parameter.
(Included here for your convenience:
/// Usage:
this.lblTimeDisplay.SafeInvoke(() => this.lblTimeDisplay.Text = this.task.Duration.ToString(), false);
// or
string taskName = string.Empty;
this.txtTaskName.SafeInvoke(() => taskName = this.txtTaskName.Text, true);
/// <summary>
/// Execute a method on the control's owning thread.
/// </summary>
/// <param name="uiElement">The control that is being updated.</param>
/// <param name="updater">The method that updates uiElement.</param>
/// <param name="forceSynchronous">True to force synchronous execution of
/// updater. False to allow asynchronous execution if the call is marshalled
/// from a non-GUI thread. If the method is called on the GUI thread,
/// execution is always synchronous.</param>
public static void SafeInvoke(this Control uiElement, Action updater, bool forceSynchronous)
{
if (uiElement == null)
{
throw new ArgumentNullException("uiElement");
}
if (uiElement.InvokeRequired)
{
if (forceSynchronous)
{
uiElement.Invoke((Action)delegate { SafeInvoke(uiElement, updater, forceSynchronous); });
}
else
{
uiElement.BeginInvoke((Action)delegate { SafeInvoke(uiElement, updater, forceSynchronous); });
}
}
else
{
if (!uiElement.IsHandleCreated)
{
// Do nothing if the handle isn't created already. The user's responsible
// for ensuring that the handle they give us exists.
return;
}
if (uiElement.IsDisposed)
{
throw new ObjectDisposedException("Control is already disposed.");
}
updater();
}
}
I just figured out a simpler way without using Invoke:
int fakepercentage = -1;
//some loop here......if no loop exists, just change the value to something else
if (fakepercentage == -1)
{
fakepercentage = -2;
}
else
{
fakepercentage = -1;
}
backgroundworker1.ReportProgress(fakepercentage);
Then in backgroundworker1_ProgressChanged(object sender, ProgressChangedEventArgs e):
if (e.ProgressPercentage < 0)
{
//access your ui control safely here
}

crossthread operations error

if (listBox1.InvokeRequired)
{
listBox = new StringBuilder(this.listBox1.Text);
}
This is the code in c# which when executed produces an invalid cross thread operation error for listBox1 which is a listbox in my form.
Could u guys please tell me why??
I am using the invokeRequired method too and am not changing the contents of the listbox either.
InvokeRequired only tells you that an Invoke is necessary in order to validly access the element. It doesn't make the access legal. You must use the invoke method to push the update to the appropriate thread
Action update = () => listbox = new StringBuilder(this.listBox1.Text);
if (listBox1.InvokeRequired) {
listBox1.Invoke(update);
} else {
update();
}
InvokeRequired simply checks to see if Invoke is required. You found it's required, yet didn't call Invoke!
Your code should run when InvokeRequired is false
delegate void SetListBoxDelegate();
void SetListBox()
{
if(!InvokeRequired)
{
listBox = new StringBuilder(this.listBox1.Text);
}
else
Invoke(new SetListBoxDelegate(SetListBox));
}
Edit:
Check out Making Windows Forms thread safe

Categories