Showing posts with label Multi Threading. Show all posts
Showing posts with label Multi Threading. Show all posts

Sunday, 1 November 2009

Creating simple WPF Multi-Thread application.

This videos shows how to create simple wpf -thread application,gives an overview of how to use "Dispatcher" object in WPF.




Below extension method will allow us to update any controls in thread safe manner.

public static class ControlExtension
{
public static void UpdateControlSafe(this Control control, Action code)
{
if (!control.Dispatcher.CheckAccess())
control.Dispatcher.BeginInvoke(code);
else
code.Invoke();
}
}

Monday, 29 June 2009

Generic InvokeRequired pattern - (C# Multi-Threading)

Tody I read article on InokeRequired patterns posted in code project ( Read Full Article )

I have wrote more generic version of the same ( Less code !!!!! ...clear & crisp...)

public static class ControlExtension
{
public static void UpdateControl(this Control control, MethodInvoker code)
{
if (control.InvokeRequired) {
control.Invoke(code);
}
else{
code.Invoke();
}
}

using 'Control' type param in Extension mentod give us an advantage to call UpdateControl on any control.

for eg:- In the below example I have called 'UpdateControl' extension method on textbox control.

private void btnUpdate_Click(object sender, EventArgs e)
{
Thread th = new Thread(new ThreadStart( () => txtInfo.UpdateControl(Display) ) );
th.Start();
}

private void Display()
{
txtInfo.Text += "Updated";
}

Thursday, 9 April 2009

Multi-Threading (.Net) :- System.Timer.Time class

Dotnet provides a server-based timer ( and is different from System.Windows.Forms.Timer and System.Threading.Timer ), allow us to specify a recurring interval at which an event will be raised in our application(or Easy way of executing a task at specified interval).


Below example (c# console application) shows how to create a Timer ,execute a task at specified interval and how to stop Timer gracefully.

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Timers;

namespace BackgroundThread_Terminate
{

class Program
{
static void Main(string[] args)
{
using (TimerClass_example1 timerSample = new TimerClass_example1())
{

Console.ReadLine();
}

Console.ReadLine();
}
}


public class TimerClass_example1:IDisposable
{
System.Timers.Timer timer;

long _stopTimer = 0; // 0 for start , 1 for stop

ManualResetEvent manualReset = new ManualResetEvent(false);

public TimerClass_example1()
{
timer = new System.Timers.Timer(1000);
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.Start();
}

//call back function excuted on a threadpool thread.
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
//always use try catch in callback
try
{
//gracefully stop timer
if(Interlocked.Read(ref _stopTimer) == 1 )
{
timer.Stop();
//signal the thread which is waiting.
manualReset.Set();

return;
}

Console.WriteLine(string.Format("Executed callback at {0}. Threadpool Thread ID:- {1}", DateTime.Now.ToString(), Thread.CurrentThread.ManagedThreadId));

//Note #1:- Uncomment below the and note the ThreadID's printed
//Thread.Sleep(2000); //[ Reason for printing different Thread is :- The Elapsed event is raised on a ThreadPool thread. If processing of the Elapsed event lasts longer than Interval, the event might be raised again on another ThreadPool thread. Thus, the event handler should be reentrant.]
}
catch (Exception ex)
{
//log exception and you can stop excuting Timer.
}
}


public void Dispose()
{
StopAndDisposeTimer();
GC.SuppressFinalize(this);

Console.WriteLine("Timer has been disposed");
}

private void StopAndDisposeTimer()
{
Interlocked.Exchange(ref _stopTimer, 1);

//wait 5 secs for signal
if (!manualReset.WaitOne(5000, false))
{
Console.WriteLine("Timer is not stopped gracefully.");
}

timer.Dispose();
}

~TimerClass_example1()
{
Dispose();
}
}
}