[Gtk-sharp-list] GLib.Timerout How to use?

True Friend true.friend2004 at gmail.com
Sun Sep 14 19:17:41 EDT 2008


Thanks a lot. Due to "beginnerness" I was unable to find such finer details.
Regards

On Sun, Sep 14, 2008 at 8:52 PM, Michael Hutchinson <
m.j.hutchinson at gmail.com> wrote:

> On Sun, Sep 14, 2008 at 4:37 AM, True Friend <true.friend2004 at gmail.com>
> wrote:
> > Thanks a lot. It is working fine now. I used this code on windows and was
> > trying to search something like it on mono as well.
> > ----------------------------------------------
> > void ToolStripMenuItemAutoSaveClick(object sender, EventArgs e)
> >         {
> >             ToolStripMenuItem item = (ToolStripMenuItem)sender;
> >             if(item.Checked == true)
> >             {
> >             timer = new Timer();
> >             timer.Interval = 6000;
> >             timer.Start();
> >             timer.Tick += AutoSaveEventHandler;
> >             }
> >             else
> >             {
> >                 timer.Stop();
> >             }
> >         }
> >         //auto save event handler
> >         void AutoSaveEventHandler(object sender, EventArgs e)
> >         {
> >             if(Form1.textFilePath != null)
> >             {
> >                 if(pattern.IsMatch(textFilePath) == true)
> >                 {
> >                     string f = File.ReadAllText(textFilePath);
> >                     string pathg = textFilePath + "~";
> >                     File.WriteAllText(pathg, f);
> >                     urduRichTextBox1.SaveFile(textFilePath,
> > RichTextBoxStreamType.RichText);
> >                 }
> >                 else{
> >                     string f = File.ReadAllText(textFilePath);
> >                     string pathg = textFilePath + "~";
> >                     File.WriteAllText(pathg, f);
> >                 urduRichTextBox1.SaveFile(textFilePath,
> > RichTextBoxStreamType.UnicodePlainText);
> >                 }
> >             }
> > ----------------------------------------------------
> > You can see there is no separate thread involved here,
> System.Timers.Timer
> > class is working fine. I tried to use this class but the event Timer.Tick
> > wasn't available and when I tried to use Timer.Elapsed event.
> > -----------------------------------------------------
> > if(Action4.Active == true)
> >             {
> >
> >             if(textFilePath == null)
> >             {
> >                 OnSaveActionActivated(sender, e);
> >             }
> >             timer = new Timer();
> >             timer.Interval = 300;
> >             timer.Start();
> >             timer.Elapsed += AutoSaveEventHandler;
> >             }
> >             else
> >             {
> >                 timer.Stop();
> >             }
> > ------------------------------------------
> > It was pretty much straight forward but I couldn't get it work on mono,
> it
> > also frozen the GUI. Can you tell me whats wrong with this code?
>
> System.Timers.Timer does use a thread internally, and the "Elapsed"
> callback is invoked on this other thread.
>
> See
> http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx#remarksToggle
> which says:
>
> " If you use the Timer with a user interface element, such as a form
> or control, assign the form or control that contains the Timer to the
> SynchronizingObject property, so that the event is marshaled to the
> user interface thread. "
>
> You have been lucky that it works on winforms... it's still quite
> possible that your code will hang, unless you add synchronisation.
>
>
> You can still use timers with GTK#, but you will have to use
> Gtk.Application.Invoke to run delegates on the GUI thread:
> timer.Elapsed += delegate {
>    Application.Invoke (delegate {
>        AutoSave()();
>    })
> });
>
> GLib.Timeouts are a much nicer (thread-less) model.
>
> --
> Michael Hutchinson
> http://mjhutchinson.com
>



-- 
Muhammad Shakir Aziz محمد شاکر عزیز
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/gtk-sharp-list/attachments/20080915/c7ff0af9/attachment-0001.html 


More information about the Gtk-sharp-list mailing list