I am writing some code to solve thread-safe issues in a system am working on, and one of the recommended approaches is to use delegates to solve cross-thread issues. But for some reason, I don't like having to define a delegate for every possible operation i might have to intercept, and thus prefer working with anonymous methods or lambda expressions, but I the compiler refuses to cast any of these to a System.Delegate object.
Is there a shortcut to this?
if (someListBox.InvokeRequired)
{
someListBox.Invoke(Some_System.Delegate_Object, new object[] {item});
}
else
someListBox.Items.Add(item);
I want something like...
if (someListBox.InvokeRequired)
{
someListBox.Invoke((i) => { someListBox.Items.Add(i); }, new object[] {item});
}
else
someListBox.Items.Add(item);
One of your problems is that the compiler can't infer the parameter types of your lamda. And even for a given parameter signature there are infinitely many potential delegate types. So you need to explicitly give a type. Action<...>
and Func<...>
are typical candidates if you don't care about parameter names.
I think this should work:
`someListBox.Invoke((Action<int>)((i) => {listviewResults.Items.Add(i); }), new object[] {item});`
Or in refactored form to avoid repeating yourself:
Action<int> myAction=(i) => listviewResults.Items.Add(i);
if (someListBox.InvokeRequired)
{
someListBox.Invoke( myAction, new object[] {item});
}
else
myAction(item);
And I see no reason why you'd want to have i
as a parameter at all:
Action myAction = () => listviewResults.Items.Add(item);
if (someListBox.InvokeRequired)
{
someListBox.Invoke( myAction );
}
else
myAction();
The downside with the approach that you want is that the actual work will be implemented in two places. One alternative approach might look like this:
private void AddItemToListView(ListViewItem item, ListView listView)
{
if (listView.InvokeRequired)
{
listView.BeginInvoke((Action)delegate { AddItemToListView(item, listView); });
}
else
{
listView.Items.Add(item);
}
}
Then again, you could debate how often this code is executed. If it is not extremely much, perhaps its better to simplify it a bit by not checking for InvokeRequired
, but rather always wrap the call in a delegate passed to BeginInvoke
:
private void AddItemToListView(ListViewItem item, ListView listView)
{
listView.BeginInvoke((Action)delegate { listView.Items.Add(item); });
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.