c# - Best way to notify ui what's happening on another thread in wpf? -
i'm using mvvm, , in viewmodel start thread (it server application, connection thread), , i'm looking appropriate way (or ways (!)) notify ui what's happening on thread. way want it, have textbox of sort logs (lines stored in observablecollection probably), , each time happens on connection thread, want new line added textbox. here's how set command (the method starts thread listening connections):
public viewmodel() { startcommand = new relaycommand(packethandler.start); }
packethandler class:
public static void start() { var connectionthread = new thread(startlistening); connectionthread.isbackground = true; connectionthread.start(); } private static void startlistening() { if (!isinitialized) initialize(); try { listener.start(); while (true) { client = listener.accepttcpclient(); // kind of logging here reaches ui var protocol = new protocol(client); var thread = new thread(protocol.startcommunicating) { isbackground = true }; thread.start(); connectedthreads.add(thread); } } catch (exception) { // temp messagebox.show("error in packethandler class"); } }
i'm looking possible solutions, preferably best. i'm beginner programmer, may not comprehend complex solutions, please bear in mind this, too. note: read events, observer pattern, , few other things possible solutions, don't know (and of course: how) use them properly. in advance!
one more similar working example
view
<window x:class="multipledatagrid.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" height="350" width="525"> <stackpanel> <scrollviewer maxheight="100"> <itemscontrol itemssource="{binding serverlog}"> <itemscontrol.itemtemplate> <datatemplate> <textblock text="{binding}"/> </datatemplate> </itemscontrol.itemtemplate> </itemscontrol> </scrollviewer> </stackpanel> </window>
view codebehind
using system.windows; namespace multipledatagrid { public partial class mainwindow : window { public mainwindow() { initializecomponent(); this.datacontext = new viewmodel(); } } }
your serverthread
using system; using system.collections.objectmodel; using system.threading; using system.windows; using system.windows.data; namespace multipledatagrid { public class { public static void start() { var connectionthread = new thread(startlistening); log = new observablecollection<string>(); bindingoperations.enablecollectionsynchronization(log, _lock);//for thread safety connectionthread.isbackground = true; connectionthread.start(); } public static observablecollection<string> log { get; private set; } private static readonly object _lock = new object(); private static void startlistening() { try { int = 0; while (i <= 100) { log.add("something happened " + i); thread.sleep(1000); i++; } } catch (exception) { // temp messagebox.show("error in packethandler class"); } } } }
and viewmodel
using system.collections.objectmodel; using system.componentmodel; namespace multipledatagrid { public class viewmodel : inotifypropertychanged { public observablecollection<string> serverlog { get; private set; } public viewmodel() { something.start(); something.log.collectionchanged += (s, e) => { serverlog = something.log; raisepropertychanged("serverlog"); }; } public event propertychangedeventhandler propertychanged; public void raisepropertychanged(string propname) { var pc = propertychanged; if (pc != null) pc(this, new propertychangedeventargs(propname)); } } }
Comments
Post a Comment