Click here to Skip to main content
15,887,812 members
Home / Discussions / C#
   

C#

 
GeneralRe: huffman tree algorithm Pin
Frank Olorin Rizzi24-Jul-03 4:11
Frank Olorin Rizzi24-Jul-03 4:11 
GeneralRe: huffman tree algorithm Pin
Julian Bucknall [MSFT]24-Jul-03 8:48
Julian Bucknall [MSFT]24-Jul-03 8:48 
GeneralRe: huffman tree algorithm Pin
Frank Olorin Rizzi24-Jul-03 4:07
Frank Olorin Rizzi24-Jul-03 4:07 
GeneralRe: huffman tree algorithm Pin
jtmtv1824-Jul-03 7:52
jtmtv1824-Jul-03 7:52 
GeneralRe: huffman tree algorithm Pin
Julian Bucknall [MSFT]24-Jul-03 8:50
Julian Bucknall [MSFT]24-Jul-03 8:50 
GeneralRe: huffman tree algorithm Pin
Frank Olorin Rizzi24-Jul-03 16:39
Frank Olorin Rizzi24-Jul-03 16:39 
GeneralMultithreaded application design question. Pin
GriffonRL23-Jul-03 11:11
GriffonRL23-Jul-03 11:11 
GeneralRe: Multithreaded application design question. Pin
wightman_michael22-Dec-09 15:40
wightman_michael22-Dec-09 15:40 
You could use something like this.

I should allow all threads to run apox 30 seconds then abort them
if they have not completed.


class Program
{
private static AutoResetEvent e = new AutoResetEvent(false);



//No delay
public static void DoWork1()
{
Console.WriteLine("DoWork1 Completed");
e.Set();
}

//Long but completes
public static void DoWork2()
{
Thread.Sleep(new TimeSpan(0, 0, 25));
Console.WriteLine("DoWork2 Completed");
e.Set();
}

//runs too long
public static void DoWork3()
{
Thread.Sleep(new TimeSpan(0, 0, 60));
Console.WriteLine("DoWork3 Completed");
e.Set();
}


static void Main(string[] args)
{

//Create array of worker threads
Thread[] workers = new Thread[3];
(workers[0] = new Thread(new ThreadStart(DoWork1))).Start();
(workers[1] = new Thread(new ThreadStart(DoWork2))).Start();
(workers[2] = new Thread(new ThreadStart(DoWork3))).Start();

DateTime abortTime = DateTime.Now.AddSeconds(30);

//Wait until timout expires or all all thread are
//completed.
while (DateTime.Now < abortTime)
{
//Waits for 10 seconds on until any thread completes
e.WaitOne(new TimeSpan(0, 0, 10));

int running = 0;

foreach (Thread t in workers)
{
if (t.ThreadState != System.Threading.ThreadState.Stopped)
{
running++;
}
}

if (running == 0)
{
break;
}
else
{
Console.WriteLine("Running threads {0}", running);
}

}

//Kill off the threads that have not stopped
foreach (Thread t in workers)
{
try
{
t.Abort();
}
catch
{
Console.WriteLine("Aborted");
}
}
}
}
QuestionMethodInfo Invoke, bad performance? Pin
STW23-Jul-03 9:15
STW23-Jul-03 9:15 
AnswerRe: MethodInfo Invoke, bad performance? Pin
Eric Gunnerson (msft)23-Jul-03 10:02
Eric Gunnerson (msft)23-Jul-03 10:02 
GeneralRe: MethodInfo Invoke, bad performance? Pin
STW26-Jul-03 23:37
STW26-Jul-03 23:37 
GeneralRe: MethodInfo Invoke, bad performance? Pin
Eric Gunnerson (msft)27-Jul-03 5:23
Eric Gunnerson (msft)27-Jul-03 5:23 
GeneralRe: MethodInfo Invoke, bad performance? Pin
STW27-Jul-03 8:48
STW27-Jul-03 8:48 
AnswerRe: MethodInfo Invoke, bad performance? Pin
Daniel Turini23-Jul-03 11:31
Daniel Turini23-Jul-03 11:31 
GeneralRe: MethodInfo Invoke, bad performance? Pin
STW26-Jul-03 23:41
STW26-Jul-03 23:41 
GeneralAccessing Functions Pin
ppathan23-Jul-03 6:08
ppathan23-Jul-03 6:08 
GeneralRe: Accessing Functions Pin
Rocky Moore23-Jul-03 12:39
Rocky Moore23-Jul-03 12:39 
QuestionHow to birng to top an application? Pin
Ivan Fernandez23-Jul-03 3:51
Ivan Fernandez23-Jul-03 3:51 
AnswerRe: How to birng to top an application? Pin
Valeria Bogdevich23-Jul-03 9:44
Valeria Bogdevich23-Jul-03 9:44 
GeneralRe: How to birng to top an application? Pin
Ivan Fernandez23-Jul-03 21:41
Ivan Fernandez23-Jul-03 21:41 
GeneralRe: How to birng to top an application? Pin
Valeria Bogdevich23-Jul-03 22:05
Valeria Bogdevich23-Jul-03 22:05 
GeneralReflector Pin
monrobot1323-Jul-03 3:22
monrobot1323-Jul-03 3:22 
GeneralRe: Reflector Pin
Nathan Blomquist23-Jul-03 4:55
Nathan Blomquist23-Jul-03 4:55 
GeneralRe: Reflector Pin
Erick Sgarbi23-Jul-03 17:41
Erick Sgarbi23-Jul-03 17:41 
GeneralRe: Reflector Pin
Ista24-Jul-03 5:54
Ista24-Jul-03 5:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.