Proper separation of interface and logic?

R

robert.waters

I am new to C#, and have written some code to update a textbox control
on a form with the output from an external script. However, I am not
sure if I have properly separated the user interface from the script
processing logic. Would somebody please look at the code below and
let me know if there is anything I should change? (I have omitted
implementation details):
===================================================
[ScriptProcessor.cs]
class ScriptProcessor {
public delegate void UpdateTextCallback(string text);
Process p;
Thread t;
public ScriptProcessor(RichTextBox output,string script) {
p = new Process();
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo .... //(set up Process object)
}
public void Run() {
t = new Thread(new
ThreadStart(StartProcessAndUpdateRichTextBox));
t.Start();
}
private void StartProcessAndUpdateRichTextBox() {
p.Start();
while ((string stdout = p.StandardOutput.ReadLine()) !=
null) {
richText.Invoke(new
UpdateTextCallback(this.UpdateRichTextBoxStdout), stdout);
}
p.WaitForExit();
}
private void UpdateRichTextBoxStdout(string text) {
AppendText(richText, Color.Black, text + "\n");
}
private void AppendText(RichTextBox r, Color c, string text) {
... //print to richtextbox
}
}
[Form1.cs]
....
ScriptProcessor sp;
public Form1() {
InitializeComponent();
sp = new ScriptProcessor(this.richTextBox1, "path-to-
script");
}
private void button1_Click(object sender, EventArgs e) {
sp.Run();
}


===================================================
Among things I am unsure of, are the location of the
UpdateRichTextBox...() function (shouldn't it be in the Form's
code??), and the fact that RichTextBox has to be hard-coded into the
ScriptProcessor implementation (maybe I should use a generic?).

How should I change this?

Thanks!!
Robert Waters
-www.computerator.com
 
I

Ignacio Machin ( .NET/ C# MVP )

I am new to C#, and have written some code to update a textbox control
on a form with the output from an external script. However, I am not
sure if I have properly separated the user interface from the script
processing logic. Would somebody please look at the code below and
let me know if there is anything I should change? (I have omitted
implementation details):


Take a look at MVP design pattern.
 
R

robert.waters

Take a look at MVP design pattern.

Thank you, that is exactly what I am trying to accomplish.
Do you have any idea what part of the code I listed could be
refactored out of the layer in which it currently exists?
I am trying to teach myself by example, and some hints in this matter
would be enormously helpful.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top