473,382 Members | 1,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

C#-APP: Blocking while waiting for an event

Hi.

I'm coding a C# app and I need to block processing in order to wait for an event.
If I use Thread.Sleep() the event is never triggered so I don't know what else to do.

This is my code (well, pseudo, as I don't have access to it right now):

Expand|Select|Wrap|Line Numbers
  1. bool dataArrived;
  2. byte[] data;
  3.  
  4. void EventReceiveData()
  5. {
  6.   data = //.. whatever
  7.   dataArrived = true;
  8. }
  9.  
  10. byte[] RequestX()
  11. {
  12.   dataArrived = false;
  13.   SendData('X');
  14.  
  15.   while (!dataArrived)
  16.     Thread.Sleep(10);
  17.  
  18.   Return data;
  19. }

Any ideas?

Thank you in advance.
Alex
Nov 2 '07 #1
1 24226
balabaster
797 Expert 512MB
Hi.

I'm coding a C# app and I need to block processing in order to wait for an event.
If I use Thread.Sleep() the event is never triggered so I don't know what else to do.

This is my code (well, pseudo, as I don't have access to it right now):

Expand|Select|Wrap|Line Numbers
  1. bool dataArrived;
  2. byte[] data;
  3.  
  4. void EventReceiveData()
  5. {
  6. data = //.. whatever
  7. dataArrived = true;
  8. }
  9.  
  10. byte[] RequestX()
  11. {
  12. dataArrived = false;
  13. SendData('X');
  14.  
  15. while (!dataArrived)
  16. Thread.Sleep(10);
  17.  
  18. Return data;
  19. }

Any ideas?

Thank you in advance.
Alex
I would use a ManualResetEvent (found in System.Threading). You would reset this before you fire your secondary thread and then use the WaitOne method to block the current thread. You would then have your secondary thread set the ManualResetEvent which would cause the main thread to continue.
VB
Expand|Select|Wrap|Line Numbers
  1.  'Classwide declaration: 
  2. Dim oSignalEvent As New System.Threading.ManualResetEvent(False)
  3.  
  4. Private Sub SecondThread()
  5. 'Do stuff
  6. oSignalEvent.Set()
  7. End Sub
  8.  
  9. Private Sub MainThread()
  10.     'Do stuff
  11.     'Call secondary thread
  12.  
  13.     Dim oSecondThread As New System.Threading.Thread(AddressOf SecondThread)
  14.     oSecondThread.Start()
  15.  
  16.     oSignalEvent.WaitOne() 'This thread will block here until the reset event is set.
  17.     oSignalEvent.Reset() 'Set the signal back to false
  18.     'Do more stuff
  19.  
  20. End Sub
  21.  
C#
Expand|Select|Wrap|Line Numbers
  1.  ManualResetEvent oSignalEvent = New ManualResetEvent; 
  2. void SecondThread(){
  3. //DoStuff
  4. oSignalEvent.Set();
  5. }
  6. void Main(){
  7. //DoStuff
  8. //Call second thread
  9. System.Threading.Thread oSecondThread = new System.Threading.Thread(SecondThread);
  10. oSecondThread.Start();
  11.  
  12. oSignalEvent.WaitOne(); //This thread will block here until the reset event is sent.
  13. oSignalEvent.Reset();
  14. //Do more stuff
  15. }
  16.  
Nov 2 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Paul E. Orman | last post by:
I have a piece of VB code (.NET 1.1 - VB 2003) that loads data from a database through a timer. So the timer is setup and from it I call the procedure that loads the latest records from the...
3
by: David Sworder | last post by:
This message was already cross-posted to C# and ADO.NET, but I forgot to post to this "general" group... sorry about that. It just occured to me after my first post that the "general" group readers...
44
by: Charles Law | last post by:
Hi guys. I'm back on the threading gig again. It's the age-old question about waiting for something to happen without wasting time doing it. Take two threads: the main thread and a worker...
2
by: Rene | last post by:
Hi, In my VB6 application I'm using a class/object that is using full-async ADO. I can start multiple queries, the class stores the ADODB.Recordset object in an array and waits for the...
2
by: Charles Law | last post by:
I may have asked this before, but what is the purpose of both these functions? Is there a time when one should be called over the other? Does one supersede the other, or do they both have their...
2
by: Paul E. Orman | last post by:
I have a piece of VB code (.NET 1.1 - VB 2003) that loads data from a database through a timer. So the timer is setup and from it I call the procedure that loads the latest records from the...
0
by: lechatthierry | last post by:
Is it possible to block a mouse event on an Hyperlink with a general script event? This is quite troublesome for me. I am trying to find a way to block the windows shortcut SHIFT + MOUSE LEFT...
1
by: Neko | last post by:
Is it possible to block a mouse event on an Hyperlink with a general script event? This is quite troublesome for me. I am trying to find a way to block the windows shortcut SHIFT + MOUSE LEFT...
3
by: geskerrett | last post by:
We have been asked to develop and application for a client that is a 'notification" system. We would like to use python, but are struggling to find the right starting point. Any suggestions, tips...
2
by: ext237 | last post by:
hello. I'm trying to halt a function's execution while waiting for user interaction. For example, I have a function called getUserValue() that pops up a hidden div containing several buttons....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.