Demo Mode

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to show few forms in sequential order, one after another with an
interval of approx 10 sec. What would be the best way out using Timers?
 
By specifying 10 seconds as criteria, you are specifying the need for a timer. Why isn't a timer viable?

I must admit that this seems kinda silly, but here it goes:

using System;
using System.Windows.Forms;
using System.Threading;

public class FormSlideShow
{
private int complete = 0;

/// <param name="timeout">Number of milliseconds each form is shown.</param>
public void ShowForms(int timeout, params Form[] forms)
{
foreach (Form form in forms)
{
if (form.InvokeRequired)
form.Invoke(new MethodInvoker(form.Show));
else
form.Show();

// This should NOT be a System.Windows.Forms.Timer
// The callback is on a ThreadPool thread. Do not use the forms to call any Begin* inovcations or
// run any more Timer classes. (to prevent dead-locks)
using (System.Threading.Timer timer = new System.Threading.Timer(
new TimerCallback(HideForm), form, timeout, Timeout.Infinite))
{
while (complete == 0)
Application.DoEvents();

System.Threading.Interlocked.Exchange(ref complete, 0);
}
}
}


private void HideForm(object state)
{
Form form = state as Form;

try
{
if (form.InvokeRequired)
form.Invoke(new MethodInvoker(form.Close));
else
form.Close();
}
finally
{
System.Threading.Interlocked.Exchange(ref complete, 1);
}
}
}
 
sorry, i misunderstood when u said "out using Timers" as "without using Times" :)

anyway, the code i posted will work, but may not be the most elegant solution. If performance is not an issue, this will work fine.
 
Can you translate this code for VB.NET?


Dave said:
sorry, i misunderstood when u said "out using Timers" as "without using Times" :)

anyway, the code i posted will work, but may not be the most elegant solution. If performance is not an issue, this will work fine.
 
Probably not correctly :)

I can tell you that the MethodInvoker delegate is in the System.Windows.Forms namespace, and that the TimerCallback delegate is in
the System.Threading namespace.

I'm not sure how you would invoke them using VB.NET, but I imagine it must be similar.

I'm pretty sure that VB.NET doesn't have a "using" block as the one I've used here:

using (System.Threading.Timer timer = new System.Threading.Timer(
new TimerCallback(HideForm), form, timeout, Timeout.Infinite))
{
while (complete == 0)
Application.DoEvents();

System.Threading.Interlocked.Exchange(ref complete, 0);
}

so, instead... just do this (in VB, of course):

Dim timer As System.Threading.Timer

Try
timer = new System.Threading.Timer(new TimerCallback(HideForm), form, timeout, Timeout.Infinite))

while (complete = 0)
Application.DoEvents()
end while ??

System.Threading.Interlocked.Exchange(ByRef complete, 0)
Finally ??
timer.Dispose()
End Try ??


The rest of the conversions are probably intuitive, i.e. delete ";"'s at the end of each line, change the way you dimension
variables, replace { } blocks with "name"..."End name", etc.
 
Back
Top