I have included the coding for Threading in C#. I tried to convert the coding into vb.net but an error saying "Parameter count mismatch" displays. I don't know how to resolve the problem. Pls help me resolve the problem.
C# code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
using System.Timers;
namespace TimerSamples
{
public class frmMain : System.Windows.Forms.Form
{
private System.Windows.Forms.Timer tmrWindowsFormsTimer;
private System.Timers.Timer tmrTimersTimer;
private System.Threading.Timer tmrThreadingTimer;
private System.Windows.Forms.Button btnStart;
private System.Windows.Forms.GroupBox grpTimerClasses;
private System.Windows.Forms.Button btnStop;
private System.Windows.Forms.ListBox lstTimerEvents;
private System.Windows.Forms.RadioButton rbWindowsFormsTimer;
private System.Windows.Forms.RadioButton rbTimersTimer;
private System.Windows.Forms.RadioButton rbThreadingTimer;
private System.Windows.Forms.Button btnSleepThread;
private System.ComponentModel.Container components = null;
private int _tickEventCounter;
private delegate void ShowTimerEventFiredDelegate(DateTime eventTime, string threadName);
public frmMain()
{
InitializeComponent();
InitializeTimers();
}
private void InitializeTimers()
{
//Initialize System.Windows.Forms.Timer (instance members are not thread safe)...
tmrWindowsFormsTimer = new System.Windows.Forms.Timer();
tmrWindowsFormsTimer.Interval = 1000;
tmrWindowsFormsTimer.Tick += new EventHandler(tmrWindowsFormsTimer_Tick);
//Initialize System.Timers.Timer (this type is safe in multi-threaded apps)...
tmrTimersTimer = new System.Timers.Timer();
tmrTimersTimer.Interval = 1000;
tmrTimersTimer.Elapsed += new ElapsedEventHandler(tmrTimersTimer_Elapsed);
//If the Elapsed event handler has UI code,
//the SynchronizingObject property of the System.Timers.Timer class
//provides an alternative to using BeginInvoke.
//tmrTimersTimer.SynchronizingObject = this;
//Initialize System.Threading.Timer (instance members are not thread safe)...
tmrThreadingTimer = new System.Threading.Timer(new TimerCallback(tmrThreadingTimer_TimerCallback), null, System.Threading.Timeout.Infinite, 1000);
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmMain));
this.btnStart = new System.Windows.Forms.Button();
this.grpTimerClasses = new System.Windows.Forms.GroupBox();
this.rbThreadingTimer = new System.Windows.Forms.RadioButton();
this.rbTimersTimer = new System.Windows.Forms.RadioButton();
this.rbWindowsFormsTimer = new System.Windows.Forms.RadioButton();
this.lstTimerEvents = new System.Windows.Forms.ListBox();
this.btnStop = new System.Windows.Forms.Button();
this.btnSleepThread = new System.Windows.Forms.Button();
this.grpTimerClasses.SuspendLayout();
this.SuspendLayout();
//
// btnStart
//
this.btnStart.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
this.btnStart.Location = new System.Drawing.Point(238, 14);
this.btnStart.Name = "btnStart";
this.btnStart.Size = new System.Drawing.Size(110, 23);
this.btnStart.TabIndex = 1;
this.btnStart.Text = "Start";
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
//
// grpTimerClasses
//
this.grpTimerClasses.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.grpTimerClasses.Controls.AddRange(new System.Windows.Forms.Control[] {
this.rbThreadingTimer,
this.rbTimersTimer,
this.rbWindowsFormsTimer});
this.grpTimerClasses.Location = new System.Drawing.Point(7, 8);
this.grpTimerClasses.Name = "grpTimerClasses";
this.grpTimerClasses.Size = new System.Drawing.Size(212, 80);
this.grpTimerClasses.TabIndex = 0;
this.grpTimerClasses.TabStop = false;
this.grpTimerClasses.Text = "Timer Class";
//
// rbThreadingTimer
//
this.rbThreadingTimer.Location = new System.Drawing.Point(12, 56);
this.rbThreadingTimer.Name = "rbThreadingTimer";
this.rbThreadingTimer.Size = new System.Drawing.Size(160, 16);
this.rbThreadingTimer.TabIndex = 2;
this.rbThreadingTimer.Text = "System.Threading.Timer";
//
// rbTimersTimer
//
this.rbTimersTimer.Location = new System.Drawing.Point(12, 32);
this.rbTimersTimer.Name = "rbTimersTimer";
this.rbTimersTimer.Size = new System.Drawing.Size(136, 24);
this.rbTimersTimer.TabIndex = 1;
this.rbTimersTimer.Text = "System.Timers.Timer";
//
// rbWindowsFormsTimer
//
this.rbWindowsFormsTimer.Checked = true;
this.rbWindowsFormsTimer.Location = new System.Drawing.Point(12, 16);
this.rbWindowsFormsTimer.Name = "rbWindowsFormsTimer";
this.rbWindowsFormsTimer.Size = new System.Drawing.Size(180, 16);
this.rbWindowsFormsTimer.TabIndex = 0;
this.rbWindowsFormsTimer.TabStop = true;
this.rbWindowsFormsTimer.Text = "System.Windows.Forms.Timer";
//
// lstTimerEvents
//
this.lstTimerEvents.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.lstTimerEvents.Location = new System.Drawing.Point(8, 96);
this.lstTimerEvents.Name = "lstTimerEvents";
this.lstTimerEvents.Size = new System.Drawing.Size(345, 173);
this.lstTimerEvents.TabIndex = 4;
//
// btnStop
//
this.btnStop.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
this.btnStop.Enabled = false;
this.btnStop.Location = new System.Drawing.Point(238, 68);
this.btnStop.Name = "btnStop";
this.btnStop.Size = new System.Drawing.Size(110, 23);
this.btnStop.TabIndex = 3;
this.btnStop.Text = "Stop";
this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
//
// btnSleepThread
//
this.btnSleepThread.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
this.btnSleepThread.Enabled = false;
this.btnSleepThread.Location = new System.Drawing.Point(238, 41);
this.btnSleepThread.Name = "btnSleepThread";
this.btnSleepThread.Size = new System.Drawing.Size(110, 23);
this.btnSleepThread.TabIndex = 2;
this.btnSleepThread.Text = "Sleep";
this.btnSleepThread.Click += new System.EventHandler(this.btnSleepThread_Click);
//
// frmMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(360, 278);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.btnSleepThread,
this.lstTimerEvents,
this.grpTimerClasses,
this.btnStop,
this.btnStart});
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.MinimumSize = new System.Drawing.Size(344, 200);
this.Name = "frmMain";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "MSDN Magazine - Timer Classes Demo";
this.grpTimerClasses.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Thread.CurrentThread.Name = "UIThread";
Application.Run(new frmMain());
}
private void tmrWindowsFormsTimer_Tick(object sender, System.EventArgs e)
{
ShowTimerEventFired(DateTime.Now, GetThreadName());
}
private void tmrTimersTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//With this timer class, we could also use e.SignalTime instead of DateTime.Now.
//ShowTimerEventFired(e.SignalTime, GetThreadName());
ShowTimerEventFired(DateTime.Now, GetThreadName());
}
private void tmrThreadingTimer_TimerCallback(object state)
{
ShowTimerEventFired(DateTime.Now, GetThreadName());
}
private string GetThreadName()
{
if (Thread.CurrentThread.IsThreadPoolThread)
return "WorkerThread";
else
return Thread.CurrentThread.Name;
}
private void ShowTimerEventFired(DateTime eventTime, string threadName)
{
//InvokeRequired will be true when using System.Threading.Timer
//or System.Timers.Timer (without a SynchronizationObject)...
if (lstTimerEvents.InvokeRequired)
{
//Marshal this call back to the UI thread (via the form instance)...
BeginInvoke(new ShowTimerEventFiredDelegate(ShowTimerEventFired), new object[] {eventTime, threadName});
}
else
lstTimerEvents.TopIndex = lstTimerEvents.Items.Add(String.Format("--> Timer Event {0} @ {1} on Thread: {2}", ++_tickEventCounter, eventTime.ToLongTimeString(), threadName));
}
private void btnStart_Click(object sender, System.EventArgs e)
{
_tickEventCounter = 0;
if (rbWindowsFormsTimer.Checked)
tmrWindowsFormsTimer.Start(); //Same as: tmrWindowsFormsTimer.Enabled = true;
else if (rbTimersTimer.Checked)
tmrTimersTimer.Start(); //Same as: tmrTimersTimer.Enabled = true;
else // if (rbThreadingTimer.Checked)
tmrThreadingTimer.Change(0, 1000);
UpdateControls(true);
}
private void btnStop_Click(object sender, System.EventArgs e)
{
if (rbWindowsFormsTimer.Checked)
tmrWindowsFormsTimer.Stop(); //Same as: tmrWindowsFormsTimer.Enabled = false;
else if (rbTimersTimer.Checked)
tmrTimersTimer.Stop(); //Same as: tmrTimersTimer.Enabled = false;
else // if (rbThreadingTimer.Checked)
tmrThreadingTimer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
UpdateControls(false);
}
private void btnSleepThread_Click(object sender, System.EventArgs e)
{
lstTimerEvents.TopIndex = lstTimerEvents.Items.Add("Sleeping for 5000 ms...");
lstTimerEvents.Refresh();
Thread.Sleep(5000);
}
private void UpdateControls(bool start)
{
string timerType = "";
if (rbWindowsFormsTimer.Checked)
timerType = rbWindowsFormsTimer.Text;
else if (rbTimersTimer.Checked)
timerType = rbTimersTimer.Text;
else // if (rbThreadingTimer.Checked)
timerType = rbThreadingTimer.Text;
lstTimerEvents.TopIndex = lstTimerEvents.Items.Add(String.Format("{0} {1} @ {2}", timerType, start ? "Started": "Stopped", DateTime.Now.ToLongTimeString()));
grpTimerClasses.Enabled = !start;
btnStart.Enabled = !start;
btnStop.Enabled = start;
btnSleepThread.Enabled = start;
Refresh();
}
}
}
Urgent
Thanx in advance
harry