473,394 Members | 1,674 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,394 software developers and data experts.

Suspending a thread from another thread

Max
Hello,

I made an application that uses the main thread for the UI, and another thread to communicate through the RS232 port. I would
like the communication thread to be suspended immediately when the user presses the ESC key. It should resume when the user answers
a dialog box. The Thread.Suspend cannot be used anymore in .NET and I'm trying to figure out how to do this differently. Can anyone
help?

Cheers,
Max.

Feb 7 '07 #1
2 2290

Use a ManualResetEvent.

The RS232 thread should wait on this event regularly within it's
communication loop. Leave the event will be in Set state. When the
user presses ESC in the UI thread Reset the event and the RS232 will
wait the next time it gets to a WaitOne call (so you have to call
WaitOne within your communication loop as often as possbile).

Then when the UI thread answers the dialog call ManualResetEvent.Reset
and the RS232 thread will continue.

HTH,

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Wed, 7 Feb 2007 09:28:22 -0500, "Max" <ma******@yahoo.comwrote:
>Hello,

I made an application that uses the main thread for the UI, and another thread to communicate through the RS232 port. I would
like the communication thread to be suspended immediately when the user presses the ESC key. It should resume when the user answers
a dialog box. The Thread.Suspend cannot be used anymore in .NET and I'm trying to figure out how to do this differently. Can anyone
help?

Cheers,
Max.
Feb 7 '07 #2

"Max" <ma******@yahoo.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
Hello,

I made an application that uses the main thread for the UI, and another
thread to communicate through the RS232 port. I would like the
communication thread to be suspended immediately when the user presses the
ESC key. It should resume when the user answers a dialog box. The
Thread.Suspend cannot be used anymore in .NET and I'm trying to figure out
how to do this differently. Can anyone help?
In reality, there's no need for a separate thread to accomplish what you
describe. Windows serial ports fully support overlapped I/O completion
callbacks. However the serial port class in the .NET Framework BCL doesn't
expose this functionality, and the .NET message loop doesn't wait alertably,
so you can't even implement this single threaded design via P/Invoke.

However, using overlapped I/O with completion callbacks is entirely possible
in your second thread, then you use an alertable wait
(WaitForSingleObjectEx) to wait for I/O events to occur, or if you set your
suspend/exit event. If you need more wakeup conditions, there's also
WaitForMultipleObjectsEx.

Another way, requiring Vista, is to P/Invoke CancelIoEx to interrupt your
current I/O operation.
>
Cheers,
Max.

Feb 7 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Jeffrey Hiess | last post by:
Something like getchar() doesn't work, since it "waits" for input. #include <stdio.h> while (1) { if (getchar()) /* break if any key is pressed */ { break; }
0
by: Owen Woo | last post by:
By Microsoft.Win32.PowerModeChangedEventHandler my application can only receive suspending event. But how can I prevent from suspending?
10
by: Xarky | last post by:
Hi, I created a thread, that is creating an array of labels and is writing them to my windows form, exatly on a tab page. But it is giving me the following error. An unhandled exception of...
20
by: Bob Day | last post by:
Using VS 2003, VB, MSDE... There are two threads, A & B, that continously run and are started by Sub Main. They instantiationsl of identical code. Thread A handles call activity on telephone...
7
by: Charles Law | last post by:
My first thought was to call WorkerThread.Suspend but the help cautions against this (for good reason) because the caller has no control over where the thread actually stops, and it might have...
8
by: Rob | last post by:
Hello, I've got an issue where a process in a third party application has a dll which while exiting sort of freezes and runs away with processor cycles. i've written a block of code so that I...
5
by: | last post by:
I have a user control (tag) made up up 9 smaller user controls (minitag). I have a panel on a form that is being populated with 81 of the tag controls in a grid configuration. Even though I am...
12
by: Dave | last post by:
Hi all, I have an application which has some worker threads which often have to stop and wait for some further information from other threads. These pauses will often take a long time (a couple...
5
by: Uzi | last post by:
Hi all, I have a thread which is running a long method in a third party component (witch is a com object and accessed via interop). I need to be able to suspend this thread. the...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.