How to enable a cancel button when an upload takes place.

Leo's Avatar, Join Date: Nov 2006
Leo
Go4Expert Member
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's Avatar, Join Date: Jul 2004
Go4Expert Founder
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's Avatar, Join Date: Apr 2005
Team Leader
Is there anythinhg like VB's DoEvents in C#?? If so, it can help.
Leo's Avatar, Join Date: Nov 2006
Leo
Go4Expert Member
Thanks Shabbir for moving my query in to Queries and Discussion forum.
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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's Avatar, Join Date: Apr 2005
Team Leader
That's pretty good enough, that'll do the trick here.
Leo's Avatar, Join Date: Nov 2006
Leo
Go4Expert Member
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;         
        }

Last edited by shabbir; 16Nov2006 at 21:58.. Reason: Code formating.
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Not the Invoke method but use the BegineInvoke method.
Leo's Avatar, Join Date: Nov 2006
Leo
Go4Expert Member
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)
            {
            }
        }

Last edited by shabbir; 22Nov2006 at 14:47.. Reason: Code formating.
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.