Should i use one backgroundworker for two different processes or to add another...

  • Thread starter Thread starter Chocolade1972
  • Start date Start date
C

Chocolade1972

Guest
I have now one backgrounworker:


private void ftpProgress1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.toolStripStatusLabel1.Text = e.UserState.ToString();
this.toolStripProgressBar1.Value = Math.Min(this.toolStripProgressBar1.Maximum, e.ProgressPercentage);
}


Im using to upload files to my ftp and also to report to progressbar.

But i have another task that sometimes might work alone and sometimes at the same time with the files upload to the ftp.

The second task is to get from the ftp all directories files and display them on treeview.

This is how im getting and displaying the ftp server content on the treeView1:


private TreeNode CreateDirectoryNode(string path, string name)
{
var directoryNode = new TreeNode(name);
var directoryListing = GetDirectoryListing(path);

var directories = directoryListing.Where(d => d.IsDirectory);
var files = directoryListing.Where(d => !d.IsDirectory);

foreach (var dir in directories)
{
directoryNode.Nodes.Add(CreateDirectoryNode(dir.FullPath, dir.Name));
}
foreach (var file in files)
{
directoryNode.Nodes.Add(new TreeNode(file.Name));
}
return directoryNode;
}

public IEnumerable<FTPListDetail> GetDirectoryListing(string rootUri)
{
var CurrentRemoteDirectory = rootUri;
var result = new StringBuilder();
var request = GetWebRequest(WebRequestMethods.Ftp.ListDirectoryDetails, CurrentRemoteDirectory);
using (var response = request.GetResponse())
{
using (var reader = new StreamReader(response.GetResponseStream()))
{
string line = reader.ReadLine();
while (line != null)
{
result.Append(line);
result.Append("\n");
line = reader.ReadLine();
}
if (string.IsNullOrEmpty(result.ToString()))
{
return new List<FTPListDetail>();
}
result.Remove(result.ToString().LastIndexOf("\n"), 1);
var results = result.ToString().Split(\n);
string regex =
@"^" + //# Start of line
@"(?<dir>[\-ld])" + //# File size
@"(?<permission>[\-rwx]{9})" + //# Whitespace \n
@"\s+" + //# Whitespace \n
@"(?<filecode>\d+)" +
@"\s+" + //# Whitespace \n
@"(?<owner>\w+)" +
@"\s+" + //# Whitespace \n
@"(?<group>\w+)" +
@"\s+" + //# Whitespace \n
@"(?<size>\d+)" +
@"\s+" + //# Whitespace \n
@"(?<month>\w{3})" + //# Month (3 letters) \n
@"\s+" + //# Whitespace \n
@"(?<day>\d{1,2})" + //# Day (1 or 2 digits) \n
@"\s+" + //# Whitespace \n
@"(?<timeyear>[\d:]{4,5})" + //# Time or year \n
@"\s+" + //# Whitespace \n
@"(?<filename>(.*))" + //# Filename \n
@"$"; //# End of line

var myresult = new List<FTPListDetail>();
foreach (var parsed in results)
{
var split = new Regex(regex)
.Match(parsed);
var dir = split.Groups["dir"].ToString();
var permission = split.Groups["permission"].ToString();
var filecode = split.Groups["filecode"].ToString();
var owner = split.Groups["owner"].ToString();
var group = split.Groups["group"].ToString();
var filename = split.Groups["filename"].ToString();
myresult.Add(new FTPListDetail()
{
Dir = dir,
Filecode = filecode,
Group = group,
FullPath = CurrentRemoteDirectory + "/" + filename,
Name = filename,
Owner = owner,
Permission = permission,
});
};
return myresult;
}
}
}

private FtpWebRequest GetWebRequest(string method, string uri)
{
Uri serverUri = new Uri("ftp://"+ uri);
if (serverUri.Scheme != Uri.UriSchemeFtp)
{
return null;
}
var reqFTP = (FtpWebRequest)FtpWebRequest.Create(serverUri);
reqFTP.Method = method;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(txtUsername.Text, txtPassword.Text);
reqFTP.Proxy = null;
reqFTP.KeepAlive = false;
reqFTP.UsePassive = false;
return reqFTP;
}

public class FTPListDetail
{
public bool IsDirectory
{
get
{
return !string.IsNullOrWhiteSpace(Dir) && Dir.ToLower().Equals("d");
}
}
internal string Dir { get; set; }
public string Permission { get; set; }
public string Filecode { get; set; }
public string Owner { get; set; }
public string Group { get; set; }
public string Name { get; set; }
public string FullPath { get; set; }
}


1. If using another backgroundworker in any case how do i report the time its taking in percentages to get all files and directories ?


2. Since its taking time to get all files and directories how can i also display in real time/live the treeView1 update ? I mean once its getting the first files and directories show them on the treeView1 instead waiting to the end and show them all.


3. How to use the files and directories on treeView1 with the backgroundworker events dowork,reporting progress and completed ?

Continue reading...
 
Back
Top