Click here to Skip to main content
15,898,373 members
Home / Discussions / C#
   

C#

 
QuestionAdhoc networking using windows application in VS2005 Pin
alish061031-Mar-09 5:00
alish061031-Mar-09 5:00 
AnswerRe: Adhoc networking using windows application in VS2005 Pin
musefan31-Mar-09 5:12
musefan31-Mar-09 5:12 
GeneralRe: Adhoc networking using windows application in VS2005 Pin
alish061031-Mar-09 20:17
alish061031-Mar-09 20:17 
QuestionOpenFileDialog Pin
netJP12L31-Mar-09 4:22
netJP12L31-Mar-09 4:22 
AnswerRe: OpenFileDialog Pin
musefan31-Mar-09 4:30
musefan31-Mar-09 4:30 
AnswerRe: OpenFileDialog Pin
Giorgi Dalakishvili31-Mar-09 4:39
mentorGiorgi Dalakishvili31-Mar-09 4:39 
Questionc# 2005 add_In Excel 2003 Pin
sofianeannabi31-Mar-09 4:13
sofianeannabi31-Mar-09 4:13 
QuestionMulti-threading - Parallel Syncronization Pin
caksabre231-Mar-09 4:12
caksabre231-Mar-09 4:12 
Hello,

I have a program that opens multiple user controls, each one needing to run 3 parallel processes. It needs to run all 3 simultaneously, and then when all 3 are complete run a process. This needs to occur every second.

What I have done, is create 3 threads, one main thread which when it starts running it calls two .Set() methods for manualevent handlers which then sets the other two threads running. I then have a waitall in the main thread (after the point where it has done its work) which waits for the other two threads to complete. At the end of the two threads I have a waitall which waits for a different manualevent to be reset (which happens when the main thread finishes).

Now this all seems to work fine, but when running 4 user controls at once, approximetly every minute the threads somehow get confused and one of the waitall's for the two sub threads get stuck. So I have put a timeout parameter here which is a hack, and doesn't really solve why it is locking up. I can't put all the code in but below is an example of it working.

Can anyone help come up with why this is occurring, or a better way of doing this?

//Thread Ones Run Method
public void ThreadOne()
{
while(true)
{

ogEvents[0] = new ManualResetEvent(false);
ogEvents[1] = new ManualResetEvent(false);

//Do a task here
//Wait for the other 2 threads to finish
bool blSuccess = WaitHandle.WaitAll(ogEvents, 4000, false);

//Do some calcs

Thread.Sleep(1000);
ogThreadTwo[0].Set();
ogThreadThree[0].Set();
}
}

//Thread Two's Run Method
public void ThreadTwo()
{
while(true)
{
//Do some task
//Task Finished
ogEvents[0].Set();
ogThreadTwo[0] = new ManualResetEvent(false);
bool blSuccess = WaitHandle.WaitAll(ogThreadTwo, 4000, false);
}
}

//Thread Three's Run Method
public void ThreadThree()
{
while(true)
{
//Do some task

//Task Finished
ogEvents[0].Set();
ogThreadThree[0] = new ManualResetEvent(false);
bool blSuccess = WaitHandle.WaitAll(ogThreadThree, 4000, false);
}
}
AnswerRe: Multi-threading - Parallel Syncronization Pin
Luc Pattyn31-Mar-09 4:54
sitebuilderLuc Pattyn31-Mar-09 4:54 
AnswerRe: Multi-threading - Parallel Syncronization Pin
Ian Shlasko31-Mar-09 5:02
Ian Shlasko31-Mar-09 5:02 
AnswerRe: Multi-threading - Parallel Syncronization Pin
Alan N31-Mar-09 5:17
Alan N31-Mar-09 5:17 
GeneralRe: Multi-threading - Parallel Syncronization Pin
caksabre231-Mar-09 5:37
caksabre231-Mar-09 5:37 
Questionwhy button_click event raising 2 times? Pin
Narendra Reddy Vajrala31-Mar-09 3:43
Narendra Reddy Vajrala31-Mar-09 3:43 
AnswerRe: why button_click event raising 2 times? Pin
stancrm31-Mar-09 3:47
stancrm31-Mar-09 3:47 
GeneralRe: why button_click event raising 2 times? Pin
Narendra Reddy Vajrala31-Mar-09 3:57
Narendra Reddy Vajrala31-Mar-09 3:57 
GeneralRe: why button_click event raising 2 times? Pin
Ian Shlasko31-Mar-09 4:02
Ian Shlasko31-Mar-09 4:02 
GeneralRe: why button_click event raising 2 times? Pin
Narendra Reddy Vajrala31-Mar-09 4:12
Narendra Reddy Vajrala31-Mar-09 4:12 
AnswerRe: why button_click event raising 2 times? Pin
musefan31-Mar-09 3:54
musefan31-Mar-09 3:54 
GeneralRe: why button_click event raising 2 times? Pin
Narendra Reddy Vajrala31-Mar-09 3:57
Narendra Reddy Vajrala31-Mar-09 3:57 
GeneralRe: why button_click event raising 2 times? Pin
musefan31-Mar-09 4:02
musefan31-Mar-09 4:02 
GeneralRe: why button_click event raising 2 times? Pin
Narendra Reddy Vajrala31-Mar-09 4:16
Narendra Reddy Vajrala31-Mar-09 4:16 
GeneralRe: why button_click event raising 2 times? Pin
musefan31-Mar-09 4:21
musefan31-Mar-09 4:21 
GeneralRe: why button_click event raising 2 times? Pin
Narendra Reddy Vajrala31-Mar-09 4:32
Narendra Reddy Vajrala31-Mar-09 4:32 
GeneralRe: why button_click event raising 2 times? Pin
musefan31-Mar-09 4:34
musefan31-Mar-09 4:34 
GeneralRe: why button_click event raising 2 times? Pin
Colin Angus Mackay31-Mar-09 4:30
Colin Angus Mackay31-Mar-09 4:30 

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.