Button click event - c#

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.

Related

C# adding infinity loop in button click event and break it with another button click event

I tried to make a while (work){....}
inside button event called start
to do some stuff like label changing in the same form with random values
and I have another button called stop I want to make the work=false; so the while should be break
the problem is when I clicked start button and the form froze and did nothing
how I can do that like stay in the while loop and access all other events
As long as the loop runs, no events (like your other button's click) can be processed. This results in freezing the UI.
Better use a Timer instead of an infinite loop. The timer will not freeze the UI but call a Tick event at defined intervals and allow other events to be processed between two ticks. It is then easy for another button to stop this timer.
Since you have not mentioned any UI technology (is it WinForms, WPF, WebForms, MAUI, Xamarin, ...?) and not shown any code, it is difficult to give you example code.

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

No Load Complete event for windows form

In windows form on click on Next I need to display other form and start some processing. Am coding in .Net C#
The problem is before the form is complete visible, the method gets triggered and the processing starts and the UI looks like it crashed. Processing started even before controls are loaded. and once processing is done all controls are visible.
The actual output must be all controls should be loaded and then processing must start.
I need to call the method to start processing after the form (user control) is visible and is loaded completely.
I tried this and this, but no luck.
Added code:
private void FeatureRemovalControl_Load(object sender, EventArgs e)
{
pictureBox2.Image = Properties.Resources.line;
prgbar.Value = 0;
//code to load images and some other stuff
StratProcess();
}
You're calling StartProcess (which seems to block until it's finished) from your UI thread. Because WinForms repaints occur on that thread, nothing is painted, and it appears that your process has hung. You should look at using a BackgroundWorker, or other way to call StartProcess asynchronously.
Best way, if you ask me, would be to start processing asynchronously, so that you maintain full control of the UI and process at the same time.
http://msdn.microsoft.com/en-us/library/2e08f6yc(v=vs.71).aspx
Try calling that method at the end of the FormLoad event, the control should have finished loading by then. If it hasn't that you may need perform some checks and possibly create a custom event that fires when you're happy that it is ready.
Another solution is to have a button that the user must press to trigger the processing, which they will only be able to click once everything has loaded
EDIT: The reason it look's like it's happening is because you're starting the process in one of the control's load method, which I assume is not the last control to load, so it's starts processing before the other controls are given a chance to load. Make StratProcess method public and call it in the FormLoad method of the parent form instead, like so:
private void ParentForm_Load(object sender, EventArgs e)
{
FeatureRemovalControl.StratProcess(); // Should it be called StartProcess instead?
}
Beware though this is still doing the processing on the UI thread, so the screen may appear to 'hang' whilst this is happening so I advise you move it to a background thread as others have suggested.

Do firing events in C# block the current thread execution?

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.

Categories

Resources