On Sun, 14 Sep 2008 18:55:01 -0700, Matt <Ma**@discussions.microsoft.com>
wrote:
Hi Pete
Thanks for the ideas, I had looked at SynchronizationContext briefly, but
every time i accessed SynchronizationContext.Current it always returned
null,
although I was only doing this early on in the app before I had called
Application.Run, so that might have been the issue.
Hmmm...not sure. I admit, I've never had to use the class in a context
when I was trying to get the instance before I'd called
Application.Run(). I don't know why it shouldn't work before you called
Application.Run(), but that could simply be one of the dark corners of the
Forms API.
I suppose you might try just instantiating the class directly ("new
SynchronizationContext()") but I have low confidence that that would work
(I'd expect a Forms application to require an actual
WindowsFormsSynchronizationContext instance).
I had tried instantiating the MainForm object and then calling Create,
but
that did not allow me to use Invoke, although I haven't tried using
CreateHandle,
There's no "Create()" method. Maybe you mean CreateControl()? If so,
then yes...that behaves differently than CreateHandle(). In particular,
it won't do anything if the Visible property of the Control is false,
which it would be in this case.
CreateHandle(), on the other hand, will create the window handle for the
control, regardless of its current visibility state.
[...]
Also your comments about not calling Application.Run until I need to show
the form are interesting, hadn't thought of that one, thanks for
pointing it
out! :) One question though, do I need to call Application.Run for the
notification icon to work?
I don't know. It's not something I've had a need to try. I wouldn't
think you'd need to, but if you do need for Application.Run() to be called
before NotifyIcon to work, a variation on what I described would be to go
ahead and call Application.Run() during startup, without showing the
form. But use the Run(ApplicationContext) overload so that you can pass
in your own ApplicationContext instance that allows you to call ExitThread
on that instance.
That way, you can cause Application.Run() to exit, after which that thread
could then create the form instance, show it, and then call
Application.Run() again.
Now, all that said, I believe that putting a call to CreateHandle() into
your form constructor will allow you to call Invoke() on that form
instance without having actually shown it yet.
Barring that, I'd hope you could get the NotifyIcon to work without having
to actually call Application.Run(). After all, the NotifyIcon is running
on a different thread, and in fact that fact is the reason you're having
trouble in the first place. :)
And barring all that, you could use an explicit ApplicationContext so that
you can call Application.Run() and then control its execution from another
thread, so that you can force it to return allowing your code to then
create the form, show it, and then call Application.Run() again.
Especially once you get to that last approach, it's kind of a hack and I'd
hope that calling CreateHandle() would work instead. But if it doesn't,
these alternatives shouldn't be _too_ bad, I think.
Pete