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

Discussion in 'C#' started by Leo, Nov 14, 2006.

  1. Leo

    Leo New Member

    Joined:
    Nov 10, 2006
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    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?
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  3. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Is there anythinhg like VB's DoEvents in C#?? If so, it can help.
     
  4. Leo

    Leo New Member

    Joined:
    Nov 10, 2006
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    Thanks Shabbir for moving my query in to Queries and Discussion forum.
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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,
     
  6. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    That's pretty good enough, that'll do the trick here.
     
  7. Leo

    Leo New Member

    Joined:
    Nov 10, 2006
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    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 a moderator: Nov 16, 2006
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  9. Leo

    Leo New Member

    Joined:
    Nov 10, 2006
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    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)
                {
                }
            }
     
    Last edited by a moderator: Nov 22, 2006
  10. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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.
     
  11. Leo

    Leo New Member

    Joined:
    Nov 10, 2006
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    The problem comes when i have to make the button1 visible on the click of button2 and then make it invisible when the process(method Test()) gets over in the code.
     
  12. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Is the problem you are facing relating to the thread then you can use the Control.CheckForIllegalCrossThreadCalls = false in the constructor of the form.
     
  13. Leo

    Leo New Member

    Joined:
    Nov 10, 2006
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    I have found the solution to this which is working fine.I am using Background worker component to do this.

    I had the problem that once the code in Dowork is completed i had the put the code of enabling the timer to execute some code in the timer which did not work at all.It may execute once or does not execute at all.

    Right now i am using Dowork() method where i put the upload process.And then i have a timer which will monitor this upload process and then once the upload process is over ,i have a 2nd timer which will do the rest of the process as usual before.

    Thanks Shabbir for taking time to reply to my question
     
  14. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    My pleasure.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice