Re: threading questions
- From: "ChunkyMonkey" <Chunky@xxxxxxxxxx>
- Date: Thu, 13 Aug 2009 13:37:44 -0700
HI,
Thanks for you feedback.
The idea of the new thread creating a new form, is probably not really required if I think about it.
Would I make it all easier if instead of creating a new form, I just dealt with a control on the main form ?
Could the new thread just then update a control on the frmMain, as the thread was created from frmMain or is the invoke stuff still required ?
Thanks
"Brian Gideon" <briangideon@xxxxxxxxx> wrote in message news:36b7e882-4a67-4926-a964-8530c61974c9@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Aug 13, 2:29 pm, "ChunkyMonkey" <Chu...@xxxxxxxxxx> wrote:
Howdy
In my startup form, I start up a thread for a function that I want to run in
the background(the function just polls a system every second). See first
section of code below.
What I am doing at the moment, is in the new thread, I load a form, and this
form has a timer that when it ticks, get the status of a remote machine. At
the moment on every tick, it add an item to a list box which is just the
now() value so that I can see it working
You cannot create a new Form in a worker thread and expect everything
to work correctly. The problem is that your worker thread will not be
running a message loop. Windows forms and controls do not work
correctly without the message loop.
This all works fine.
It may appear to work fine, but all is not good.
The issue is that I want the new thread that is polling the machine, to
update the main thread, and call a function in my startup form.
I tried in my timer event, to call the public function in my startup form,
just passing some text, but nothing happens.
What do I need to do, so that the new thread can call functions on the
frmMain...
You will need to use the ISynchronizeInvoke interface on the startup
form to accomplish this. That interfaces exposes Invoke and
BeginInvoke methods that both accept a delegate as a parameter. The
execution of that delegate is marshalled onto the thread hosting the
target object (usually a form or control).
I would avoid creating the frmComms form on the worker thread. It
really needs be created and used on the main UI thread. The polling
of the system can be accomplished in several ways, but if you want to
continue with the worker thread design then you could create an
infinite loop on it that calls Thread.Sleep at the desired intervals.
After Thread.Sleep returns then the thread could execute the desired
logic and report its progress to the UI thread via calls to
Form.Invoke or Form.BeginInvoke.
.
- Follow-Ups:
- Re: threading questions
- From: Brian Gideon
- Re: threading questions
- References:
- threading questions
- From: ChunkyMonkey
- Re: threading questions
- From: Brian Gideon
- threading questions
- Prev by Date: Re: New to .NET
- Next by Date: Re: threading questions
- Previous by thread: Re: threading questions
- Next by thread: Re: threading questions
- Index(es):
Relevant Pages
|
Loading