How to delay Silverlight's loading screen? - c#

I have an experimental project in silverlight, that has no database and scarce resources. Now, I wanted to know if you can prolong or delay the Silverlight loading screen, so I can check what I have modified in the loading page. Problem is, it loads too fast for me to check. I have no data to fetch from the webservice or any resources needed. I'm just experimenting in modifying Silverlight's load page. Can this be done code-wise? Any help would be appreciated. Thanks.

Already found the answer. I just needed a timer for things. thanks for all the queries, anyway
private void Application_Startup(object sender, StartupEventArgs e)
{
var timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(10);
EventHandler eh = null;
eh = (s, args) =>
{
timer.Stop();
this.RootVisual = new Test();
timer.Tick -= eh;
};
timer.Tick += eh;
timer.Start();
}

Related

Windows Service Timer Not Firing

I have a windows service whose timer doesn't want to fire. I have an attached debugger but it gave me no useful information. The odd thing is; that this service is nearly identical to another service I built which works as intended.
The code in question is this:
public Constructor()
{
_timer.Interval += 300000;
InitializeComponent();
_timer.Enabled = true;
//_timer.Start(); <this is not actually here. just one of the things i tried
_timer.Elapsed += new ElapsedEventHandler(timer_Tick);
}
After which I have:
private void timer_Tick(object sender, EventArgs e)
{
DoWorkHere();
}
I dont understand why attaching the "ElapsedEventHandler" doesn't fire the timer_Tick method.
Edit: "_timer" is:
System.Timers.Timer _timer = new System.Timers.Timer();
Problem was with permissions.
Sorry for wasting anyone's time.

How to use a timer to wait?

I am trying to delay events in my method by using a timer, however i do not necessarily understand how to use a timer to wait.
I set up my timer to be 2 seconds, but when i run this code the last call runs without a 2 second delay.
Timer timer = new Timer();
timer.Tick += new EventHandler(timer_Tick); // Everytime timer ticks, timer_Tick will be called
timer.Interval = (1000) * (2); // Timer will tick evert second
timer.Enabled = true; // Enable the timer
void timer_Tick(object sender, EventArgs e)
{
timer.Stop();
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "first";
timer.Start();
label1.Text = "second";
}
So when i click my button, it immediately shows label1 as "second", as opposed to changing to "first", waiting 2 seconds, then changing to "second". I have read lots of threads here about using timers instead of thread.sleep, but i cannot seem to find/figure out how to actually implement that.
If you're using C# 5.0 await makes this much easier:
private async void button1_Click(object sender, EventArgs e)
{
label1.Text = "first";
await Task.Delay(2000);
label1.Text = "second";
}
timer.Start() just starts the timer but immediately returns while the timer is running in the background. So between setting the label text to first and to second there is nearly no pause. What you want to do is wait for the timer to tick and only then update the label again:
void timer_Tick(object sender, EventArgs e)
{
timer.Stop();
label1.Text = "second";
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "first";
timer.Start();
}
Btw. you should not set timer.Enabled to true, you are already starting the timer using timer.Start().
As mentioned in the comments, you could put the timer creation into a method, like this (note: this is untested):
public void Delayed(int delay, Action action)
{
Timer timer = new Timer();
timer.Interval = delay;
timer.Tick += (s, e) => {
action();
timer.Stop();
};
timer.Start();
}
And then you could just use it like this:
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "first";
Delayed(2000, () => label1.Text = "second");
}
Tergiver’s follow-up
Does using Delayed contain a memory leak (reference leak)?
Subscribing to an event always creates a two-way reference.
In this case timer.Tick gets a reference to an anonymous function (lambda). That function lifts a local variable timer, though it's a reference, not a value, and contains a reference to the passed in Action delegate. That delegate is going to contain a reference to label1, an instance member of the Form. So is there a circular reference from the Timer to the Form?
I don't know the answer, I'm finding it a bit difficult to reason about. Because I don't know, I would remove the use of the lambda in Delayed, making it a proper method and having it, in addition to stopping the timer (which is the sender parameter of the method), also remove the event.
Usually lambdas do not cause problems for the garbage collection. In this case, the timer instance only exists locally and the reference in the lambda does not prevent the garbage collection to collect the instances (see also this question).
I actually tested this again using the .NET Memory Profiler. The timer objects were collected just fine, and no leaking happened. The profiler did give me a warning that there are instances that “[…] have been garbage collected without being properly disposed” though. Removing the event handler in itself (by keeping a reference to it) did not fix that though. Changing the captured timer reference to (Timer)s did not change that either.
What did help—obviously—was to call a timer.Dispose() in the event handler after stopping the timer, but I’d argue if that is actually necessary. I don’t think the profiler warning/note is that critical.
If all you're trying to do is change the text when the timer ticks, would you not be better off putting...
label1.Text = "second";
...In the timer tick, either before or after you change the timer to enabled = false;
Like so;
void timer_Tick(object sender, EventArgs e)
{
timer.Stop();
label1.Text = "second";
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "first";
timer.Start();
}
private bool Delay(int millisecond)
{
Stopwatch sw = new Stopwatch();
sw.Start();
bool flag = false;
while (!flag)
{
if (sw.ElapsedMilliseconds > millisecond)
{
flag = true;
}
}
sw.Stop();
return true;
}
bool del = Delay(1000);

