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.
Related
My goal is to make a desktop pet. I've already programmed a lot of logic that executes in a while loop and updates every iteration. To display the creature I'm looking to use windows forms, but that has brought up a dilemma.
I want to be able to execute the logic, and then update the window in the same loop (process events and redraw), without having to deal with Application.Run() or multi threading. As an example, and as someone who's come from python, using tkinter it's possible to call the update() method on a window in a loop, which is essentially the same as calling mainloop() once, except it doesn't block the program.
Do forms offer any similar functionality?
As Scott Chamberlain mentioned, you should use a timer to run your 'loop'. Winforms is event based so adding an infinite loop will freeze the program since events are blocked.
This code illustrates how to use a timer. I added a picture box to the form and it moves across the screen as the timer fires.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Timer tmr = new Timer();
tmr.Interval = 50; // milliseconds
tmr.Tick += Tmr_Tick; // set handler
tmr.Start();
}
private void Tmr_Tick(object sender, EventArgs e) //run this logic each timer tick
{
pictureBox1.Left += 1; // move image across screen, picture box is control so no repaint needed
}
}
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.
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
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why does Thread.sleep() behave in this way
This is a simple code that i have written:
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "first";
Thread.Sleep(1000);
label1.Text = "second";
}
When this code is executed and button is clicked, label1 displays text as only 'second' but not 'first'. I checked using break point, and statement label1.text="first" is executed but label does not display 'first'. Why is this so?
Because your code is running on the UI thread.
When your app is running and you're not clicking on things, the application's main thread is rushing around, waiting for events, processing Windows messages, and painting the screen. When you click your button, the app stops doing those things, and runs your function instead. While it's running your method, it can't paint the screen. Your function sets the label to "First", blocks the thread for a second, then sets the label to "second".
When you return, control passes back to the application, which is then free to repaint the form. It repaints it using the "second" label. The message loop never had chance to paint it with the "first" label.
You're running a single threaded application. Although the statement setting the label1 object's Text property to "first" has been executed, because you are causing the main application thread to pause, the "Windows Message Pump" is not being executed.
The problem is your a freezing the UI thread here.
The text "first" is set but then you put the main GUI thread to sleep - so the UI does not get painted.
After the sleep phase is over, the text "second" is set and the UI gets painted.
That's just the way Windows works, so you have to do that differently.
e.g. using Threads, or Backgroundworker
If you want delay use timers.
The reason why you don't see the result in UI is because Dipatcher's priorities. The Render task has lower priority than the execution.
This helps you: TIMERS!
DispatcherTimer dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 1);
dispatcherTimer.Start();
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
//your code goes here
}
If you work in WPF you can try this:
private void button1_Click(object sender, RoutedEventArgs e)
{
label1.Text = "first";
Dispatcher.Invoke(new Action(() => { }), DispatcherPriority.ContextIdle);
Thread.Sleep(1000);
label1.Text = "second";
}
I am working on a wpf application. Here I need to use System.Windows.Forms.FolderBrowserDialog in my Wpf application.
System.Windows.Forms.FolderBrowserDialog openFolderBrowser = new System.Windows.Forms.FolderBrowserDialog();
openFolderBrowser.Description = "Select Resource Path:";
openFolderBrowser.RootFolder = Environment.SpecialFolder.MyComputer;
if (openFolderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
//some logic
openFolderBrowser.Dispose();
}
I launch a FolderBrowserDialog, select a Folder and click OK, and then I launch another System.Windows.Forms.FolderBrowserDialog, My problem is when I select a Folder and click OK in this FolderBrowserDialog, the shadow of FolderBrowserDialog remains on the screen(means my screen doesn't refresh). I need to minimize or resize it in order to remove the shadow of FolderBrowserDialog. How can I solve this issue?
Any help plz?
Edit:
I found the solution. I called OnRender method on my wpf Window and it worked for me. It redraws everythign on the screen.
You can call InvalidateVisual method to refresh UI.
on a form code
Update();
refreshes screen and updates ui.
We are using winforms so Update() is a basic function that redraws the window content. so you may use it directly from a form. The basic usage could be a timer which updates a label on a screen. when timer ticks you update the label:
System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
t.Interval = 1000; // specify interval time as you want
t.Tick += new EventHandler(timer_Tick);
t.Start();
void timer_Tick(object sender, EventArgs e)
{
label1.text = DateTime.Now.ToString("h:mm:ss"));
Update(); //this will refresh the form and label's text is updated.
}
otherwise label1.text will never change.