--- SOLVED ---
original post:
Progress:I'm looking for a c# example that shows multithreading and MVC combined.
for example: a form with two counters counting up from 0 to 100 with thread1 increasing counter1 every 100ms and thread2 increasing counter2 every 150ms.
I know the MVC pattern and I'm familiar with multithreading but I haven't worked with them alot so I'd really appreciate a simple example to show me the basic setup.
thx in advance!
I wrote an example code for the proposed example but can't get it to work. As sugested I used a cross thread controler to fix the thread access problem. Now I get a different error though:
"Parameter count mismatch." on line "Application.Run(new Form1());"
current code:
program.cs
Form1.cs:Code:using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace WindowsFormsApplication2 { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }
Code:using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Windows.Forms; namespace WindowsFormsApplication2 { public partial class Form1 : Form, TView { TModel model = new TModel(); private TControler controler; public Form1() { InitializeComponent(); model.addView(this); } private void button1_Click(object sender, EventArgs e) { //setup 2 threads that work to increase counters TControler cont = new TControler(model); cont.setStep(400); Thread thread = new Thread(new ThreadStart(cont.runme)); thread.Name = "first"; thread.Start(); TControler cont2 = new TControler(model); cont2.setCounter(2); cont2.setStep(500); Thread thread2 = new Thread(new ThreadStart(cont2.runme)); thread2.Name = "second"; thread2.Start(); } private delegate void CrossThreadLabelHandler(Label obj, String str); private void UpdateLabelText(Label obj, String str) { try { if (obj.InvokeRequired) obj.BeginInvoke(new CrossThreadLabelHandler(this.UpdateLabelText), str); else obj.Text = str; } catch(Exception e) { Console.Out.WriteLine("Exception: "+e); } } public void doUpdate() { UpdateLabelText(label1, model.getC1().ToString()); UpdateLabelText(label2, model.getC2().ToString()); } } public interface aModel { void addView(TView v); void removeView(TView v); void notifyListeners(); } public class TModel : aModel { private int c1 = 0; private int c2 = 0; public void increase1() { c1++; notifyListeners(); } public void increase2() { c2++; notifyListeners(); } public int getC1() { return c1; } public int getC2() { return c2; } ArrayList l=new ArrayList(); public void addView(TView v) { l.Add(v); } public void removeView(TView v) { l.Remove(v); } public void notifyListeners() { foreach(TView v in l) { v.doUpdate(); } } } public interface TView { void doUpdate(); } public interface aControler { } public class TControler: aControler { private TModel model; private volatile bool run; private int step=100; private int counter = 1; internal TControler(TModel m) { model=m; run = true; } public void setStep(int ms) { step = ms; } public void setCounter(int c) { counter = c; } public void runme() { while(run) { if(counter==1) model.increase1(); else model.increase2(); Thread.Sleep(step); } } } }



LinkBack URL
About LinkBacks