Stopping DispatcherTimer in its own anonymous Tick event handler

Is it safe to do something like this:
private void MyFunction()
{
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 1);
timer.Tick += (object sender, object e) =>
{
timer.Stop();
// Some code here
};
timer.Start();
}
Matt raise the point that the way you attach the anonymous method that there is no easy way to detach it. Here is a general pattern you can use to enable you to detach if necessary.
private void MyFunction()
{
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 1);
EventHandler eh = null;
eh = (object sender, object e) =>
{
timer.Tick -= eh;
timer.Stop();
// Some code here
};
timer.Tick += eh;
timer.Start();
}
However in this specific case there is nothing wrong with the way your original code works since the timer becomes collectable as soon as it is stopped.
Yes. Your timer will fire once.
Edit: I'll rephrase my answer based on the comments. In the situation you've given, yes it's perfectly safe to use an anonymous delegate.
There are some situations in which adding an anonymous delegate and not detaching it could prevent your class from being garbage collected (for example, attaching an anonymous delegate to a singleton). See this answer for information about when it is and isn't necessary to detach the event handler.

Is there a QTimer::singleShot equivalent in C# Windows Forms?

Is there an analog to the following static function from the Qt library in Windows Forms?
http://doc.qt.io/qt-5/qtimer.html#singleShot
The best I can come up with is the following:
ThreadPool.QueueUserWorkItem((o) =>
{
Thread.Sleep(someNumberOfMilliseconds);
DoDelayedWorkHere();
});
UPDATE
This does the trick using System.Windows.Forms.Timer.
var timer = new System.Windows.Forms.Timer();
timer.Interval = someNumberOfMilliseconds;
timer.Tick += (o, args) =>
{
timer.Stop();
DoDelayedWorkHere();
};
timer.Start();
QTimer is a synchronous timer, just like the Winforms Timer. Threading or one of the other Timer classes is not a substitute. A single-shot is easy to implement, just set the timer's Enabled property to false in the Tick event handler. No danger of a race:
private void timer1_Tick(object sender, EventArgs e) {
((Timer)sender).Enabled = false;
// etc..
}
How about System.Threading.Timer? Use one of the constructors with the period parameter and specify the parameter accordingly.

Timer, event and garbage collection : am I missing something?

Consider the following code :
class TestTimerGC : Form
{
public TestTimerGC()
{
Button btnGC = new Button();
btnGC.Text = "GC";
btnGC.Click += (sender, e) => GC.Collect();
this.Controls.Add(btnGC);
System.Windows.Forms.Timer tmr = new System.Windows.Forms.Timer();
tmr.Interval = 1000;
tmr.Tick += (sender, e) => this.Text = DateTime.Now.ToString();
tmr.Start();
}
}
If I'm not mistaken, after the tmr variable goes out of scope, the Timer isn't referenced anywhere, so it should be eligible for garbage collection. But when I click the GC button, the timer continues to run, so I guess it wasn't collected...
Does anyone have an explanation for that ?
PS: it's not a real program of course, I was just trying to prove a point to someone... but my proof didn't work ;)
OK, I think I know what's going on... I looked at the code of the Timer class with Reflector, and I found the following instruction in the setter of the Enabled property :
this.timerRoot = GCHandle.Alloc(this);
So, when it is started, the timer allocates a GCHandle for itself, which prevents its collection by the GC...

Categories

Resources