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

GUI - Threading question

Hello group,

I have a form that starts a thread via a ThreadStart. The thread lives for
the lifetime of the form, doing processing and so on. My question is: In
such a situation, what is the accepted way to monitor the thread to make
sure it hasn't crashed. Do you use a timer and check the thread's IsAlive or
it's ThreadState every few seconds/minutes, or is there another way?

Suggestions, links are most welcome!

Thanks in advance,
--
Tim Gallivan
I know I'm a great teacher because when I give a lesson, the person never
comes back.
Nov 16 '05 #1
5 1069
Tim,

What I would do is create another thread on top of that, and then pass
the Thread instance that represents the thread you want to check the status
of to that other thread. Then, in this thread, you would call the Join
method. This will cause the thread to block until the other thread
completes.

Then, in the other thread, you can fire an event when the Join method
returns.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Tim Gallivan" <no**********************@edu.gov.on.ca> wrote in message
news:O5**************@TK2MSFTNGP12.phx.gbl...
Hello group,

I have a form that starts a thread via a ThreadStart. The thread lives for
the lifetime of the form, doing processing and so on. My question is: In
such a situation, what is the accepted way to monitor the thread to make
sure it hasn't crashed. Do you use a timer and check the thread's IsAlive
or
it's ThreadState every few seconds/minutes, or is there another way?

Suggestions, links are most welcome!

Thanks in advance,
--
Tim Gallivan
I know I'm a great teacher because when I give a lesson, the person never
comes back.

Nov 16 '05 #2
Thanks, Nicholas. Once again, you earned your volunteer salary!

--
Tim Gallivan
I know I'm a great teacher because when I give a lesson, the person never
comes back.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:O%****************@tk2msftngp13.phx.gbl...
Tim,

What I would do is create another thread on top of that, and then pass
the Thread instance that represents the thread you want to check the status of to that other thread. Then, in this thread, you would call the Join
method. This will cause the thread to block until the other thread
completes.

Then, in the other thread, you can fire an event when the Join method
returns.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Tim Gallivan" <no**********************@edu.gov.on.ca> wrote in message
news:O5**************@TK2MSFTNGP12.phx.gbl...
Hello group,

I have a form that starts a thread via a ThreadStart. The thread lives for the lifetime of the form, doing processing and so on. My question is: In
such a situation, what is the accepted way to monitor the thread to make
sure it hasn't crashed. Do you use a timer and check the thread's IsAlive or
it's ThreadState every few seconds/minutes, or is there another way?

Suggestions, links are most welcome!

Thanks in advance,
--
Tim Gallivan
I know I'm a great teacher because when I give a lesson, the person never comes back.


Nov 16 '05 #3
Another way would be:
1) Have a public status var (bool, enum, other) that is a public or internal
property of the form. This property would be sync'd via a monitor lock.
2) The worker thread will be a member of its own object (i.e. WorkerObject,
etc). This object will have a back reference to the form so it can get/set
the public status property.
3) At appropriate points in the Thread method (i.e. before it blocks on
receive, after it gets data, etc.) post an update to the public field using
the reference.
4) You can have a timer method on the form that queries this shared field
(struct, class, var) for the status info and make correct action on the
form.
5) Optionally, you can also have the worker call a public Update method on
the form that invokes work on the gui thread to make updates. This would
eliminate the need for the timer, but updates can only happen when the
worker decides.

--
William Stacey, MVP

"Tim Gallivan" <no**********************@edu.gov.on.ca> wrote in message
news:O5**************@TK2MSFTNGP12.phx.gbl...
Hello group,

I have a form that starts a thread via a ThreadStart. The thread lives for
the lifetime of the form, doing processing and so on. My question is: In
such a situation, what is the accepted way to monitor the thread to make
sure it hasn't crashed. Do you use a timer and check the thread's IsAlive or it's ThreadState every few seconds/minutes, or is there another way?

Suggestions, links are most welcome!

Thanks in advance,
--
Tim Gallivan
I know I'm a great teacher because when I give a lesson, the person never
comes back.


Nov 16 '05 #4
On Wed, 22 Sep 2004 08:43:34 -0400, "Tim Gallivan"
<no**********************@edu.gov.on.ca> wrote:
Hello group,

