In my code I am using await Task.Run so the form does not freeze while uploading files to SFTP
I got similar error when I was trying to write to textbox and fixed it with LogUpload function
but now I get this error when I try to access ListView items
System.InvalidOperationException: 'Cross-thread operation not valid: Control 'lvwFiles' accessed from a thread other than the thread it was created on.'
I wonder how can I fix that
here is my code
await Task.Run(() =>
{
using (SftpClient client = new SftpClient(host, port, username, password))
{
client.Connect();
if (client.IsConnected)
{
LogUpload("I'm connected to the client" + "\r\n");
foreach (ListViewItem item in lvwFiles.Items) //<<---- Causing the error
{
string fileName = item.SubItems[0].Text;
string uploadFile = item.SubItems[1].Text;
and the function to LogUpload is
private void LogUpload(string txt)
{
if (txtLog.InvokeRequired)
{
txtLog.Invoke(new MethodInvoker(delegate { txtLog.Text = txt + txtLog.Text; }));
}
}