Reputation: 4735
I am doing a project in ftp,which will do multiple uploads,and the process i am doing is compressing the file then encrypting then cut into several pieces and send it to the server i assign all these things to a thread.likewise a thread will be there for every file i assign .
this is the new piece of code and it has only one functionality the same error appears here also please help me to find out whats wrong here
public partial class Form1 : Form
{ ArrayList AscendingList = new ArrayList(); ListViewItem Litem = null; Thread MyThread = null; ThreadStart Starter = null;
public Form1()
{
InitializeComponent();
}
private void btn_split_Click(object sender, EventArgs e)
{
foreach (ListViewItem litem in listView1.Items)
{
Starter = delegate { SplitFile(litem.Text,litem.SubItems[1].Text,int.Parse(litem.SubItems[2].Text)); };
MyThread = new Thread(Starter);
MyThread.IsBackground = true;
MyThread.Start();
}
}
public void SplitFile(string inputFile, string outputPrefix, int chunkSize)
{
int pointr = 0;
byte[] buffer = new byte[chunkSize];
using (FileStream fs = new FileStream(inputFile, FileMode.Open, FileAccess.Read, FileShare.None))
{
int index = 0;
pointr = fs.Read(buffer, 0, buffer.Length);
while (pointr != 0)
{
using (FileStream fso = new FileStream(outputPrefix + "\\" + index + ".log", FileMode.Create))
{
AscendingList.Add(fso.Name);
fso.Write(buffer, 0, pointr);
pointr = fs.Read(buffer, 0, buffer.Length);
}
index++;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
Litem = new ListViewItem();
Litem.Text = "E:\\butterfly.mpg";
Litem.SubItems.Add("H:\\karthik");
Litem.SubItems.Add("102400");
listView1.Items.Add(Litem);
}
private void button2_Click(object sender, EventArgs e)
{
Litem = new ListViewItem();
Litem.Text = "E:\\karthik.mpeg";
Litem.SubItems.Add("H:\\karthik\\karthik");
Litem.SubItems.Add("102400");
listView1.Items.Add(Litem);
}
}
Upvotes: 2
Views: 462
Reputation: 48066
This code is a mess; you should probably try to clean it up. In the process, you may discover your bug gets fixed by itself.
SplitFile
and Decompress
..Read(
statements that assume that read actually reads the full buffer - it doesn't, it waits until at least 1 byte is available and just returns what's immediately available, or returns 0 if the stream is finished. You should never ignore the count of bytes returned by the .Read(
method. If you split out the (better) while-based code from SplitFile
and Decompress
into a helper method, you can use this elsewhere too. This is quite likely to cause problems when writing to a network or physical drive..Flush()
before .Close()
.IDisposable
in a using block. Even things like MemoryStream
and CryptoStream
- it's likely not to matter, but I see you're .Close()
-ing them anyhow (so you're not saving any code), and you're violating the code-contract and you probably don't know about their implementation, so you're relying on unspecified behavior; that's not worth it..Substring(....).ToString()
is equivalent to .Substring()
Basically, it looks like you're making a big complicated thing using technology you're not quite familiar with; try splitting it into small, clean bits that allow you to more precisely pinpoint what you need - that way you can better maintain control over any new tech.
Clean up what you have first; then try pinpointing whatever error(s) you have remaining - if you're a bit lucky, you won't have any...
I hope this helps!
Upvotes: 13
Reputation: 24098
unable to access the file
This is common for multithreaded applications where multiple threads are trying to access the same file.
What you need to do is to ensure this is not happening.
You should not share any objects, and each thread should work with its own files (no intersectinos).
Looking at the code I can see that Slicer. instace is shared.
Try to move all the code into the Thread's Starter delegate and instantiate all the objects there.
Upvotes: 1
Reputation: 12334
This is problematic:
string EncryptedFile = "";
Slicer.SplitFile(EncryptedFile, lt.SubItems[3].Text, 10240);
The Slicer.SplitFile() call is being asked to work on a nonexistent file.
Upvotes: 2
Reputation:
Why are you using FileShare.ReadWrite when creating fsout, are you trying to write to the same file from different threads? That won't work, at least not using GZipStream like that. With the other file you're writing you've specified FileShare.None which I assume means you're not trying to write to the same file from multiple threads in that case.
Upvotes: 1