Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/forums/c-sharp/)
-   -   How to enable a cancel button when an upload takes place. (http://www.go4expert.com/forums/enable-cancel-button-upload-takes-t1885/)

Leo 14Nov2006 12:52

How to enable a cancel button when an upload takes place.
 
Actually my aim to enable a cancel button while this upload takes place.I tried raising an event which makes the cancel button enabled while the upload process is going on.

But I am not able to click on the cancel button, i dont get the access to click it.Once the process is completed i am able to click the button to generate the event.

Can anyone tell me how this code can be done?

shabbir 14Nov2006 14:09

Re: How to enable a cancel button when an upload takes place.
 
You have posted it as an Article under the Article / Source code section. I have moved it to the Queries and Discussion forum.

Use thread to upload as it looks like the upload will not allow any other event to get handled.

pradeep 14Nov2006 15:32

Re: How to enable a cancel button when an upload takes place.
 
Is there anythinhg like VB's DoEvents in C#?? If so, it can help.

Leo 14Nov2006 16:42

Re: How to enable a cancel button when an upload takes place.
 
Thanks Shabbir for moving my query in to Queries and Discussion forum.

shabbir 14Nov2006 18:25

Re: How to enable a cancel button when an upload takes place.
 
BeginInvoke is similar but not exactly same to DoEvents in VB. Do Events allow other events to be processed and BeginInvoke calls you function into a seperate thread allowing to handle other events in the main process,

pradeep 14Nov2006 18:26

Re: How to enable a cancel button when an upload takes place.
 
That's pretty good enough, that'll do the trick here.

Leo 16Nov2006 20:00

Re: How to enable a cancel button when an upload takes place.
 
I tried creating an event EventLoad(true) and making the cancelbutton visible in that event , still the button did not become visible.

Pls help me out.
This is the code sample which i am using.It contains the upload process and the Event i created.
Code:

private void DoUploadProcess(string filePath)
    {
        FileStream fileToUpLoad = new FileStream(filePath, FileMode.Open, FileAccess.Read);
        int chunkSize = LoadManagerConstants.ChunkSize; //Chunk size is 532480             
        byte[] buffer = new byte[chunkSize];
        long fileSize = fileToUpLoad.Length;
        int bytesRead = fileToUpLoad.Read(buffer, LoadManagerConstants.Offset, chunkSize);//Offset=0

        this._taskId = this._workFlowProxy.BeginLoad(this._workFlowProxy.CurrentObjectClassID);
        this._statusProgressBar.Value = LoadManagerConstants.InitialValue;

        //If file to be uploaded is larger even after 6 uploads the chunk size is recalculated for the
        //remaining uploads
        int iteration = 1;
        int preferredTransferDuration = LoadManagerConstants.PreferredTransferDuration; //PreferredTransferDuration = 2000
        int percentageOfTotalSize = LoadManagerConstants.PercentageOfTotalSize;//PercentageOfTotalSize = 1
        int bytesWritten;
        long timeForInitialChunks;
        long averageChunkTime;
        long sentBytes = 0;
       
        while (bytesRead > 0)
        {
            EventLoad.Invoke(true);
            if (gAbort) break;

            if (iteration == 1)
                this._startTime = DateTime.Now;

            bytesWritten = this._workFlowProxy.UploadChunk(this._taskId, buffer, sentBytes, bytesRead);
            if (iteration == LoadManagerConstants.AverageSample + 1)        //AverageSample = 5
            {
                timeForInitialChunks = (long)DateTime.Now.Subtract(this._startTime).TotalMilliseconds;
                averageChunkTime = System.Math.Max
                    (1, timeForInitialChunks / LoadManagerConstants.AverageSample);

                //Maximum size that can be uploaded at a time cannot be more than 4KB
                chunkSize = (int)System.Math.Min(LoadManagerConstants.MaxFileSize,
                    chunkSize * preferredTransferDuration / averageChunkTime);

                buffer = new byte[chunkSize];
            }
            sentBytes += bytesRead;

            //Retrieve and update the progress percentage of total file uploaded till now
            percentageOfTotalSize = (int)(((decimal)sentBytes / (decimal)fileSize) * 100);
            this.SetProgressStatus(percentageOfTotalSize);
            bytesRead = fileToUpLoad.Read(buffer, LoadManagerConstants.Offset, chunkSize);
            iteration++;
        }

        //Method called to inform the server file has uploaded
        bytesWritten = this._workFlowProxy.UploadChunk(this._taskId, buffer, sentBytes, bytesRead);
        fileToUpLoad.Close();
    }

 public void EventLoad()
        {
            _cancelButton.Visible = true;
            _cancelButton.Enabled = true;       
        }


shabbir 16Nov2006 22:07

Re: How to enable a cancel button when an upload takes place.
 
Not the Invoke method but use the BegineInvoke method.

Leo 22Nov2006 14:27

Pls help me .Its urgent .I tried BeginInvoke still did not work.
 
Hi Shabbir,

I am still not able to have the focus on Cancelbutton so that i can click during an asynchronous process takes place.This is the code sample.

I am calling Test() method which will take some time to finish.During that time I wanna click on the cancel button which i am not able to do.
Code:

public delegate void AsyncFactorCaller();
AsyncFactorCaller factorDelegate;

 public void Test()
        {

            for (int i = 0; i <= 1000000000; i++)
            {

            }

         
        }
        public void CallBackTest(IAsyncResult result)
        {
            DisplayInfoDelegate del = new DisplayInfoDelegate(Tick1);
            AsyncFactorCaller factorDelegate = (AsyncFactorCaller)result.AsyncState;
            factorDelegate.EndInvoke(result);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            btnCancel.Visible = true;
            btnCancel.Focus();
            btnCancel.Refresh();

            factorDelegate = new AsyncFactorCaller(Test);
            IAsyncResult result = factorDelegate.BeginInvoke(new AsyncCallback(CallBackTest), null);
            while (!result.IsCompleted)
            {
            }
        }


shabbir 22Nov2006 15:14

Re: How to enable a cancel button when an upload takes place.
 
I am not sure why you are unable to make it work.

Just follow the steps
1. Add a new C# windows application project
2. Add couple of buttons and one text box to the forum
3. Add the following code to the event handlers of the buttons
Code:

private void button1_Click(object sender, EventArgs e)
{
    textBox1.Text = "Updated in the clicked event";
}

private void button2_Click(object sender, EventArgs e)
{
    x x1 = new x(test);
    x1.BeginInvoke(null, null);
}

public delegate void x();

public void test()
{
    for (int i = 0; i <= 1000000000; i++)
    {
    }

}

It should do the job for you.


All times are GMT +5.5. The time now is 15:46.