source: trunk/CrypPlugins/MD5Collider/Algorithm/ColliderWorkerAdapter.cs @ 1430

Last change on this file since 1430 was 1430, checked in by pretzsch, 11 years ago

MD5Collider: Added comments

File size: 2.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.ComponentModel;
6
7namespace Cryptool.Plugins.MD5Collider.Algorithm
8{
9    /// <summary>
10    /// Manager for a worker thread that runs a <c>IMD5ColliderAlgorithm</c> belonging to a <c>MultiThreadedMD5Collider</c>
11    /// </summary>
12    /// <typeparam name="T"></typeparam>
13    class ColliderWorkerAdapter<T> where T : IMD5ColliderAlgorithm, new()
14    {
15        /// <summary>
16        /// The collider class which is run
17        /// </summary>
18        private IMD5ColliderAlgorithm wrappedCollider;
19
20        /// <summary>
21        /// The multi-threaded collider to which this adapter belongs
22        /// </summary>
23        private MultiThreadedMD5Collider<T> multiThreadedCollider;
24
25        /// <summary>
26        /// The managed <c>BackgroundWorker</c> object
27        /// </summary>
28        private BackgroundWorker worker;
29
30        /// <summary>
31        /// Indicates whether the worker is running
32        /// </summary>
33        public bool IsStarted { get { return worker.IsBusy; } }
34
35        /// <summary>
36        /// Constructs the adapter, giving the multi-threaded collider it belongs to and the collider object to execute in the managed thread.
37        /// </summary>
38        /// <param name="multiThreadedCollider">The multi-threaded collider to which this adapter belongs</param>
39        /// <param name="wrappedCollider">The collider which is executed in the managed thread</param>
40        public ColliderWorkerAdapter(MultiThreadedMD5Collider<T> multiThreadedCollider, IMD5ColliderAlgorithm wrappedCollider)
41        {
42            this.multiThreadedCollider = multiThreadedCollider;
43            this.wrappedCollider = wrappedCollider;
44
45            worker = new BackgroundWorker();
46            worker.DoWork += DoWork;
47            worker.RunWorkerCompleted += RunWorkerCompleted;
48        }
49
50        /// <summary>
51        /// Delegate which the managed <c>BackgroundWorker</c> object calls to perform work
52        /// </summary>
53        /// <param name="sender">Event sender</param>
54        /// <param name="e">Event arguments</param>
55        private void DoWork(object sender, DoWorkEventArgs e)
56        {
57            wrappedCollider.FindCollision();
58        }
59
60        /// <summary>
61        /// Delegate which the managed <c>BackgroundWorker</c> object calls when work is finished
62        /// </summary>
63        /// <param name="sender">Event sender</param>
64        /// <param name="e">Event arguments</param>
65        private void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
66        {
67            multiThreadedCollider.SignalWorkIsFinished(wrappedCollider);
68        }
69
70        /// <summary>
71        /// Starts the managed worker thread
72        /// </summary>
73        public void StartWork()
74        {
75            worker.RunWorkerAsync();
76        }
77    }
78}
Note: See TracBrowser for help on using the repository browser.