how to access two button click method if first button doing work when we click second button?
I tried to access other button click event when first button click event is performing operation and I want to stop this execution and perform another operation
You are going to need to do some reading about asynchronous programming and then implement Cooperative Task Cancellation.
When you press the second button:
check that a task is in progress
if an operation is in progress, cancel the ongoing task and wait for it to 'complete' (cancel successfully)
run your new operation
Related
I have a "Refresh" button in update panel, onclick it fires an event which calls and starts a scheduler. Scheduler runs for some time, I need to show "In progress.." label instead of Refresh Button while the scheduler is running, once it completes the Button should be visible again.
I have a checkstatus() function which checks for status(select status...from table) in Database's Table if it completes or not. How do I monitor/call the checkstatus() continuously in order to show "In progress.." and once it finds the required status then show the button again ?
Please give me approach on this, I'll try to make it work.
I used a delegate and called that function recursively till the job is finished.
I have just passed one well known test and I am curious to know answer to the question:
"Scenario: You are writing a form that has a "Start Task" button and a "Task Info" button. The "Task Info" button starts disabled, but whenever the "Start Task" button is clicked, the "Task Info" is enabled and an event handler is added to it that sends a status report to the printer. Once the task completes, the "Task Info" button is disabled again until "Start Task" is clicked once more. A task may take many minutes to complete, and sometimes status reports need to be printed both near the beginning and near the end of the task.
This functions perfectly for the first task but you find that for subsequent tasks, extra status reports are sent to the printer, wasting paper and effort sorting through them.
Based on the scenario above, how do you solve the problem?
Choices:
Disable the "Task Info" button within the "Task Info" button click handler.
Disable the "Start Task" button within the "Task Info" button click handler, then re-enable it when the task finishes.
Implement the IDisposable interface for the "Start Task" button and the "Task Info" button.
Increment a counter within the "Task Info" button click handler, and have the rest of the code only execute when the counter's value is zero.
Add the event handler for the "Task Info" button in the form's constructor rather than "Start Task" button's handler
I personally prefer answer # 5. But it is intersting to know the community point of view.
Yes, the correct solution is number 5.
I am creating C#.net(4.0) application.This application is a Console Application and I am creating a Window Form in this application with two button start and cancel.
From the main Function I am calling the form :
Application.Run(new Form());
Now the form contains two buttons :
Start Button-On start button click I am doing some task.
Task1();
Task2();
Cancel Button-On Cancel button click I want to terminate application.
Application.Exit() //is called on Cancel Button click.
The problem is as I start the application and click cancel button application terminates, but when I press start button and then press cancel button the application doesn't terminate..
If I'm firing the event:
var handler = OnMyEvent;
if (handler != null)
{
handler(some_info);
}
then will the execution thread wait until all suscriber methods return to continue the execution after line:
handler(some_info);
?
Or events are fired "in another thread", meaning that it automatically goes to the next line after handler(some_info)?
Events are fired on the same thread and it will block until they are completed. Of course the event handling code itself can spawn another thread and return immediately but this is completely different matter.
Also note that events like button clicks in a desktop applications like Windows Forms apps are put on a message queue and will fire one at a time. i.e. if you press a button and then press another button the second button event will not fire until the first is completed. Also the form will not repaint and will be "not responding" because painting the form is also an event.
Events are fired in the thread that raised them.
I have a button in my program that supposed to be clicked after a while loop finished, whats the code to click the button?
To programatically click a button just call the Click method:
button.Click();
Note that this doesn't cause the UI to update as if the button had been pressed - it just results in the event handler for the click event being run.
In your question you mention that you are running a while loop that presumably takes some time. If you do this in the naive way - running it in the main application thread - it will cause the UI to block while the loop is running. To fix this you need to run the while loop in another thread, for example by using a BackgroundWorker. But then when your loop finishes you have to be careful to ensure that the click event is called back on the main thread. The general way to do this is to use Invoke, but in the specific case that you have a BackgroundWorker you can run the code after the loop finishes in the OnRunWorkerCompleted event handler then you don't need to call Invoke yourself as the BackgroundWorker takes care of this for you.