Click here to Skip to main content
15,914,943 members
Home / Discussions / C#
   

C#

 
GeneralRe: Disabling MenuItems Pin
naglbitur5-Dec-05 7:05
naglbitur5-Dec-05 7:05 
AnswerRe: Disabling MenuItems Pin
mav.northwind5-Dec-05 7:18
mav.northwind5-Dec-05 7:18 
GeneralRe: Disabling MenuItems Pin
naglbitur6-Dec-05 2:06
naglbitur6-Dec-05 2:06 
AnswerRe: Disabling MenuItems Pin
André Ziegler5-Dec-05 4:04
André Ziegler5-Dec-05 4:04 
QuestionBackgroundWorker and ProgressBar Pin
SunsOfFun4-Dec-05 3:57
SunsOfFun4-Dec-05 3:57 
AnswerRe: BackgroundWorker and ProjessBar Pin
Rob Philpott4-Dec-05 4:40
Rob Philpott4-Dec-05 4:40 
GeneralRe: BackgroundWorker and ProjessBar Pin
SunsOfFun4-Dec-05 6:49
SunsOfFun4-Dec-05 6:49 
GeneralRe: BackgroundWorker and ProjessBar Pin
Rob Philpott4-Dec-05 8:09
Rob Philpott4-Dec-05 8:09 
Hi,

I've cobbled together an implementation you can use to get you started. Please note this is rough and ready and has no Exception handling in it etc. Ideally, to do the job properly I'd implement a new class and provide BeginCopy, CancelCopy and EndCopy static methods as we are encouraged to do for asynchronous methods.

BTW - because you will launch the copy on a worker thread, make sure you switch thread before attempting to update your progress bar (use Control.BeginInvoke).

<br />
using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
using System.IO;<br />
<br />
namespace ConsoleApplication1<br />
{<br />
    class Program<br />
    {<br />
        static void Main(string[] args)<br />
        {<br />
            FileCopy(@"f:\BTSDev Hard Disk.vhd", @"c:\test.vhd", new UpdateProgressDelegate(RelayProgress));<br />
        }<br />
<br />
        public static void RelayProgress(int percentile)<br />
        {<br />
            Console.WriteLine("{0}% complete", percentile);<br />
        }<br />
<br />
        delegate void UpdateProgressDelegate(int percentile);<br />
<br />
        static void FileCopy(string sourceFileName, string destinationFileName, UpdateProgressDelegate handler)<br />
        {<br />
            byte[] buffer = new byte[1024];<br />
<br />
            FileStream fsInput = new FileStream(sourceFileName, FileMode.Open, FileAccess.Read);<br />
            FileStream fsOutput = new FileStream(destinationFileName, FileMode.Create, FileAccess.Write);<br />
            <br />
            long totalLength = fsInput.Length;<br />
            long readSoFar = 0;<br />
            int percentile = 0;<br />
<br />
            // send the 0% complete messsage<br />
            handler(0);<br />
                       <br />
            int readThisBuffer = fsInput.Read(buffer, 0, 1024);<br />
<br />
            while (readThisBuffer > 0)<br />
            {<br />
                readSoFar += readThisBuffer;<br />
                int newPercentile = (int)((double)readSoFar / (double)totalLength * 100.0);<br />
                if (newPercentile > percentile)<br />
                {<br />
                    percentile = newPercentile;<br />
                    <br />
                    if (handler != null)<br />
                    {<br />
                        handler(percentile);<br />
                    }<br />
                }<br />
                <br />
                fsOutput.Write(buffer, 0, readThisBuffer);<br />
                readThisBuffer = fsInput.Read(buffer, 0, 1024);<br />
            }<br />
<br />
            // send the 100% complete messsage<br />
            handler(100);<br />
<br />
            fsInput.Close();<br />
            fsOutput.Close();<br />
        }   <br />
    }<br />
}<br />


Regards,
Rob Philpott.
GeneralRe: BackgroundWorker and ProjessBar Pin
SunsOfFun4-Dec-05 19:04
SunsOfFun4-Dec-05 19:04 
GeneralRe: BackgroundWorker and ProjessBar Pin
Rob Philpott4-Dec-05 22:02
Rob Philpott4-Dec-05 22:02 
QuestionAbout encoding setting? Pin
pmasknguyen4-Dec-05 3:34
pmasknguyen4-Dec-05 3:34 
QuestionComboBox question Pin
1nsp1r3d4-Dec-05 1:11
1nsp1r3d4-Dec-05 1:11 
AnswerRe: ComboBox question Pin
Curtis Schlak.4-Dec-05 2:38
Curtis Schlak.4-Dec-05 2:38 
GeneralRe: ComboBox question Pin
1nsp1r3d4-Dec-05 3:36
1nsp1r3d4-Dec-05 3:36 
GeneralRe: ComboBox question Pin
Curtis Schlak.4-Dec-05 11:31
Curtis Schlak.4-Dec-05 11:31 
QuestionUsing Dispose in Classes? Pin
redfish343-Dec-05 23:24
redfish343-Dec-05 23:24 
AnswerRe: Using Dispose in Classes? Pin
Guffa3-Dec-05 23:41
Guffa3-Dec-05 23:41 
AnswerRe: Using Dispose in Classes? Pin
Colin Angus Mackay3-Dec-05 23:42
Colin Angus Mackay3-Dec-05 23:42 
AnswerThanks for Help Pin
redfish344-Dec-05 13:07
redfish344-Dec-05 13:07 
QuestionWhat Does this Code Mean? Pin
redfish343-Dec-05 23:21
redfish343-Dec-05 23:21 
AnswerRe: What Does this Code Mean? Pin
Colin Angus Mackay3-Dec-05 23:44
Colin Angus Mackay3-Dec-05 23:44 
GeneralRe: What Does this Code Mean? Pin
leppie4-Dec-05 2:30
leppie4-Dec-05 2:30 
AnswerRe: What Does this Code Mean? Pin
Colin Angus Mackay3-Dec-05 23:46
Colin Angus Mackay3-Dec-05 23:46 
AnswerRe: What Does this Code Mean? Pin
Guffa3-Dec-05 23:52
Guffa3-Dec-05 23:52 
AnswerThanks for Help Pin
redfish344-Dec-05 13:21
redfish344-Dec-05 13:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.