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

Re-running an already created thread.

Hi,
I am running a separate thread in order to be able to poll devices on a
MODBUS RTU network. I create a thread and look at the ThreadObj.IsAlive
flag, and this work ok. I then might have to come back later to the
thread an re run the task contained within it. When I come to re-run
the thread, when I call to ThreadObj.Start it throws an exception.
Ordinarily in C++ I would delete the ThreadObj and then re-create one
with the oThread = new Thread(new ThreadStart(SearchThread)); call.
Seeing as I cannot do my own garbage collection when I need to wondered
if anyone had any comments?

Most of my experience is C++/MFC and am now porting a current app to C#
in order to better my understanding of the langauge.
Thanks,

Rob.
Nov 16 '05 #1
2 2334
Robert,

You will have to recreate the thread exactly as you indicated. Though,
you should make sure that the original thread is not running first.
Take a look at the following multipage article on multithreading in
..NET for tips on working with threads. The article is an excellent
resource that is frequently cited in this group.

<http://www.yoda.arachsys.com/csharp/threads/>

Also, there may be better design patterns for executing this task. One
alternative that comes to mind is to create a thread that is dedicated
to executing the task. This thread will sit idle in an infinite loop
waiting for an event (AutoResetEvent or ManualResetEvent) to be
signaled that instructs the thread to begin executing the task. If the
task completes quickly the ThreadPool may be a better approach. It's
difficult to say for sure which one is better without knowing what the
task does.

Brian

Robert Wilson wrote:
Hi,
I am running a separate thread in order to be able to poll devices on
a MODBUS RTU network. I create a thread and look at the
ThreadObj.IsAlive flag, and this work ok. I then might have to come
back later to the thread an re run the task contained within it.
When I come to re-run the thread, when I call to ThreadObj.Start it
throws an exception. Ordinarily in C++ I would delete the ThreadObj
and then re-create one with the oThread = new
Thread(new ThreadStart(SearchThread)); call. Seeing as I cannot do my
own garbage collection when I need to wondered if anyone had any
comments?

Most of my experience is C++/MFC and am now porting a current app to
C# in order to better my understanding of the langauge.
Thanks,

Rob.


Nov 16 '05 #2
Brian Gideon wrote:
Robert,

You will have to recreate the thread exactly as you indicated. Though,
you should make sure that the original thread is not running first.
Take a look at the following multipage article on multithreading in
.NET for tips on working with threads. The article is an excellent
resource that is frequently cited in this group.

<http://www.yoda.arachsys.com/csharp/threads/>

Also, there may be better design patterns for executing this task. One
alternative that comes to mind is to create a thread that is dedicated
to executing the task. This thread will sit idle in an infinite loop
waiting for an event (AutoResetEvent or ManualResetEvent) to be
signaled that instructs the thread to begin executing the task. If the
task completes quickly the ThreadPool may be a better approach. It's
difficult to say for sure which one is better without knowing what the
task does.

Brian

Robert Wilson wrote:
Hi,
I am running a separate thread in order to be able to poll devices on
a MODBUS RTU network. I create a thread and look at the
ThreadObj.IsAlive flag, and this work ok. I then might have to come
back later to the thread an re run the task contained within it.
When I come to re-run the thread, when I call to ThreadObj.Start it
throws an exception. Ordinarily in C++ I would delete the ThreadObj
and then re-create one with the oThread = new
Thread(new ThreadStart(SearchThread)); call. Seeing as I cannot do my
own garbage collection when I need to wondered if anyone had any
comments?

Most of my experience is C++/MFC and am now porting a current app to
C# in order to better my understanding of the langauge.
Thanks,

Rob.


Thanks a lot Brian. Looks like i'm on my way now.

Regards,

Rob.
Nov 16 '05 #3

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

Similar topics

1
by: Nel | last post by:
I have a question related to the "security" issues posed by Globals ON. It is good programming technique IMO to initialise variables, even if it's just $foo = 0; $bar = ""; Surely it would...
4
by: Craig Bailey | last post by:
Anyone recommend a good script editor for Mac OS X? Just finished a 4-day PHP class in front of a Windows machine, and liked the editor we used. Don't recall the name, but it gave line numbers as...
1
by: Chris | last post by:
Sorry to post so much code all at once but I'm banging my head against the wall trying to get this to work! Does anyone have any idea where I'm going wrong? Thanks in advance and sorry again...
11
by: James | last post by:
My form and results are on one page. If I use : if ($Company) { $query = "Select Company, Contact From tblworking Where ID = $Company Order By Company ASC"; }
1
by: John Ryan | last post by:
What PHP code would I use to check if submitted sites to my directory actually exist?? I want to use something that can return the server code to me, ie HTTP 300 OK, or whatever. Can I do this with...
10
by: James | last post by:
What is the best method for creating a Web Page that uses both PHP and HTML ? <HTML> BLA BLA BLA BLA BLA
8
by: Beowulf | last post by:
Hi Guru's, I have a query regarding using PHP to maintain a user profiles list. I want to be able to have a form where users can fill in their profile info (Name, hobbies etc) and attach an...
3
by: presspley | last post by:
I have bought the book on advanced dreamweaver and PHP recently. I have installed MySQL and PHP server but am getting an error on the $GET statement show below. It says there is a problem with...
2
by: sky2070 | last post by:
i have two file with jobapp.html calling jobapp_action.php <HTML> <!-- jobapp.html --> <BODY> <H1>Phop's Bicycles Job Application</H1> <P>Are you looking for an exciting career in the world of...
1
by: Brian | last post by:
I have an array like this: $events = array( array( '2003-07-01', 'Event Title 1', '1' //ID Number (not unique) ), array( '2003-07-02',
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.