Hi Heiko,
ich muss ehrlich sagen, ich habe mit Threads noch nicht so sehr viel zu
tun gehabt. Vielleicht ist jemand anderes in der Lage weiter zu helfen.
Ich nehme den Code mal morgen mit auf die Arbeit, vielleicht kann jemand
von meinen Kollegen helfen...
CIAO
Michael
> Nur: Ist das so effizient? Verwende ich lock() {} und
> Monitor.Enter() und Monitor.Exit() richtig?
>
> Gru�
>
> Heiko Richler
>
>
>
> //
> // Dies ist eine Ausschnitt aus dem Code, der Zeigen soll wie
> die Thread-Verwaltung funktioniert. // Bis zu 20 Threads
> sollen jeweils arbeiten. // Wird ein neues Element den
> References hinzugef�gt, wird Baby.PulsePool() in CurRef aufgerufen,
> // um Pool.Wait(..) zu beenden.
> //
>
> ExtThreading.Pool Pool = new ExtThreading.Pool();
>
> while (References.UncheckedItemsLeft>0)
> {
> while (References.UncheckedItemsLeft>0 && Pool.Count<20)
> {
> HttpReferences.Item CurRef =
> References.NextUncheckedItem;
> switch (CurRef.Uri.Scheme)
> {
> // more protokolls should be handled!
> case "http":
> case "https":
> Console.WriteLine("Count:{0}
> Cur:{1}", Pool.Count, CurRef.Uri.ToString());
> Pool.RunNewThread( CurRef );
> CurRef.WillBeChecked = false;
> break;
> default:
> // Ignore it:
> CurRef.WillBeChecked = false;
> break;
> }
> }
>
> Pool.Wait(new TimeSpan(0,0,5));
> Console.WriteLine("Pool.Count {0}\r\nUncheckedItemsLeft
> {1}", Pool.Count, References.UncheckedItemsLeft);
> Pool.PrintThreadList(Console.Out);
>
> while (References.UncheckedItemsLeft==0 && Pool.Count>0)
> {
> // While any Thread is running new References
> may be added!
> Pool.Wait(new TimeSpan(0,0,5));
> Console.WriteLine("Pool.Count
> {0}\r\nUncheckedItemsLeft {1}", Pool.Count,
> References.UncheckedItemsLeft);
> Pool.PrintThreadList(Console.Out);
> }
> }
>
> Console.WriteLine("Pool.Count {0}\r\nUncheckedItemsLeft {1}",
> Pool.Count, References.UncheckedItemsLeft);
>
> #endregion
>
>
>
>
>
> //
> // Thread-Verwaltung:
> //
>
>
> #region namespace ExtThreading
>
> namespace ExtThreading
> {
> using System.Threading;
>
> #region abstract class Baby
>
> abstract class Baby
> {
> #region protected variables
>
> protected Pool ThreadsPool;
>
> #endregion
> #region internal functions
>
> internal void SetPool(Pool ThreadsPool)
> {
> this.ThreadsPool = ThreadsPool;
> }
>
> #endregion
> #region abstract functions
>
> abstract public void Start();
> abstract public string GetStatusText();
>
> #endregion
> #region protected functions
>
> protected void PulsePool()
> {
> if (ThreadsPool != null)
> lock(ThreadsPool)
> {
> Monitor.Pulse(ThreadsPool);
> }
> }
>
> protected void Done()
> {
> if (ThreadsPool != null)
> {
> lock(ThreadsPool)
> {
>
> ThreadsPool.UnlistThread(Thread.CurrentThread);
> Monitor.Pulse(ThreadsPool);
> }
> }
> }
> #endregion
> }
>
> #endregion
> #region class Pool
>
> class Pool
> {
> private Hashtable list;
> #region public functions
> public void Wait()
> {
> lock(this)
> {
> Monitor.Wait(this);
> }
> }
> public void Wait(TimeSpan time)
> {
> lock(this)
> {
> Monitor.Wait(this, time);
> }
> }
>
> public void UnlistThread(Thread Element)
> {
> Monitor.Enter(this);
> list.Remove(Element);
> Monitor.Exit(this);
> }
>
> public void RunNewThread( Baby oObject )
> {
> Monitor.Enter(this);
> lock(this)
> {
> oObject.SetPool(this);
> Thread Element = new Thread(
> new ThreadStart (oObject.Start) );
> Element.IsBackground = true;
> list.Add(Element, oObject);
> Element.Start();
> }
> Monitor.Exit(this);
> }
>
> public int Count
> {
> get
> {
> return list.Count;
> }
> }
>
> public void PrintThreadList(TextWriter dest)
> {
> Monitor.Enter(this);
>
> ICollection theKeys = list.Keys;
>
> object[] arrValues = new
> Baby[list.Values.Count];
> list.Values.CopyTo(arrValues,0);
> int index = 0;
> foreach(object CurKey in theKeys)
> {
> dest.WriteLine("{0}:
> Alive:{1}", ((Baby)(arrValues[index])).GetStatusText(),
> ((Thread)CurKey).IsAlive);
> index++;
> }
>
> Monitor.Exit(this);
> }
>
> #endregion
> #region constructors
>
> public Pool()
> {
> list = new Hashtable();
> }
>
> #endregion
>
> }
>
> #endregion
> }
>
> #endregion
>
> | [dotnetdecsharp] als [EMAIL PROTECTED] subscribed
> | http://www.dotnetgerman.com/archiv/dotnetdecsharp/ = Listenarchiv
> | Listenregeln, sowie An- und Abmeldung zu dieser Liste:
> | http://www.dotnetgerman.com/listen/dotnetdecsharp.asp
>
| [dotnetdecsharp] als [email protected] subscribed
| http://www.dotnetgerman.com/archiv/dotnetdecsharp/ = Listenarchiv
| Listenregeln, sowie An- und Abmeldung zu dieser Liste:
| http://www.dotnetgerman.com/listen/dotnetdecsharp.asp