473,732 Members | 2,227 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Single instance and showing a form

Hi

I have an app that runs without a main form, just a notification icon, when
the user clicks the icon the form is shown, and when the form is minimized
it's hidden.

This all works great, and I have implemented single instance so that when
another instance is run, an event gets signalled in the 1st instance, and
what I would like to do is to show the form. I can do this using Form.Invoke
to show the form in most cases, however at startup of my app the form isn't
shown and if the user runs the app again, I can't use Form.Invoke as there
isn't a valid window handle.

How can I call my code to show the form in the context of the UI thread? If
I call Form.Show from the thread that is waiting on the event (which is from
the threadpool) the form doesn't show properly and all hell breaks loose :)

Thanks in advance!

Matt
Sep 14 '08 #1
7 1956
On Sun, 14 Sep 2008 02:03:01 -0700, Matt <Ma**@discussio ns.microsoft.co m>
wrote:
[...]
How can I call my code to show the form in the context of the UI thread?
If
I call Form.Show from the thread that is waiting on the event (which is
from
the threadpool) the form doesn't show properly and all hell breaks loose
:)
There is a class call Synchronization Context that you can use in lieu of a
Control instance for invoking a delegate on a particular thread. It's
possible that using that would address your need.

Alternatively, you could go ahead and create the form instance immediately
when your application starts, and then call Control.CreateH andle() in your
form's constructor to ensure that its window handle is created and that it
can receive messages (necessary for Invoke() to work). Then you have the
form instance available for calling Invoke(), even though it hasn't been
shown yet. I haven't had a chance to test that particular technique, but
I think it should work.

Finally, you might consider having your main thread not call
Application.Run () until you're ready to show the form for the first time.
Just have it wait (e.g. using an AutoResetEvent or with the Monitor
class), and then have logic in your window-showing code that, if the form
hasn't been created yet, signals the main thread to proceed with creating
and showing the form instance, and then calling Application.Run (). If the
form has been created, then of course you'd just show it. The former
wouldn't require calling Invoke() -- you'd just be using the inter-thread
communication object to release the main thread -- and of course the
latter can call Invoke() since you'd have created the form instance by
then.

Pete
Sep 15 '08 #2
Hi Pete

Thanks for the ideas, I had looked at Synchronization Context briefly, but
every time i accessed Synchronization Context.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.

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, I'll give that a try later and look into the
Synchronization Context in more detail as well. I can't try anything at the
moment as my development machine b0rked this morning :(

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'll give it a try and see what happens later,
when my machine is back up and running :)

Cheers

Matt

"Peter Duniho" wrote:
On Sun, 14 Sep 2008 02:03:01 -0700, Matt <Ma**@discussio ns.microsoft.co m>
wrote:
[...]
How can I call my code to show the form in the context of the UI thread?
If
I call Form.Show from the thread that is waiting on the event (which is
from
the threadpool) the form doesn't show properly and all hell breaks loose
:)

There is a class call Synchronization Context that you can use in lieu of a
Control instance for invoking a delegate on a particular thread. It's
possible that using that would address your need.

Alternatively, you could go ahead and create the form instance immediately
when your application starts, and then call Control.CreateH andle() in your
form's constructor to ensure that its window handle is created and that it
can receive messages (necessary for Invoke() to work). Then you have the
form instance available for calling Invoke(), even though it hasn't been
shown yet. I haven't had a chance to test that particular technique, but
I think it should work.

Finally, you might consider having your main thread not call
Application.Run () until you're ready to show the form for the first time.
Just have it wait (e.g. using an AutoResetEvent or with the Monitor
class), and then have logic in your window-showing code that, if the form
hasn't been created yet, signals the main thread to proceed with creating
and showing the form instance, and then calling Application.Run (). If the
form has been created, then of course you'd just show it. The former
wouldn't require calling Invoke() -- you'd just be using the inter-thread
communication object to release the main thread -- and of course the
latter can call Invoke() since you'd have created the form instance by
then.

Pete
Sep 15 '08 #3
On Sun, 14 Sep 2008 18:55:01 -0700, Matt <Ma**@discussio ns.microsoft.co m>
wrote:
Hi Pete

Thanks for the ideas, I had looked at Synchronization Context briefly, but
every time i accessed Synchronization Context.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
Synchronization Context()") but I have low confidence that that would work
(I'd expect a Forms application to require an actual
WindowsFormsSyn chronizationCon text 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(Application Context) overload so that you can pass
in your own ApplicationCont ext 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 ApplicationCont ext 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
Sep 15 '08 #4
Thanks again Pete for your help, I'll try out your ideas and let you know
about the Application.Run () with the NotifyIcon, I'm interested to know
myself.

You were correct, I did mean CreateControl, sorry, not having access to my
PC (lol, it won't even turn on, I'm waiting for an engineer to arrive) I
can't access the source code, it sounds like CreateHandle is what I need, so
I'll give that a try once my pc is back up and running.

And as far as using the application context, I have to agree with you, seems
like a bit of a hack and only to be used if all else fails :)

Thanks again for your help!

Matt
Sep 15 '08 #5
Hi Peter

I finally have got a development PC back up and running again, and have
given your ideas a try, and I'm happy to say that the Control.CreateH andle
idea has worked a treat! So now I have a nice single instance class that can
notify the running instance when it's run a second time and will show the
form correctly in all cases.

