Re: Invoking a form that was created on a separate thread



OK it wasn't clear that you are parsing an XML file to create a form (XAML-style). I thought you were parsing an XML file for other more conventional purposes and just needed to report progress to the UI.

I think you are on a loser there. You are going to have to bear the performance hit for choosing such an approach. Do the form loading/xaml parsing on the UI thread and stick an hour glass up or some progress bar to inform the user that they are in for a wait. Basically, trying to do anything with UI elements on other threads is not supported (unless you come up with some wacky ideas and I don't have any). How long does it take to load the form if you just let it run and wait?

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


"Dannyboy" <daniel@xxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:e9wlPVmQFHA.3144@xxxxxxxxxxxxxxxxxxxxxxx
Hi Daniel

The my problem occurs in that the new form and all its controls only get created by the parsing routine, its a bit like xaml so all the form definition is in the xml file that is parsed, So I need to be able to parse these files which can take a while on the XDA's but still give progress back and be able to interact with the code behind (the class that instantiated the parsing) once the form is created and showed.

Sorry if I'm not quite getting this.
Thanks
Dan

"Daniel Moth" <dmoth74@xxxxxxxxxxx> wrote in message news:%23ifZhDmQFHA.2788@xxxxxxxxxxxxxxxxxxxxxxx
Don't return a form from a background thread. The form should be on the main form. It is the progress info (percentage and possibly status text) that you return to the main thread/form from the background thread. With backgroundWorker you don't need to explicitly invoke anything. Keep your parsing on the background thread if you have calculated that it takes too long and hence would block the UI.

Whether you are using the BW from my site (VB version) or from opennetcf (C# version), the sample here should help:
http://www.danielmoth.com/Blog/2004/12/backgroundworker-sample.html


Cheers
Daniel
--
http://www.danielmoth.com/Blog/


"Dannyboy" <daniel@xxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:uH18yffQFHA.3988@xxxxxxxxxxxxxxxxxxxxxxx
Hi Daniel thanks for that I shall go through your background worker code, I do currently have a background process implemenation that is based on an article written by Rocky Lhotka on msdn http://msdn.microsoft.com/vbasic/using/columns/adventures/default.aspx?pull=/library/en-us/dnadvnet/html/vbnet09272002.asp the trouble is the blasted form that is returned from that background process throws an exception everytime I try and invoke its show method.

Should I be changing the implementation so that I don't do the parsing on the background thread?

Dan

"Daniel Moth" <dmoth74@xxxxxxxxxxx> wrote in message news:enGb7SfQFHA.508@xxxxxxxxxxxxxxxxxxxxxxx
Check out the BackgroundWorker or do it yourself (parsing on thread and updating the form with control invoke)

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


"Dannyboy" <daniel@xxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:%23bFkZNfQFHA.248@xxxxxxxxxxxxxxxxxxxxxxx
Hi Chris your right its not good all, the the thing is the xml files that are being parsed are pretty big and they take quite a bit of time, so I need to put a progress bar on, I did try creating that on the main the and then updating that from a separate thread.

If there is a better way to do this I'm open to all suggestions cos this is p#@*ing me off

Thanks
Dan
"Chris Tacke, eMVP" <ctacke@xxxxxxxxxxxxxxxxxxxxxx> wrote in message news:u9%23zJ0dQFHA.1528@xxxxxxxxxxxxxxxxxxxxxxx
The first question has to be why are you creating a Form on another thread? It's a very bad idea, a bad design, and bad practice.


-- Chris Tacke Co-founder OpenNETCF.org Has OpenNETCF helped you? Consider donating to support us! http://www.opennetcf.org/donate


"Dan" <daniel@xxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:e5x$vrdQFHA.3628@xxxxxxxxxxxxxxxxxxxxxxx
Hi everyone

I have a class which parses an xml file and creates a form and its controls from it, and I have recenlty moved this so that it happens on its own thread, I know about controls that are created on a different thread to the UI can not just have its methods called without using invoke so with this I have tried several different ways to get the form to show but I'm having problems.

The ways I have have tried are:

1. With the form that is returned from the parsing class I have tried
me.invoke(new delegateSub(addressof newform.show))
This throws the error "Cannot call Invoke or InvokeAsync on a control until the window handle has been created."


2. Add a controller class that is also created on the separate thread which shows the form when the parsing is complete, this does show the form briefly but it then disappears.

3. With the same controller class I use Application.Run(parsedform). This works beautifully apart from one small snag, the class which starts the parsing is passed a reference to the parser and acts as the code behind for the the controls that are created in the parsed form e.g. the click event of a button in the parsed form is handled by a method in the class that is sent to the parser. This is where this method seems to fall down because the reference no longer works.

Am I fighting a loosing battle with this or can anyone offer some code of wisdom.
Any help will be much appreciated.


Many Thanks
Dan















.