Click here to Skip to main content
15,891,529 members
Home / Discussions / C#
   

C#

 
AnswerRe: MultiThreading in c# Pin
Jacobb Michael18-Sep-09 1:01
Jacobb Michael18-Sep-09 1:01 
QuestionThread Performance Pin
Member 368144317-Sep-09 23:47
Member 368144317-Sep-09 23:47 
AnswerRe: Thread Performance Pin
Nicholas Butler18-Sep-09 0:12
sitebuilderNicholas Butler18-Sep-09 0:12 
AnswerRe: Thread Performance Pin
Luc Pattyn18-Sep-09 2:08
sitebuilderLuc Pattyn18-Sep-09 2:08 
QuestionWeb Browser Event Hook - Problems Pin
Jacobb Michael17-Sep-09 23:31
Jacobb Michael17-Sep-09 23:31 
AnswerRe: Web Browser Event Hook - Problems Pin
omizyurko6-Jun-11 2:21
omizyurko6-Jun-11 2:21 
AnswerRe: Web Browser Event Hook - Problems Pin
j to the 4n8-Nov-13 0:05
j to the 4n8-Nov-13 0:05 
QuestionHelp making threads work in C# similar to Java Pin
Cruces17-Sep-09 23:17
Cruces17-Sep-09 23:17 
Hello,
I've been coding projects in Java for some time now and decided to move to C#. I have some difficulty using the notifyall equivalent of Java in C#. Here's how I use it:
In Java when I make a multithreaded application I use the Wait() command to make the thread sleep, and at some point I use the notifyAll command which makes all threads in the wait state continue operation (from then on it's usually a race over which gets the resources first and the others become blocked again.

Now the closest thing I have found to this in C# is the AutoRaiseEvent and ManualRaiseEvent classes.

How my program works is: There are usually 4-8 threads running asking a question from the class by putting the question in the input queue, the class (called Handler) gets the next question and puts the answer in the outputqueue along with the code that identifies the original question.

Up to now I've been using something like this:
While {!myHandler.hasAnswer(myQuestionCode))
{
Application.DoEvents();
}


unfortunatelly this uses up a lot of the CPU so it has to change.

I tried the following:

While {!myHandler.hasAnswer(myQuestionCode))
{
   RaiseEventObject.WaitOne();
}


and when the Handler produces the answer it calls RaiseEventObject.Set();

I tried using this with AutoRaiseEvent but then only one of the Threads triggers and usually it's the wrong one so it goes back to WaitOne() and nothing happens.

I am now testing the ManualRaiseEvent and altering my code to this:


While {!myHandler.hasAnswer(myQuestionCode))
{
   RaiseEventObject.Reset();
   RaiseEventObject.WaitOne();
}



but still I see a problem, what if two threads pass the WaitOne stage and then before the others can become unblocked (among them the one whose answer has been given) the event is Reset, can that happen? or when the Set() command is sent ALL threads unblock?
If the above happens isn't there a chance that the handler (the calculations it does don't take much time) will trigger the Set while one of the threads is in part of the loop other than WaitOne? if that happens and the Reset triggers, will the Set be lost?

Can anyone suggest corrections to my code or some sort of alternative?

I'm sorry if I did not explain it correctly, English is not my primary language, I tried explaining it as best as I can.

Thanks in advance for any help
AnswerRe: Help making threads work in C# similar to Java Pin
Nicholas Butler18-Sep-09 0:19
sitebuilderNicholas Butler18-Sep-09 0:19 
GeneralRe: Help making threads work in C# similar to Java Pin
Cruces18-Sep-09 2:14
Cruces18-Sep-09 2:14 
GeneralRe: Help making threads work in C# similar to Java Pin
Nicholas Butler18-Sep-09 4:17
sitebuilderNicholas Butler18-Sep-09 4:17 
GeneralRe: Help making threads work in C# similar to Java Pin
Cruces18-Sep-09 4:33
Cruces18-Sep-09 4:33 
QuestionHow can we set formats of excel sheet Pin
Gunjan pandey17-Sep-09 23:16
Gunjan pandey17-Sep-09 23:16 
QuestionDraw Controls in Layers Pin
softwarejaeger17-Sep-09 22:52
softwarejaeger17-Sep-09 22:52 
QuestionDatagridview refreshes automatically reflecting changes in database? Pin
LordZoster17-Sep-09 21:54
LordZoster17-Sep-09 21:54 
AnswerRe: Datagridview refreshes automatically reflecting changes in database? Pin
Christian Graus17-Sep-09 21:56
protectorChristian Graus17-Sep-09 21:56 
AnswerRe: Datagridview refreshes automatically reflecting changes in database? Pin
Tamer Oz17-Sep-09 22:05
Tamer Oz17-Sep-09 22:05 
GeneralRe: Datagridview refreshes automatically reflecting changes in database? Pin
Christian Graus17-Sep-09 22:07
protectorChristian Graus17-Sep-09 22:07 
GeneralRe: Datagridview refreshes automatically reflecting changes in database? Pin
Tamer Oz17-Sep-09 22:15
Tamer Oz17-Sep-09 22:15 
GeneralRe: Datagridview refreshes automatically reflecting changes in database? Pin
LordZoster17-Sep-09 22:17
LordZoster17-Sep-09 22:17 
GeneralRe: Datagridview refreshes automatically reflecting changes in database? Pin
Tamer Oz17-Sep-09 22:22
Tamer Oz17-Sep-09 22:22 
GeneralRe: Datagridview refreshes automatically reflecting changes in database? Pin
Christian Graus17-Sep-09 22:23
protectorChristian Graus17-Sep-09 22:23 
GeneralRe: Datagridview refreshes automatically reflecting changes in database? Pin
LordZoster17-Sep-09 22:44
LordZoster17-Sep-09 22:44 
GeneralRe: Datagridview refreshes automatically reflecting changes in database? Pin
Christian Graus17-Sep-09 23:59
protectorChristian Graus17-Sep-09 23:59 
GeneralRe: Datagridview refreshes automatically reflecting changes in database? Pin
Henry Minute18-Sep-09 3:01
Henry Minute18-Sep-09 3:01 

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.