Run C# file automatically - c#

Hello all of you on stack overflow.
I have Asp.net web application which picks up image from a folder , resize them and dump in other folder. For this i have to click on a button to pick image an work on it very often.
Is there a way to make application run automatically by picking image from folder and working on it and then dump in other folder.
Setting can be like start application and run for five minutes and stop and then wait for 1 minute and then start and run again for 5 minute.
IMPORTANT thing is it should only stop after it has finished resizing and dumping the last image in interval.
this will save me lot of trouble because I have to click on button very frequently and disturb me .
I only want the approach and related LINKS .Lots of them . Please reply.

use a timer control to do this task
timer.Tick += new EventHandler(timer_Tick); // Everytime timer ticks, timer_Tick will be called
timer.Interval = (1000) * (1); // Timer will tick evert second
timer.Enabled = true; // Enable the timer
timer.Start();
void timer_Tick(object sender, EventArgs e)
{
do whatever you want
}

Related

How to stop a timer/change system UI OnSleep Xamarin Android

So I have an app that on a button press: starts a timer, cycles through one (++) piece of data and hides the start button and instead shows a stop and next button.
I have looked into messaging center and I thought it was fixing the problem (here's the link Xamarin.Forms how do I access a public function from another CS file?) but it didn't fix the problem completely.
If the app has the timer running (aka you hit the start button) and then interrupt the process by hitting the home button on your phone, the app works fine and the app hides the stop/next buttons and shows the start button and stops the timer. If you haven't started the process at all (aka you haven't hit the start button) and you hit the home button on your phone the app throws an exception error because what I'm changing with messaging center "doesn't need changing because it never changed". Is there a better way to handle this situation?
Can I use if/else statements in app state with messagingcenter?? I'm stuck.
App.xaml.cs
protected override void OnSleep()
{
// Handle when your app sleeps
Debug.WriteLine("~~~~~~~~~~~~~~OnSleep~~~~~~~~~~~~~");
MessagingCenter.Send<App>(this, "OnSleep");
}
MainPage.xaml.cs
MessagingCenter.Subscribe<App>(this, "OnSleep", (sender) => {
//shows start button instead of stop button
StartGrid.IsVisible = true;
//hides stop button
StopNextGrid.IsVisible = false;
//stops timer
timer.Stop();
timer = null;
//stops sound
startSound.Stop();
stopSound.Play();
});
Just can see the partial code,you should check if your timer is initialized before executing the method.
When you do not click the start button, you need to check whether the timer is initialized, in order to perform the following timer operation.
If no want to know whether timer is initialized. You can try this:
Modify in your notification handling method.If the state of your timer and button has not changed, you don't need to do anything in the notification.Here I use the timer as a judgment.
MessagingCenter.Subscribe<App>(this, "OnSleep", (sender) => {
//shows start button instead of stop button
if (null != timer)
{
StartGrid.IsVisible = true;
//hides stop button
StopNextGrid.IsVisible = false;
//stops timer
timer.Stop();
timer = null;
//stops sound
startSound.Stop();
stopSound.Play();
}
});

Update textbox during timer_tick

I have read many answers on this question, and yet I still cannot get this to work. I have a simple C# WinForms app with a timer control. When the timer fires, I have some code do some processing. I want to update a textbox with status during this processing. But the textbox never gets updated until the eventhandler finishes. Please tell me how I can get the textbox to update during the processing.
Here is my code:
My Form:
public Form1()
{
InitializeComponent();
timer1.Interval = 60000;
timer1.Tick += new EventHandler(CheckStatus);
timer1.Start();
}
private void CheckStatus(object Sender, EventArgs e)
{
// Set the caption to the current time.
textBox1.AppendText(DateTime.Now.ToString() + Environment.NewLine);
ProcessStatus();
}
private void ProcessStatus()
{
textBox1.AppendText("Now updated" + Environment.NewLine);
}
If I step through my code, the textbox is not updated until I step out of CheckStatus. (I'm using Visual Studio 2017)
I have tried several things like what is found here: StackOverflow
When the timer ticks it's firing on the GUI thread. While the GUI thread is busy processing (I assume whatever you're doing takes a long time) all other GUI updates will pause.
You can run textBox.Update() to force the update at that point, but that's not considered a best practice.
Instead, you should run your process on a background thread. One option is BackgroundWorker and use the ProgressChanged event to show your updates in your GUI.

Using DispatcherTimer

I'm creating a WP8 game with overlaying XMAL for the interface. I will be using multiple buttons. When the button is clicked, it will do something, and then "cooldown".
The cool down lasts for about 4 seconds and in that 4 seconds the button is not enabled. Every second the counter on the button will go down. Once it gets to 0 it will be enabled again.
I'm not sure how to update the buttons text per second to show the cooldown time. I was thinking of implementing the below
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0,0,1);
dispatcherTimer.Start();
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
// code goes here
}
Even on a PC, I don't think it would be a good idea to do this for something that could have up to nine buttons, so I definitely don't want to do it on a mobile device.
I'm fairly certain I can go around this using databinding but I just can't figure it out.
Thanks.

equal distribution event handler timer C#

I would like to fire my code based on a selected value from a drop down list of values.I want to also specify a time range (example below). The code I want to fire is behind button1_Click so id want to click that via code.
//Create the timer
Timer timer = new Timer();
//set it up
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = (1000 * 60 * 5);
timer.Enabled = true;
timer.Start();
//now do stuff
void timer_Tick(object sender, EventArgs e)
{
//click the button using code
}
Lets say I select 8 from the drop down list, that means I want to click my button 8 times, HOWEVER, the button needs to be clicked equally based on a time range.
So lets say I select:
From: 13:00
To: 15:00
that gives me 3 hours. I then want to divide the number that I have selected from the drop down, eg 8 by the number of hours/mins available.
In the example I expect the button to be clicked every 22.5mins.
On the SelectedValueChanged, reset the timer. Stop it, reset the interval and start it again. Inside the event handler, execute the click method.

Visual Studio 2008, Timer, c#

If i want my application to do something every 2hr (eg. pop up a message), how do i do that?
Do i program that set of code under onLoad() or somewhere else?
Assuming WinForms.
You should use Windows Timer Class
Drag and drop timer component to your form.
Set interval to 7200000 (2 * 60 * 60 * 1000) milliseconds.
Subscribe to Tick event (the only event that this component has).
private void timer1_Tick(object sender, EventArgs e)
{
MessageBox.Show("Example");
}
The code inside of timer will be triggered every 2 hours, if UI thread is not blocked.
Check the Timer Control and event Tick
Timer.Tick - MSDN
Use the Timer class and set it up when the application starts.

Categories

Resources