I took another look at the documentation, and according to that,
CreateControl is supposed to be the method to use, as it creates the handle
and also any child controls, but it definitely doesn't create the handle, oh
well :)

Many thanks for your suggestions!

Matt
Sep 23 '08 #6
On Mon, 22 Sep 2008 19:56:00 -0700, Matt <Ma**@discussio ns.microsoft.co m>
wrote:
Hi Peter

I finally have got a development PC back up and running again, and have
given your ideas a try, and I'm happy to say that the
Control.CreateH andle
idea has worked a treat! So now I have a nice single instance class that
can
notify the running instance when it's run a second time and will show the
form correctly in all cases.
Excellent. Glad to hear it worked for you.
I took another look at the documentation, and according to that,
CreateControl is supposed to be the method to use, as it creates the
handle
and also any child controls, but it definitely doesn't create the
handle, oh
well :)
I'm not sure what docs you're looking at, but the MSDN docs do clearly
state that CreateControl() will _not_ create the handle if the Visible
property is "false". And of course, until you have shown the form, it
will be "false". Since your scenario is one in which the form has
specifically not been shown, thus ensuring that the Visible property is
"false", the docs do clearly indicate that in your scenario, calling
CreateControl() isn't going to have any effect.

Pete
Sep 23 '08 #7
Yeah it was the docs for Control.CreateH andle... it says you should use
CreateControl as the preferred method, which was why I tried CreateControl
originally, but I did miss the part about the visible flag in the docs for
CreateControl.. . Doh! Sorry about that

Thanks again

Matt

"Peter Duniho" wrote:
On Mon, 22 Sep 2008 19:56:00 -0700, Matt <Ma**@discussio ns.microsoft.co m>
wrote:
Hi Peter

I finally have got a development PC back up and running again, and have
given your ideas a try, and I'm happy to say that the
Control.CreateH andle
idea has worked a treat! So now I have a nice single instance class that
can
notify the running instance when it's run a second time and will show the
form correctly in all cases.

Excellent. Glad to hear it worked for you.
I took another look at the documentation, and according to that,
CreateControl is supposed to be the method to use, as it creates the
handle
and also any child controls, but it definitely doesn't create the
handle, oh
well :)

I'm not sure what docs you're looking at, but the MSDN docs do clearly
state that CreateControl() will _not_ create the handle if the Visible
property is "false". And of course, until you have shown the form, it
will be "false". Since your scenario is one in which the form has
specifically not been shown, thus ensuring that the Visible property is
"false", the docs do clearly indicate that in your scenario, calling
CreateControl() isn't going to have any effect.

Pete
Sep 23 '08 #8

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

Similar topics

16
10131
by: Elad | last post by:
Hi, I have an application that is made up of several executables. I need all these executables to use the same instance of an object. What is the best, most efficient way to approach this? Thanks a lot!
18
5658
by: Steve Barnett | last post by:
I want to ensure that there is only ever one instance of my app running on a single PC at any time. I understand that I can achieve this by using a mutex and, if I can't take ownership of the mutex, I know there is another instance of my program running. Problem is, how do I get the "new" instance to communicate with the "old" instance? The new one will have been started with a command line parameter that I want to pass to the old...
9
5109
by: MrSpock | last post by:
1. Create a new Windows Application project. 2. Open the project properties and check "Make single instance application". 3. Build. 4. Go to the release folder and run the application. 5. Try to open a second instance of the application. This will cause an unhandled exception and the "Send Error Report" box shows up. Does this happen to anyone else, or is it just me? Debug info: "Unhandled exception at 0x00e149fd in...
7
1958
by: Jeffery Tyree | last post by:
I am writing an application in C#.NET that is "AlwaysOnTop" and there should only be one instance of this program running at any given time. The "AlwaysOnTop" piece is working just fine but I need to know how to prevent multiple instances of this program from running. I've been away from coding for quite a while... Back in the old day with k&r C writing a Win3.x application, I seem to remember being able to simply state somewhere whether...
1
1343
by: Ratnesh Raval | last post by:
hi all, i've a tray app. that runs in a tray with no forms. when a user clicks on the trayicon i show main form. my question is, can i make it like the messenger applications.. so if user trys to run second instance it will show the form of the already running instance. the problem is if there is any form already running i can show it ( i found some solutions for that) but what in my case?
3
7195
by: Mark Jerde | last post by:
VS 2005. When I google "CSharp single instance form" the returned pages usually use a Mutex or the VB.NET runtime library. http://en.csharp-online.net/Application_Architecture_in_Windows_Forms_2.0%E2%80%94Single-Instance_Detection_and_Management http://www.codeproject.com/csharp/SingleInstanceApplication.asp http://blogs.msdn.com/onoj/archive/2004/06/04/148532.aspx "Program.cs" below seems to work fine. A feature is it restores a...
1
1001
by: Adnan Chattha | last post by:
i want to use a single instance of a form in my application but can't? plz anyone give me an easy solution? Thanks
3
1719
by: sklett | last post by:
I suspect the answer might be in one of the words of my subject, but here goes anyway. I'm working on a system that will execute a very long (300+) chain of task objects. Here is some quick pseudo code to illustrate: public class VideoAcquisition { public Image GetFrame(){}; // other stuff
1
2516
by: Prats | last post by:
I have a windows form application developed in C++/CLR How can I verify that only one instance of the application is running on the user system at any given time. Thanks
0
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9447
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9181
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.