karthik
karthik

Reputation: 4735

Problem With thread

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

Answers (4)

Eamon Nerbonne
Eamon Nerbonne

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.

  • You have several empty catch clauses which is a big red flag (the commented out using block is a much better idea). These should all be removed; it's very unlikely these are a good idea.
  • You've got a thread.Sleep statement which is probably superflous - and if it's not, that's a sign of a threading bug.
  • You should split out basic functionality into helper methods. This increases the readability and thus debuggability of your code - and automatically provides some documentation in the form of the name of the private helper method. For instance, your read-from A write-to B code could be a method - you've duplicated this functionality in both SplitFile and Decompress.
  • You have a bunch of erroneous .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.
  • multiple using blocks can be written without extra curly braces to improve readability. If you do this, VS.NET won't add an indent level for every using() clause, but just one for all of them.
  • It's not quite clear to me, but it looks like you're working with a bunch of intermediate files. A cleaner (and potentially much faster) approach would be to simply work on streams and have a wrapper that happens to provide file streams.
  • You don't need to .Flush() before .Close().
  • It's a good habit to put really every 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

Dmytrii Nagirniak
Dmytrii Nagirniak

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

Kelly S. French
Kelly S. French

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

user149533
user149533

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

Related Questions