|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Threading question....I am having trouble figuring out the best what to accomplish this fairly
simple goal. I have a program that controls a 1-wire network and I can only make requests to it one at a time, but I will need to make new requests (from multiple threads) before the first one is done. My idea is to use a Queue to add requests to and then have another process (thread) that will simply wait for the Queue to not be empty and then process requests one at a time until it is empty and then again wait for the Queue to not be empty. I'm looking for a basic framework of what would be best way to do this using what Classes and how to signal between threads (if needed?). I would use Queue, Thread? and ?? Any help is appreciated. Norm Do you care about the order the requests are handled? If not, put your
network handler in a module (class or regular) and then use the following code around your "critical section" static SyncObj as new Object Synclock SyncObj ' Process Critical Section End SyncLock Note that SyncObj is static as it must stick around for the life of the application. What this does is stops all threads and forces them to wait until the previous thread is complete with the network IO. Mike Ober. Show quoteHide quote "Justin" <justin23856 @ hotmail> wrote in message news:e9MUxvsHGHA.2012@TK2MSFTNGP14.phx.gbl... > I am having trouble figuring out the best what to accomplish this fairly > simple goal. I have a program that controls a 1-wire network and I can only > make requests to it one at a time, but I will need to make new requests > (from multiple threads) before the first one is done. My idea is to use a > Queue to add requests to and then have another process (thread) that will > simply wait for the Queue to not be empty and then process requests one at a > time until it is empty and then again wait for the Queue to not be empty. > > I'm looking for a basic framework of what would be best way to do this using > what Classes and how to signal between threads (if needed?). I would use > Queue, Thread? and ?? > > Any help is appreciated. > > Norm > > > Mike,
Thanks for the input. However, that is exactly what I am doing now, and I do (now) care about the sequence of requests. That is why I was thinking of using the Queue class to hold requests. The main part I'm having trouble with is in another thread processing those requests in the Queue and letting the thread that added the request to the queue know that its request has been fulfilled. And, also how to make the Queue processing thread 'sleep' until the Queue.count is > 0. As an aside... I was wondering if you know the difference between using SyncLock/End SyncLock and Monitor.Enter/Monitor.Exit? Thanks again for any input! Norm Show quoteHide quote "Michael D. Ober" <ober***@.alum.mit.edu.nospam> wrote in message news:StBAf.1485$Dk.119@newsread3.news.pas.earthlink.net... > Do you care about the order the requests are handled? If not, put your > network handler in a module (class or regular) and then use the following > code around your "critical section" > > static SyncObj as new Object > Synclock SyncObj > ' Process Critical Section > End SyncLock > > Note that SyncObj is static as it must stick around for the life of the > application. What this does is stops all threads and forces them to wait > until the previous thread is complete with the network IO. > > Mike Ober. > > "Justin" <justin23856 @ hotmail> wrote in message > news:e9MUxvsHGHA.2012@TK2MSFTNGP14.phx.gbl... >> I am having trouble figuring out the best what to accomplish this fairly >> simple goal. I have a program that controls a 1-wire network and I can > only >> make requests to it one at a time, but I will need to make new requests >> (from multiple threads) before the first one is done. My idea is to use >> a >> Queue to add requests to and then have another process (thread) that will >> simply wait for the Queue to not be empty and then process requests one >> at > a >> time until it is empty and then again wait for the Queue to not be empty. >> >> I'm looking for a basic framework of what would be best way to do this > using >> what Classes and how to signal between threads (if needed?). I would use >> Queue, Thread? and ?? >> >> Any help is appreciated. >> >> Norm >> >> >> > > > Justin,
In this case, create a new class that inherits from the Queue class. Override the Enqueue (Add) and Dequeue (Remove) methods and put a synclock around the actual add and remove methods of the queue class. This way your new class is thread safe. I haven't used the Monitor.Enter/Monitor.Exit interface - SyncLock and AutoResetEvents have been sufficient for my needs. Mike. Show quoteHide quote "Justin" <justin23856 @ hotmail> wrote in message news:%23Mpf3ovHGHA.3944@tk2msftngp13.phx.gbl... > Mike, > > Thanks for the input. However, that is exactly what I am doing now, and I > do (now) care about the sequence of requests. That is why I was thinking of > using the Queue class to hold requests. The main part I'm having trouble > with is in another thread processing those requests in the Queue and letting > the thread that added the request to the queue know that its request has > been fulfilled. And, also how to make the Queue processing thread 'sleep' > until the Queue.count is > 0. > > As an aside... I was wondering if you know the difference between using > SyncLock/End SyncLock and Monitor.Enter/Monitor.Exit? > > Thanks again for any input! > > Norm > > > "Michael D. Ober" <ober***@.alum.mit.edu.nospam> wrote in message > news:StBAf.1485$Dk.119@newsread3.news.pas.earthlink.net... > > Do you care about the order the requests are handled? If not, put your > > network handler in a module (class or regular) and then use the following > > code around your "critical section" > > > > static SyncObj as new Object > > Synclock SyncObj > > ' Process Critical Section > > End SyncLock > > > > Note that SyncObj is static as it must stick around for the life of the > > application. What this does is stops all threads and forces them to wait > > until the previous thread is complete with the network IO. > > > > Mike Ober. > > > > "Justin" <justin23856 @ hotmail> wrote in message > > news:e9MUxvsHGHA.2012@TK2MSFTNGP14.phx.gbl... > >> I am having trouble figuring out the best what to accomplish this fairly > >> simple goal. I have a program that controls a 1-wire network and I can > > only > >> make requests to it one at a time, but I will need to make new requests > >> (from multiple threads) before the first one is done. My idea is to use > >> a > >> Queue to add requests to and then have another process (thread) that will > >> simply wait for the Queue to not be empty and then process requests one > >> at > > a > >> time until it is empty and then again wait for the Queue to not be empty. > >> > >> I'm looking for a basic framework of what would be best way to do this > > using > >> what Classes and how to signal between threads (if needed?). I would use > >> Queue, Thread? and ?? > >> > >> Any help is appreciated. > >> > >> Norm > >> > >> > >> > > > > > > > > > Thanks Mike, I will try that....
Show quoteHide quote "Michael D. Ober" <ober***@.alum.mit.edu.nospam> wrote in message news:EPXAf.1919$Dk.808@newsread3.news.pas.earthlink.net... > Justin, > > In this case, create a new class that inherits from the Queue class. > Override the Enqueue (Add) and Dequeue (Remove) methods and put a synclock > around the actual add and remove methods of the queue class. This way > your > new class is thread safe. > > I haven't used the Monitor.Enter/Monitor.Exit interface - SyncLock and > AutoResetEvents have been sufficient for my needs. > > Mike. > > "Justin" <justin23856 @ hotmail> wrote in message > news:%23Mpf3ovHGHA.3944@tk2msftngp13.phx.gbl... >> Mike, >> >> Thanks for the input. However, that is exactly what I am doing now, and > I >> do (now) care about the sequence of requests. That is why I was thinking > of >> using the Queue class to hold requests. The main part I'm having trouble >> with is in another thread processing those requests in the Queue and > letting >> the thread that added the request to the queue know that its request has >> been fulfilled. And, also how to make the Queue processing thread >> 'sleep' >> until the Queue.count is > 0. >> >> As an aside... I was wondering if you know the difference between using >> SyncLock/End SyncLock and Monitor.Enter/Monitor.Exit? >> >> Thanks again for any input! >> >> Norm >> >> >> "Michael D. Ober" <ober***@.alum.mit.edu.nospam> wrote in message >> news:StBAf.1485$Dk.119@newsread3.news.pas.earthlink.net... >> > Do you care about the order the requests are handled? If not, put your >> > network handler in a module (class or regular) and then use the > following >> > code around your "critical section" >> > >> > static SyncObj as new Object >> > Synclock SyncObj >> > ' Process Critical Section >> > End SyncLock >> > >> > Note that SyncObj is static as it must stick around for the life of the >> > application. What this does is stops all threads and forces them to > wait >> > until the previous thread is complete with the network IO. >> > >> > Mike Ober. >> > >> > "Justin" <justin23856 @ hotmail> wrote in message >> > news:e9MUxvsHGHA.2012@TK2MSFTNGP14.phx.gbl... >> >> I am having trouble figuring out the best what to accomplish this > fairly >> >> simple goal. I have a program that controls a 1-wire network and I >> >> can >> > only >> >> make requests to it one at a time, but I will need to make new >> >> requests >> >> (from multiple threads) before the first one is done. My idea is to > use >> >> a >> >> Queue to add requests to and then have another process (thread) that > will >> >> simply wait for the Queue to not be empty and then process requests >> >> one >> >> at >> > a >> >> time until it is empty and then again wait for the Queue to not be > empty. >> >> >> >> I'm looking for a basic framework of what would be best way to do this >> > using >> >> what Classes and how to signal between threads (if needed?). I would > use >> >> Queue, Thread? and ?? >> >> >> >> Any help is appreciated. >> >> >> >> Norm >> >> >> >> >> >> >> > >> > >> > >> >> >> > > >
Is F1 in VS2005 really dumb?
Time zones: Pretending to be somewhere else beginner question about classes Newbe question, What am I doing wrong Seeking VB.Net Proxy examples vb.net tabpage hiding painting Detect if user is logged in ADO.NET now or LINQ later Selecting combobox item at runtime from code No Beep with MSGBOX Possible? |
|||||||||||||||||||||||