c# - Dynamicly showing file contents in textbox [WPF] -


im building wpf application in have textbox, problem textbox needs contents of file text, txt file keeps getting written too.

i have made class handle this:

public class chathandler {     public filestream stream;     streamwriter writer;     streamreader reader;      public chathandler()     {         stream = new filestream(@"chat/" + datetime.today.tostring("d") + ".txt", filemode.openorcreate, fileaccess.readwrite, fileshare.readwrite);         writer = new streamwriter(stream);         reader = new streamreader(stream);     }      public void write(string line)     {         writer.writelineasync(line);         writer.flush();     }      public string read()     {         string tmp = "";         string line;         while((line = reader.readline()) != null){             tmp += line + '\n';         }         return tmp;     } } 

and have user control has textbox:

public partial class chatscreen : usercontrol {     mainwindow parent;     chathandler chathandler;     backgroundworker worker;     public chatscreen()     {         initializecomponent();         chathandler = new chathandler();          worker = new backgroundworker();         worker.workersupportscancellation = true;         worker.dowork += new doworkeventhandler(run);     }      private void load()     {      }      private void write(string text)     {         if (chat.text != text && text != "")         {             chat.text = text;         }     }      private void run(object sender, doworkeventargs e)     {         backgroundworker bg = sender backgroundworker;         while (!bg.cancellationpending)         {             chat.dispatcher.begininvoke((action)(()=>{                 write(parent.handler.read());                                 }));             thread.sleep(100);         }     }      private void chat_loaded(object sender, routedeventargs e)     {         parent = (mainwindow)window.getwindow(this);         if (!worker.cancellationpending)         {             worker.runworkerasync();         }      }      private void chat_unloaded(object sender, routedeventargs e)     {         worker.cancelasync();     } } 

but problem occurs: text never changed, gets set once , never changed @ anymore :( eventhough in text file lines getting added.

the content of xaml file are:

<usercontrol x:class="tantibotv2.chatscreen"          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"           xmlns:d="http://schemas.microsoft.com/expression/blend/2008"           mc:ignorable="d"           d:designheight="381" d:designwidth="641" loaded="chat_loaded" unloaded="chat_unloaded">     <grid style="{staticresource contentroot}">         <scrollviewer horizontalscrollbarvisibility="disabled" verticalscrollbarvisibility="visible">             <textbox isreadonly="true" margin="0,0,0,0" textwrapping="wrap" name="chat" isreadonlycaretvisible="false"/>         </scrollviewer>     </grid> </usercontrol> 

can please me problem?

i cannot reproduce exact problem, problem new text in text file appears. because streamreader member variable , forward reader. can fix doing:

public string read() {     using (var stream = new filestream(@"chat.txt", filemode.openorcreate, fileaccess.readwrite, fileshare.readwrite))     {         using (var reader = new streamreader(stream))         {             string tmp = "";             string line;             while ((line = reader.readline()) != null)             {                 tmp += line + '\n';             }             return tmp;         }     } } 

edit: after problem clarified bit more:

xaml:

<grid>      <grid.columndefinitions>         <columndefinition></columndefinition>         <columndefinition></columndefinition>     </grid.columndefinitions>      <grid.rowdefinitions>         <rowdefinition height="*"></rowdefinition>     </grid.rowdefinitions>      <dockpanel grid.column="0" lastchildfill="true">         <label dockpanel.dock="top">irc channel simulator</label>         <button dockpanel.dock="top" click="button_click">send chat</button>         <scrollviewer horizontalscrollbarvisibility="disabled" verticalscrollbarvisibility="visible">             <textbox background="aliceblue" acceptsreturn="true" margin="0,0,0,0" textwrapping="wrap" name="chatwriter" isreadonlycaretvisible="false" />         </scrollviewer>     </dockpanel>      <scrollviewer grid.column="1" horizontalscrollbarvisibility="disabled" verticalscrollbarvisibility="visible">         <textbox isreadonly="true" margin="0,0,0,0" textwrapping="wrap" name="chat" isreadonlycaretvisible="false"/>     </scrollviewer> </grid> 

code behind xaml:

public partial class mainwindow : window {     chathandler chathandler;     backgroundworker worker;      public mainwindow()     {         initializecomponent();          chathandler = new chathandler();          worker = new backgroundworker();         worker.workersupportscancellation = true;         worker.dowork += new doworkeventhandler(run);     }      private void write(string text)     {         if (chat.text != text && text != "")         {             chat.text = text;         }     }      private void run(object sender, doworkeventargs e)     {         backgroundworker bg = sender backgroundworker;         while (!bg.cancellationpending)         {             chat.dispatcher.begininvoke((action)(() => {                 write(chathandler.read());             }));             thread.sleep(100);         }     }      private void chat_loaded(object sender, routedeventargs e)     {         if (!worker.cancellationpending)         {             worker.runworkerasync();         }     }      private void chat_unloaded(object sender, routedeventargs e)     {         worker.cancelasync();     }      private void button_click(object sender, routedeventargs e)     {         chathandler.write(chatwriter.text);     } } 

chathandler.cs

public class chathandler {     public filestream stream;     //streamwriter writer;     //streamreader reader;      public chathandler()     {         stream = new filestream(@"chat.txt", filemode.openorcreate, fileaccess.readwrite, fileshare.readwrite);         //writer = new streamwriter(stream);         //reader = new streamreader(stream);     }      public void write(string line)     {         using (var filestream = new filestream(@"chat.txt", filemode.append, fileaccess.write, fileshare.readwrite))         {             using (var writer = new streamwriter(filestream))             {                 writer.writelineasync(line);                 writer.flush();             }         }     }      public string read()     {         using (var filestream = new filestream(@"chat/" + datetime.today.tostring("d") + ".txt", filemode.open, fileaccess.read, fileshare.readwrite))         {             using (var reader = new streamreader(filestream))             {                 string tmp = "";                 string line;                 while ((line = reader.readline()) != null)                 {                     tmp += line + '\n';                 }                 return tmp;             }         }     } } 

Comments

Popular posts from this blog

php - Admin SDK -- get information about the group -

dns - How To Use Custom Nameserver On Free Cloudflare? -

Python Error - TypeError: input expected at most 1 arguments, got 3 -