Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends I'm doing a project in transferring files through bluetooth using c#.net and i finished that successfully and now i want to show that transferring in simulation using c# and i searched a lot but no positive result.
Posted

You can show simulation using a progress bar and time remaining to copy.

Simpulation using progess bar is very easy.
1) Just place a progess bar.
2) Count total number of files to be copied.
3) Set this as maximum property of progess bar.
4) Use a delegate to refersh progess bar on each file copy and use Application.DoEvents() too for refereshing UI.

And you must share your code how you copy using bluetooth. Yet I haven't tried it.

I think this will do all. If you have any doubts please fell free to ask me.
 
Share this answer
 
Comments
Olivier Levrey 7-Mar-11 5:21am    
Using a progress bar is OK, but you should NEVER use Application.DoEvents! In most cases, this leads to unexpected side effects.
johannesnestler 7-Mar-11 6:04am    
concept is ok, but I had to downvote for your Application.DoEvents() recommendation - this is not good... Maybe you want to improve this part with [refresh progress from a worker-thread]
Anburaj.G 7-Mar-11 6:16am    
Hi sunder i accept your answer but i want to display it in simulation like how it identifies destination and how it transfer the file
sunder.tinwar 7-Mar-11 6:17am    
Actually I guided you to use this Application.DoEvents() in delegate of on main foreground thread. so that UI will get refeshed.

And in background work or seperate thread we can copy file. From there on each copy of file we can call the delegate. Ill provide you code.
Anburaj.G 7-Mar-11 6:50am    
Thank you Sunder
If your problem is just simulating the transfer, then this should be enough:

C#
/// <summary>
/// Simulates a file tranfer.
/// </summary>
/// <param name="fileSize">Size of the file.</param>
/// <param name="bitsPerSecond">Transfer speed.</param>
void Transfer(long fileSize, float bitsPerSecond)
{
    float byteRate = bitsPerSecond / 8;
    //time needed in seconds
    float timeNeeded = fileSize / byteRate;
    //convert the time needed in milliseconds
    int ms = (int)(timeNeeded * 1000);
    //simulate the transfer time
    System.Threading.Thread.Sleep(ms);
}


If you want something to show the progress, then you can use that:
ProgressForm: a simple form linked to a BackgroundWorker[^]
 
Share this answer
 
The simulation can go in such way.

Create a delete to referesh UI.
From you presentation layer start background worker.
In that call a method which actually identify location and count total number of files to be copied using below function.

By using this we can count files...

C#
public static int CountFiles(string source,string destination)
       {
           int fileCount = 0;
           Stack<Folder> stack = new Stack<Folder>();
           stack.Push(new Folder(source, destination));
           while (stack.Count > 0)
           {
               var path = stack.Pop();
               foreach (string filepath in Directory.GetFiles(path.sourcePath, "*.*"))
               {
                   fileCount++;
               }
               foreach (string dir in Directory.GetDirectories(path.sourcePath))
               {
                   stack.Push(new Folder(dir, destination));
               }
           }
           //string [] files=Directory.GetFiles(source,"*.*",SearchOption.AllDirectories);
           //fileCount = files.Length;
           _fileCount = fileCount;
           return fileCount;
       }


According to my approch, I have created a class and used stack for counting and coying file which is more faster then recursive technique.

Then from Background worker only or from the same method start coying.

public static void CopyFiles(ReportProgressWorker worker,string source, string destination)
{
_avgTime = new TimeSpan();
TimeSpan start, end;
string time=string.Empty;
Stack<Folder> stack = new Stack<Folder>();
stack.Push(new Folder(source, destination));
copiedFile = worker;
while (stack.Count > 0)
{
var path=stack.Pop();
string dirpath=Path.Combine(path.destPath,Path.GetFileName(path.sourcePath));
if(!Directory.Exists(dirpath))
Directory.CreateDirectory(dirpath);
foreach (string filepath in Directory.GetFiles(path.sourcePath,"*.*"))
{
string targetpath=Path.Combine(dirpath,Path.GetFileName(filepath));
if (File.Exists(targetpath))
File.Delete(targetpath);
start = DateTime.Now.TimeOfDay;
File.Copy(filepath, targetpath);
_processedfiles++;
end = DateTime.Now.TimeOfDay;
string temp = ComputeTimeRequired(start, end);
if (!string.IsNullOrEmpty(temp))
time = temp;
copiedFile(Path.GetFileName(targetpath),time);
}
foreach (string dir in Directory.GetDirectories(path.sourcePath))
{
stack.Push(new Folder(dir, Path.Combine(dirpath, Path.GetFileName(dir))));
}
}
}

This copy and update together.

public void ReportTheProgress(string prg,string timeStatus)

using this progess change event update progess bar. I think it will do all.

If I am wrong anywhere please correct me guys.
Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900