Hi
Error - Cannot convert Lambda expression to delegate type because it is not a delegate type
Below is the code
C#
dgvWindows.BeginInvoke(() =>
{
if (!_isRunning)
return;
var row = dgvWindows.Rows[dgvWindows.Rows.Add()];
row.Cells[0].Value = handle;
row.Cells[1].Value = _driver == null ? string.Empty : _driver.Title;
row.Cells[2].Value = state;
row.Cells[3].Value = $"{_duration.Hours:00}:{_duration.Minutes:00}:{_duration.Seconds:00}";
row.HeaderCell.Value = row.Index + 1;
BeginInvoke(() => Text = $"YouTube Tools - {row.Index + 1}");
});Thanks

Ramco RamcoRank #462 · 5.4K reputationPosted Apr 7, 2024, 2:32 AM EDT
Hi Tahir
Sorry the code was below but error is same
dgvWindows.BeginInvoke(() => { foreach (DataGridViewRow row in dgvWindows.Rows) { var handle = row.Cells[0].Value.ToString(); _driver .SwitchTo() .Window(handle); var btnPlay = GetElement(XPATH_PLAY); btnPlay?.Click(); row.Cells[2].Value = "Playing"; } });Thanks
Tahir AnsariRank #366 · 6.8K reputationPosted Apr 7, 2024, 2:16 AM EDT
In this case, it seems like
BeginInvokeis expecting a delegate type, but the lambda expression() => Text = $"YouTube Tools - {row.Index + 1}"doesn't match that delegate type.assuming
BeginInvokeis a method that takes anActiondelegate:dgvWindows.BeginInvoke(new Action(() => { if (!_isRunning) return; var row = dgvWindows.Rows[dgvWindows.Rows.Add()]; row.Cells[0].Value = handle; row.Cells[1].Value = _driver == null ? string.Empty : _driver.Title; row.Cells[2].Value = state; row.Cells[3].Value = $"{_duration.Hours:00}:{_duration.Minutes:00}:{_duration.Seconds:00}"; row.HeaderCell.Value = row.Index + 1; BeginInvoke(new Action(() => Text = $"YouTube Tools - {row.Index + 1}")); }));Jithu ThomasRank #437 · 5.7K reputationPosted Apr 7, 2024, 7:23 AM EDT
i think the same has been resolved in the below mentioned thread.
https://www.c-sharpcorner.com/Forums/cannot-convert-lambda-expression-to-type-sharp39delegatesharp39
Jithu ThomasRank #437 · 5.7K reputationPosted Apr 7, 2024, 7:25 AM EDT
dgvWindows.BeginInvoke(new MethodInvoker(() => { if (!_isRunning) return; var row = dgvWindows.Rows[dgvWindows.Rows.Add()]; row.Cells[0].Value = handle; row.Cells[1].Value = _driver == null ? string.Empty : _driver.Title; row.Cells[2].Value = state; row.Cells[3].Value = $"{_duration.Hours:00}:{_duration.Minutes:00}:{_duration.Seconds:00}"; row.HeaderCell.Value = row.Index + 1; BeginInvoke(new MethodInvoker(() => Text = $"YouTube Tools - {row.Index + 1}")); }));