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

Setting the lifespan of a thread

Hi all,

Is there a way to set the lifespan of a thread.

I am trying to start a process that may or may not have an outcome and want
to finish it after a certain length of time finished or not.

So I have

Thread t = new Thread(new ThreadStart(targetMethod));
t.start();

how can I terminate t after a length of time I decide?

Thanks
Nick
Nov 15 '05 #1
4 1811
Hi Nicholas,

Perhaps Thread.Abort() method might help you?

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com

"Nicholas Beenham" <ni**********@blueyonder.co.uk> wrote in message
news:Oq**************@TK2MSFTNGP10.phx.gbl...
Hi all,

Is there a way to set the lifespan of a thread.

I am trying to start a process that may or may not have an outcome and want to finish it after a certain length of time finished or not.

So I have

Thread t = new Thread(new ThreadStart(targetMethod));
t.start();

how can I terminate t after a length of time I decide?

Thanks
Nick

Nov 15 '05 #2
So would I use a timer then call abort?

"Miha Markic" <miha at rthand com> wrote in message
news:u8**************@TK2MSFTNGP12.phx.gbl...
Hi Nicholas,

Perhaps Thread.Abort() method might help you?

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com

"Nicholas Beenham" <ni**********@blueyonder.co.uk> wrote in message
news:Oq**************@TK2MSFTNGP10.phx.gbl...
Hi all,

Is there a way to set the lifespan of a thread.

I am trying to start a process that may or may not have an outcome and

want
to finish it after a certain length of time finished or not.

So I have

Thread t = new Thread(new ThreadStart(targetMethod));
t.start();

how can I terminate t after a length of time I decide?

Thanks
Nick


Nov 15 '05 #3
How about using Join(timeSpan). If you don't want to block the calling
thread, have it start a "joiner thread" that itself starts your worker
thread and then calls Join on it.

The "joiner thread" would call Abort on the worker thread when it gets
control, if the latter has not yet finished.
"Nicholas Beenham" <ni**********@blueyonder.co.uk> wrote in message
news:Oq**************@TK2MSFTNGP10.phx.gbl...
Hi all,

Is there a way to set the lifespan of a thread.

I am trying to start a process that may or may not have an outcome and want to finish it after a certain length of time finished or not.

So I have

Thread t = new Thread(new ThreadStart(targetMethod));
t.start();

how can I terminate t after a length of time I decide?

Thanks
Nick

Nov 15 '05 #4
Hi,

"Nicholas Beenham" <ni**********@blueyonder.co.uk> wrote in message
news:uL**************@TK2MSFTNGP09.phx.gbl...
So would I use a timer then call abort?


Yes, or the solution Fred suggested.
You might even spawn another thread which does the "dirty" job :)

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com
Nov 15 '05 #5

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

Similar topics

4
by: abCSharp | last post by:
I understand that static variables have app-domain scope. Till the app-domain is in memory, the static variable will be in memory. When are the app-domains unloaded is the question. I have read...
3
by: Patrick | last post by:
I am dynamically creating TextArea and drop-down lists in ASP.NET using something like HtmlTextArea eachTextArea = new HtmlTextArea(); I tried to set the "name" of these TextAreas, etc. (e.g....
1
by: Roland Müller | last post by:
Hello, we have converted our project to framework 2.0 but we have still some problems, one of it with setting the UICulture for a aspx-site. Example: there is a simple aspx test site in a...
4
by: Vlad Dogaru | last post by:
Hello, I am trying to use cookies and Python to create a simple login example. But I am very disoriented at the existence of two cookie libraries, namely Cookie and cookielib. I have seen...
7
by: Academic | last post by:
What are the different effects of the following two statements: C1.Cursor = Cursors.WaitCursor C1.Cursor.Current = Cursors.WaitCursor I believe the first replaces the entire C1.Cursor...
12
by: info | last post by:
Hi All, I am trying to set the hourglass cursor inside a class that has nothing to do with MainForm class and I don't want to pass a reference to MainForm. How can I set the current cursor to...
0
by: kidders | last post by:
Seem to be having problems with the en-GB culture/uiCulture setting, its making dates a format of the incorrect MM/dd/yyyy. I've searched the groups on this issue, here is a lengthy thread, where...
4
by: =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?= | last post by:
Hi; When a Page object - the code-behind of an aspx page is created - what is the lifespan of that created object? Is it tied to the session, the request/response, or is it in a poll and reused...
1
by: nicerun | last post by:
I'm using the Application_Start event at Global.asax.cs to invoke thread that do some job. I know that Application_Start event occurs when the very first request to Web Application received. -...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.