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?
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.
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,
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; }
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) { } }
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.
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.
Is the problem you are facing relating to the thread then you can use the Control.CheckForIllegalCrossThreadCalls = false in the constructor of the form.
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