Re: threading questions



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.


.



Relevant Pages

  • Re: threading questions
    ... You cannot create a new Form in a worker thread and expect everything ... correctly without the message loop. ... and call a function in my startup form. ... infinite loop on it that calls Thread.Sleep at the desired intervals. ...
    (microsoft.public.dotnet.languages.vb)
  • Help needed with ATL component loaded in browser.
    ... I had built an ATL control that has a graphic on it and that control is ... This polling is done on a worker thread and when new ... message that in turn raises an event in the browser. ... on my worker thread since my message loop is gone. ...
    (microsoft.public.vc.atl)
  • Re: Message Loops and Application.Run()
    ... >> what kind of messages would you like to send to the message loop without ... >> window? ... thread, instantiate the control. ... and vice-versa be wrapped in Invoke or BeginInvoke statements. ...
    (microsoft.public.dotnet.framework)
  • Re: Socket is freezing my datagrid
    ... > Do I have to follow any guideline to update my UI control? ... This callback is always executed within a worker thread. ... In order to safely update your datagrid from a worker thread, ... One important thing here is that the Invoke method relies on uiControl to ...
    (microsoft.public.dotnet.framework.remoting)
  • Re: Socket is freezing my datagrid
    ... >> Do I have to follow any guideline to update my UI control? ... > This callback is always executed within a worker thread. ... > marshall the call to the method that accesses your datagrid to the UI ... > // This is the method executing ...
    (microsoft.public.dotnet.framework.remoting)

Loading