C
C#2y ago
ippo

❔ Update Form in a new Thread!

I created a form with a datagridview. Now I want to run a powershell command in a new thread and in this new thread I want to add the data from the powershell command in the datagridview. With normal threads I get an error which says that I can not call the datagridview from the main thread is there another way doing that?
15 Replies
jcotton42
jcotton422y ago
you cannot touch UI controls outside of the UI thread to post code to the UI thread, you can use the Invoke method on any control from your Form
ippo
ippoOP2y ago
@jcotton42 I used Invoke with Action and with delegate but nothing works I mean I get the results at the same time and not one after the other, but it is still locking the UI thread
jcotton42
jcotton422y ago
wdym "locking the UI thread"? as in the app is not responsive during the operation? how are you running the powershell?
ippo
ippoOP2y ago
foreach (PSObject result in myCommand.Invoke()) {
dataGridView.Rows.Add(
result.Members["Name"].Value,
result.Members["Count"].Value,
);
}
foreach (PSObject result in myCommand.Invoke()) {
dataGridView.Rows.Add(
result.Members["Name"].Value,
result.Members["Count"].Value,
);
}
as in the app is not responsive during the operation?
yes! this is what I tried:
dataGridView1.Invoke((MethodInvoker)delegate {
// the foreach code with the ps invoke()
});
dataGridView1.Invoke((MethodInvoker)delegate {
// the foreach code with the ps invoke()
});
dataGridView1.Invoke(new Action(() => {
// the foreach code
});
dataGridView1.Invoke(new Action(() => {
// the foreach code
});
dataGridView1.Invoke(new Action(() => {
// the foreach with invoke()
});
dataGridView1.Invoke(new Action(() => {
// the foreach with invoke()
});
Thread t = new Thread(() => {
dataGridView1.Invoke(new Action(() => {
// the foreach with invoke()
});
});

t.Start()
Thread t = new Thread(() => {
dataGridView1.Invoke(new Action(() => {
// the foreach with invoke()
});
});

t.Start()
all of these solutions are locking
jcotton42
jcotton422y ago
sorry, I more meant how you are invoking the powershell code? the bit that would actually be typing up the UI
ippo
ippoOP2y ago
not sure what you mean, but maybe you mean this?
PowerShell ps = PowerShell.Create();
ps.AddScript(myPsScript);

foreach (PSObject result in ps.Invoke()) {
dataGridView.Rows.Add(
result.Members["Name"].Value,
result.Members["Count"].Value,
);
}
PowerShell ps = PowerShell.Create();
ps.AddScript(myPsScript);

foreach (PSObject result in ps.Invoke()) {
dataGridView.Rows.Add(
result.Members["Name"].Value,
result.Members["Count"].Value,
);
}
jcotton42
jcotton422y ago
yeah that the UI will be blocked during the entirety of ps.Invoke
ippo
ippoOP2y ago
correct 🙂 can you help me to make that non-blocking?
jcotton42
jcotton422y ago
there should be an InvokeAsync on it iirc if not you'll have to shunt it off the UI thread with something like Task.Run
ippo
ippoOP2y ago
@jcotton42 there is no minimal example you can show 😦 ?
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
ippo
ippoOP2y ago
does anyone have an idea how to do that with threads?
jcotton42
jcotton422y ago
what part do you need help on?
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server