471,357 Members | 1,086 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,357 software developers and data experts.

Demo Mode

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?
Jul 21 '05 #1
4 1810
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);
}
}
}
--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"VB User" <VB****@discussions.microsoft.com> wrote in message news:68**********************************@microsof t.com...
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?

Jul 21 '05 #2
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.

--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"VB User" <VB****@discussions.microsoft.com> wrote in message news:68**********************************@microsof t.com...
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?

Jul 21 '05 #3
Can you translate this code for VB.NET?
"Dave" wrote:
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.

--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"VB User" <VB****@discussions.microsoft.com> wrote in message news:68**********************************@microsof t.com...
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?


Jul 21 '05 #4
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.

--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"VB User" <VB****@discussions.microsoft.com> wrote in message news:D2**********************************@microsof t.com...
Can you translate this code for VB.NET?
"Dave" wrote:
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.

--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"VB User" <VB****@discussions.microsoft.com> wrote in message news:68**********************************@microsof t.com...
>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?


Jul 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by C GIllespie | last post: by
1 post views Thread by Ken Dere | last post: by
2 posts views Thread by Bryan | last post: by
1 post views Thread by John Kinson | last post: by
7 posts views Thread by Ernie | last post: by
1 post views Thread by Sean | last post: by
1 post views Thread by www.web20developers.com | last post: by
5 posts views Thread by Andrew P | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.