I have a form that starts a thread via a ThreadStart. The thread lives for
the lifetime of the form, doing processing and so on. My question is: In
such a situation, what is the accepted way to monitor the thread to make
sure it hasn't crashed. Do you use a timer and check the thread's IsAlive or
it's ThreadState every few seconds/minutes, or is there another way?


There are many ways to approach this problem. I prefer to not poll
things but use events/messages instead.

After you have done the ThreadStart, so the thread object exists, but
before doing the yourthread.Start() that actually causes it to start
running I would set two public properties in the thread object, one
of type Control (tellWho), and one of a delegate that takes a message
string or result code (tellWhat).

Wrap your thread's "mainloop" in a try/finally block to regain control
in case the thread terminates for any reason.

Inside of the /finally/ clause you would just have to do a
tellWho.BeginInvoke(tellWhat,new object[]{message,code,etc});

When you get this event, you know the thread is dying.

Oz
--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 16 '05 #5
Sorry, I'd lost this thread. Thanks to William and oz ..., I prefer the
latter's method.

--
Tim Gallivan
I know I'm a great teacher because when I give a lesson, the person never
comes back.

"ozbear" <oz****@bigpond.com> wrote in message
news:415393ac.422073781@news-server...
On Wed, 22 Sep 2004 08:43:34 -0400, "Tim Gallivan"
<no**********************@edu.gov.on.ca> wrote:
Hello group,

I have a form that starts a thread via a ThreadStart. The thread lives forthe lifetime of the form, doing processing and so on. My question is: In
such a situation, what is the accepted way to monitor the thread to make
sure it hasn't crashed. Do you use a timer and check the thread's IsAlive orit's ThreadState every few seconds/minutes, or is there another way?


There are many ways to approach this problem. I prefer to not poll
things but use events/messages instead.

After you have done the ThreadStart, so the thread object exists, but
before doing the yourthread.Start() that actually causes it to start
running I would set two public properties in the thread object, one
of type Control (tellWho), and one of a delegate that takes a message
string or result code (tellWhat).

Wrap your thread's "mainloop" in a try/finally block to regain control
in case the thread terminates for any reason.

Inside of the /finally/ clause you would just have to do a
tellWho.BeginInvoke(tellWhat,new object[]{message,code,etc});

When you get this event, you know the thread is dying.

Oz
--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Nov 16 '05 #6

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

Similar topics

65
by: Anthony_Barker | last post by:
I have been reading a book about the evolution of the Basic programming language. The author states that Basic - particularly Microsoft's version is full of compromises which crept in along the...
19
by: Jane Austine | last post by:
As far as I know python's threading module models after Java's. However, I can't find something equivalent to Java's interrupt and isInterrupted methods, along with InterruptedException....
3
by: David Harrison | last post by:
I am working on an application on Mac OS X that calls out to python via PyImport_ImportModule(). I find that if the imported module creates and starts a python thread, the thread seems to be...
4
by: Antal Rutz | last post by:
Hi, All! I'm new to threading. I have some design questions: Task: I collect data and store them in an RDBMS (mysql or pgsql) The question is how to do that with threading? The...
6
by: CK | last post by:
I have the following code in a windows service, when I start the windows service process1 and process2 work fine , but final process (3) doesnt get called. i stop and restart the windows service...
7
by: Anthony Nystrom | last post by:
What is the correct way to stop a thread? abort? sleep? Will it start up again... Just curious... If the thread is enabling a form, if the form is disposed is the thread as well? Thanks, ...
4
by: Bob | last post by:
- For cleanup, is it sufficient to set a Thread to Nothing after it's done? - It is OK to pass objects out of the thread? (dumb question maybe but I want to be sure) - What's the best way to...
4
by: DBC User | last post by:
I have a background process which reads a table to see if there are any pending requests. If there are any, then it will start a worker thread (only 10 allowed at a time) and executes a method. In...
4
by: Steven | last post by:
I am taking an "advanced" VB.Net course via web at a state university toward an information science degree. This is my second VB class and I am kind of disappointed in it. This week we covered...
19
by: frankiespark | last post by:
Hello all, I was perusing the internet for information on threading when I came across this group. Since there seems to be a lot of good ideas and useful info I thought I'd pose a question. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.