2002-08-17 19:53:51 +00:00
|
|
|
//
|
|
|
|
// ThreadNotify.cs: implements a notification for the thread running the Gtk main
|
|
|
|
// loop from another thread
|
|
|
|
//
|
2004-03-05 04:20:09 +00:00
|
|
|
// Authors:
|
2002-08-17 19:53:51 +00:00
|
|
|
// Miguel de Icaza (miguel@ximian.com).
|
2004-03-05 04:20:09 +00:00
|
|
|
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
|
2002-08-17 19:53:51 +00:00
|
|
|
//
|
|
|
|
// (C) 2002 Ximian, Inc.
|
2004-03-05 04:20:09 +00:00
|
|
|
// (C) 2004 Novell, Inc.
|
2002-08-17 19:53:51 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
namespace Gtk {
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
using System.Threading;
|
|
|
|
using System;
|
|
|
|
|
|
|
|
// <summary>
|
|
|
|
// This delegate will be invoked on the main Gtk thread.
|
|
|
|
// </summary>
|
2002-10-27 02:30:51 +00:00
|
|
|
public delegate void ReadyEvent ();
|
2002-08-17 19:53:51 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Utility class to help writting multi-threaded Gtk applications
|
|
|
|
/// </summary>
|
2003-08-30 00:26:38 +00:00
|
|
|
/// <remarks/>
|
2002-08-17 19:53:51 +00:00
|
|
|
///
|
2003-10-17 19:17:19 +00:00
|
|
|
public class ThreadNotify : IDisposable {
|
|
|
|
bool disposed;
|
2002-08-17 19:53:51 +00:00
|
|
|
ReadyEvent re;
|
2004-03-06 18:48:20 +00:00
|
|
|
GLib.IdleHandler idle;
|
|
|
|
bool notified;
|
2002-08-17 19:53:51 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The ReadyEvent delegate will be invoked on the current thread (which should
|
|
|
|
/// be the Gtk thread) when another thread wakes us up by calling WakeupMain
|
|
|
|
/// </summary>
|
|
|
|
public ThreadNotify (ReadyEvent re)
|
|
|
|
{
|
|
|
|
this.re = re;
|
2004-03-06 18:48:20 +00:00
|
|
|
idle = new GLib.IdleHandler (CallbackWrapper);
|
2002-08-17 19:53:51 +00:00
|
|
|
}
|
|
|
|
|
2004-03-06 18:48:20 +00:00
|
|
|
bool CallbackWrapper ()
|
2002-08-17 19:53:51 +00:00
|
|
|
{
|
2004-03-06 18:48:20 +00:00
|
|
|
lock (this) {
|
|
|
|
if (disposed)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
notified = false;
|
2002-08-17 19:53:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
re ();
|
2004-03-06 18:48:20 +00:00
|
|
|
return false;
|
2002-08-17 19:53:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Invoke this function from a thread to call the `ReadyEvent'
|
|
|
|
/// delegate provided in the constructor on the Main Gtk thread
|
|
|
|
/// </summary>
|
|
|
|
public void WakeupMain ()
|
|
|
|
{
|
2004-03-06 18:48:20 +00:00
|
|
|
lock (this){
|
|
|
|
if (notified)
|
|
|
|
return;
|
|
|
|
|
|
|
|
notified = true;
|
|
|
|
GLib.Idle.Add (idle);
|
2002-08-17 19:53:51 +00:00
|
|
|
}
|
|
|
|
}
|
2003-10-17 19:17:19 +00:00
|
|
|
|
|
|
|
public void Close ()
|
|
|
|
{
|
|
|
|
Dispose (true);
|
|
|
|
GC.SuppressFinalize (this);
|
|
|
|
}
|
|
|
|
|
|
|
|
~ThreadNotify ()
|
|
|
|
{
|
|
|
|
Dispose (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IDisposable.Dispose ()
|
|
|
|
{
|
|
|
|
Close ();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void Dispose (bool disposing)
|
|
|
|
{
|
2004-03-06 18:48:20 +00:00
|
|
|
lock (this) {
|
2003-10-17 19:17:19 +00:00
|
|
|
disposed = true;
|
|
|
|
}
|
|
|
|
}
|
2002-08-17 19:53:51 +00:00
|
|
|
}
|
|
|
|
}
|
2004-03-05 04:20:09 +00:00
|
|
|
|