0

So i have a method that does an invoke on a datagridview and works fine for the first thread that runs it, however when a second thread tries to utilise the method, the download part of it still functions, however the invoke statement stops working for the first thread and starts to change both

public void ByteTransferResume(int indexResume)
{
    HttpWebRequest req;
    HttpWebResponse resp;
    req = (HttpWebRequest)HttpWebRequest.Create(FileLocationName);
    req.AddRange((int)fileInfoDestination.Length);
    resp = (HttpWebResponse)(req.GetResponse());
    long fileLength = resp.ContentLength;
    FileLocationLength = fileLength;
    using (Stream responseStream = resp.GetResponseStream())
    {
        int iBufferSize = 1024;
        iBufferSize *= 1000;
        using (FileStream saveFileStream = new FileStream(FileDestination, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
        {
            int iByteSize;
            byte[] downBuffer = new byte[iBufferSize];

            while ((iByteSize = responseStream.Read(downBuffer, 0, downBuffer.Length)) > 0)
            {
                saveFileStream.Write(downBuffer, 0, iByteSize);

                FileInfo fileInfoDestinations = new FileInfo(FileDestination);

                FileDestinationLength = (int)fileInfoDestinations.Length;

                double downloadProgress = ((double)FileDestinationLength / FileLocationLength) * 100;

                // MessageBox.Show(downloadProgress.ToString());

                dgvDownloadInfo.Invoke(new dgvCommandDelegate(DGVCommand), new object[] { $"UPDATE Download_Info SET [Progress] = '{downloadProgress:F2}%' WHERE [File Name] = '{thread1[indexResume].Name}'" });

                //MessageBox.Show(thread1[indexResume].Name);
                //MessageBox.Show(indexResume.ToString());
                // dgvDownloadInfo.Invoke(new dgvConnectionDelegate(DGVConnection));
                Thread.Sleep(10);
            }
        }
    }
}
Craig W.
  • 17,838
  • 6
  • 49
  • 82

1 Answers1

0

Maybe this will help you:

 public object _lock = new object();
 public void ByteTransferResume(int indexResume)
 {
     lock (_lock)
     {
         HttpWebRequest req;
         //rest of your method         
     }
  }
daniell89
  • 1,832
  • 16
  • 28
  • it didnt work im afraid, thanks anyway, sorry to be a pain im very new to this but i appreciate it all guys :) ill get there eventually!! :) – foreveraphone Mar 15 '17 at 16:40