[Gtk-sharp-list] GTK# app "hanging"
Raúl Moratalla
raul.moratalla@ono.com
Wed, 23 Feb 2005 12:58:16 +0100
Thanks again for the infor Jon :) but I have another question.
My application uses Glade# and I initialize the widgets in the constructor of
my class:
txtServerLog = (gxml.GetWidget("txtServerLog") as TextView);
buffer = txtServerLog.Buffer;
This was working fine.
In the constructor I also add the following line:
krnGateway.OnLogMessage+= new LogEvent(OnLog);
And this are other functions that will update a widget:
private void OnLog(Constants.Log importance,string strMsg)
{
Gtk.Timeout.Add(0,delegate{m_Log(this,importance,strMsg); return false;});
}
private void m_Log(object sender,Constants.Log importance,string strMsg)
{
if (strMsg.Length==0) return;
string newline=DateTime.Now.ToShortTimeString()+" "+strMsg+"\n";
buffer.Text = buffer.Text + newline;
}
The objects buffer and txtServerLog are initialized but when my application
starts it crashes whith the following error:
Unhandled Exception: System.NullReferenceException: Object reference not set
to an instance of an object
in [0x0003b] (at /home/raul/lphantGTK/Servers.cs:170)
eLePhantGTK.TServers:m_Log (object,eLePhant.Types.Constants/Log,string)
in <0x00019> <>AnonHelp<0>:<#AnonymousMethod>0 ()
in <0x0003b> (wrapper delegate-invoke) System.MulticastDelegate:invoke_bool ()
in <0x0000e> GtkSharp.FunctionWrapper:NativeCallback (intptr)
in <0x00029> (wrapper native-to-managed)
GtkSharp.FunctionWrapper:NativeCallback (intptr)
in (unmanaged) 0x40080ca5
in <0x00004> (wrapper managed-to-native) Gtk.Application:gtk_main ()
in <0x00007> Gtk.Application:Run ()
in [0x00095] (at /home/raul/lphantGTK/Main.cs:161)
eLePhantGTK.eLePhantGTK:.ctor (string[])
in [0x00001] (at /home/raul/lphantGTK/Main.cs:138)
eLePhantGTK.eLePhantGTK:Main (string[])
It is due to the line: buffer.Text = buffer.Text + newline;
If I initalized the buffer object why does this happen?
El Martes, 22 de Febrero de 2005 23:58, Jonathan Pryor escribió:
> On Tue, 2005-02-22 at 23:16 +0100, Raúl Moratalla wrote:
> > Thanks for your info Jon but I have the following question.
> > I added a line like this in my program:
> >
> > Gtk.Timeout.Add(0,delegate{m_Log(this,importance,strMsg);});
> >
> > If I compile my program I get the following error:
> > Not all code paths return a value in anonymous method of type
> > `Mono.CSharp.AnonymousMethod'(CS1643)
> >
> > What am I doing wrong?
>
> Let's follow the types... Gtk.Timeout.Add is declared as:
>
> public delegate bool Gtk.Function ();
>
> public class Gtk.Timeout {
> public static uint Add (uint interval, Function function);
> }
>
> Basically, the delegate passed to Gtk.Timeout.Add is expected to return
> a `bool'. Your delegate doesn't, resulting in the compiler error.
>
> To fix this, return a value from your anonymous delegate (I return
> `false' because the return value specifies whether or not the delegate
> should be called again, and I assume you don't want it called again):
>
> Gtk.Timeout.Add (0, delegate {
> m_Log (this, importance, strMsg);
> return false;
> });
>
> - Jon
>
>
> _______________________________________________
> Gtk-sharp-list maillist - Gtk-sharp-list@lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/gtk-sharp-list