event - object referance is not set to an instanse of an object windows forms C# -
i trying raise event in dll file refrenced windows forms project.
i have following message when run program "object not set instace of object":
namespace server {
public delegate void messagehnadler(); public class classserver { public event messagehnadler messageforchat public string message { get; set; } public socket listenersocket; public binaryformatter transbinary; public thread threadingserver; public tcplistener listenerserver; private list<tcpclient> connectedclients = new list<tcpclient>(); public bool openserver(string ipaddress, int portnumber) { try { listenerserver = new tcplistener(ipaddress.parse(ipaddress), portnumber);//creating listener clients connect listenerserver.start(); threadingserver = new thread(loopthroughclients); threadingserver.start(); threadingserver = new thread(getmessage); threadingserver.start(); return true; } catch (exception) { return false; } } public void loopthroughclients() { listenersocket = listenerserver.acceptsocket(); } public void getmessage() { while (true) { if (listenersocket != null) { networkstream streamwithclient = new networkstream(listenersocket); transbinary = new binaryformatter(); string stringfromclient = (string)transbinary.deserialize(streamwithclient); if (stringfromclient != null) { message = stringfromclient; messageforchat(); } streamwithclient = new networkstream(listenersocket); binaryformatter tranbinary = new binaryformatter(); tranbinary.serialize(streamwithclient, stringfromclient); stringfromclient = null; } } }
in windows forms project signed event function:
namespace chat_project_server_ui { public partial class severui : form { openserverform openserver = new openserverform(); classserver serverforevent = new classserver();
public severui() { initializecomponent(); openserver.showdialog(); serverforevent.messageforchat += new messagehnadler(serverforevent_messageforchat); openningserver(); } public void openningserver() { if(openserver.isconnected) { chattextbox.appendtext("server open!\n"); } else { chattextbox.appendtext("faild open server...\n"); } } private void test_click(object sender, eventargs e) { chattextbox.appendtext("aaaaa"); } public void eventholder() { } void serverforevent_messageforchat() { chattextbox.appendtext(serverforevent.message); } }
always check if handler has been assigned first follows:
var handler = messageforchat; if (handler != null) handler()
Comments
Post a Comment