where to add eventhandler - c#

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.

Related

notifyIcon.BalloonTipClicked firing when tray icon is clicked

I have a tray app backed by a NotifyIcon. It sometimes fires toast notifications via notifyIcon.ShowBallonTip(). It is running on Windows 10 1607.
I'm subscribed to the following two events:
notifyIcon.MouseUp += notifyIcon_MouseUp;
notifyIcon.BalloonTipClicked += notifyIcon_BalloonTipClicked;
At first glance everything works great:
When I click on the tray icon, I get the MouseUp event and the correct things happen
When I click on the toast, I get the BalloonTipClicked event and the correct things happen
The bug in the app right now is that if the user clicks on the tray icon while the toast is showing, it acts as if the user clicked on the toast instead. In other words, clicking on the tray icon while the balloon/toast is showing will fire BallonTipClicked even though the balloon/toast was never actually clicked.
When I am in BalloonTipClicked the "sender" is just the NotifyIcon. I don't see any way of distinguishing "balloon tip clicked" vs "tray icon clicked". I mean, the event itself is called BallonTipClicked so I have no idea why it fires when I click the app icon.
According to MSDN this should not be happening:
BalloonTipClicked: Occurs when the balloon tip is clicked.
MouseUp: Occurs when the user releases the mouse button while the pointer is over the icon in the notification area of the taskbar.
Are the docs wrong? Any ideas for getting the behavior I intend?

Asp.net C#, monitor a c# function that checks the status from sql DB?

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.

Stop button click event on other button click

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

Button click event

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.

WPF: Is there an event that is fired when the user presses the close button [X]?

Is there an event that is fired when the user presses the close button?
This is because the Window Closing event is fired both when one closes the window manually (with the Close method) and also when the user presses the [X] button...but I somehow need to know only when the user presses the [X] button not when the window is closed manually.
I don't believe there is a way to tell those apart in WPF (though I'm not positive).
The way I always handled it in WinForms was to create a member variable "_Closing", set it to false, and a method "ReallyClose()" that would set _Closing to true, then call Close. My Closing handler would then cancel the close if _Closing was not set to true.
Yeah, it's a bit of a hack, but it worked.
I also don't think there is a way to tell them apart. You can put a handler on the Application.Exit event, but it doesn't distinguish between "red X button close" and "alt-F4 close" (or whatever other types of close you are considering).
BTW, if you check for Application.Exit, be sure to check for Application.SessionEnding too - if someone logs off while your app is running, you can't be guaranteed that Application.Exit will be called.
Try to put button with name Cancel and bool variable in your class so When you click on button set it to the true and in Closing Event check if is true use e.Cancel=false to exit window I tried everything and It doesn't work for me and I do on this way and also you can remove X button just to have Accept ot Cancel buttons if you insert some informations.

Categories

Resources