Single Instance per Machine of a WPF Application - c#

I have created a taskbar application that I want to ensure that one and only one instance is running. In an article called, WPF Single Instance Best Practices, Evan Wondrasek posted a great answer and I have added it to my application. This appears to work really well with one small exception.
When I implemented this I have lost my splash screen and I do not know why. Can anyone explain why my splash screen no longer works and how I might get the splash screen working again? I used a simple PNG file and marked its properties as a SplashScreen. I am beginning to think I may need to do something with the SplashScreen class to fix this.
TIA, Doug

By implementing the answer in the link you supplied, you replaced the Main function & startup object of your application. You will need to create the SplashScreen object yourself now, put this inside your own Main method:
SplashScreen splashScreen = new SplashScreen("images/logo.png");
splashScreen.Show(true);
See this link for more information: http://social.msdn.microsoft.com/Forums/vstudio/en-US/33894287-101a-4c9f-8c6a-f0452ab4ced0/splash-screen-not-displaying-anymore-on-startup?forum=wpf

Related

changing culture for a winform application

As explained here I have built a sample console application in which I change the culture in the Main function. It works well.
Now, building another sample winform application, I wonder where would be the best place to put this
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture ("en-US");
Thread.CurrentThread.CurrentUICulture=new CultureInfo("en-US");
If I put this in the constructor of a Form1, will this apply to all operations in this class?
What if this Form1 opens another form Form2 and an exception occurs there?
What if Form1 uses another class Class1?
I am willing to experiment in this, but if someone has an answer based on previous knowledge, I appreciate to hear it.
For a winforms application put things that deal with the context of the entire application in program.cs and be done with it.
If I put this in the constructor of a Form1, will this apply to all operations in this class?
What if this Form1 opens another form Form2 and an exception occurs there?
What if Form1 uses another class Class1?
All your other concerns are irrelevant, and are sereverly overthinking the problem.
Note : The best way to learn programming windows or any other language is to read as much as you can. Microsoft have everything documented always go straight to the source.
Some additional reading for you
Thread.CurrentUICulture Property
Thread.CurrentCulture Property
Initialization code in a WinForms App - Program.cs or MainForm?

How to show a Wait cursor using C# without having an active (or any) Windows Form?

I have tried using this code sample:
private void DoShortRunningTask()
{
using (new StWaitCursor())
{
Thread.Sleep(5000); // 5 sec delay.
.. do some work ..
}
}
From: http://www.codeproject.com/KB/cpp/WaitCursor.aspx
But it did not do anything for me, since I did not have a main form. I do not need a main form. My C# project type is Windows Application, but the only GUI it shows if any is error or success messages dialogs. Is there a way for me to fake the existence of a WinForm (so that it exists but is not visible)? Would the mouse cursor have to be over it in order to show up?
I don't think it would be good practice just to change the cursor when it's not in your UI. You wouldn't want another program messing with the cursor in your UI. If you just want to show that your application is running, have you thought of using a tray app. These are relatively simple to create.
Here's an example I just googled:
http://alanbondo.wordpress.com/2008/06/22/creating-a-system-tray-app-with-c/
You can also show speech bubble messages as your run progresses etc, if you want to let the user know things are happening.
I do not need a main form
Yes, you do. Any UI interaction needs a form, sorry.
way for me to fake the existence of a WinForm (so that it exists but is not visible)?
How is that faking?
Make a form, make it invisible (transaprent - check all the properties on the form class and you will find it) and finished ;) Standard approach. Also tell it not to show up in the forms collection in the bottom of the screen and minimize it and you are finished. nothing fake here.
TomTom++
Are you just wanting to change the current cursor?
Try this
http://www.csharp-examples.net/hourglass-wait-cursor/

How to prevent the Form from opening Multiple Times

I have a application in which i have several forms. In that forms, I have a System Settings form. I have to open this form from the menu as well as a shortcut created on the desktop.
I am able to open the form from 2 places individually. But the problem is, It's opening two separate instance of the same form. it means, first, i have clicked on menu to open the form.Now my Form instance is created and it is displayed on the screen. But whenever i click on my desktop icon, It's creating another instance of the same form instead of displaying the same form. . So it means it's displaying two instances of the same form.
But i have display one form only. I have tried and googled in the net also. I didn't find any information.
Can anybody please help me to fix this issue. Any kind of suggestion will be really helpful to me.
You need a single instance. This construct is already available within the .Net framework. Just check out this post from Hanselman.
Note: I know that the namespace of this class is VisualBasic. But that shouldn't hinder you to use it in your C# application. It's just the name of a namespace. It doesn't meant anything about its functionality. (Microsoft had it better named Foo. In that case it would be much more popular.)
Sounds to me, that you need a mutex to control that only one application instance is running at a given time.
See http://iridescence.no/post/CreatingaSingleInstanceApplicationinC.aspx for further details
You have to implement some kind of locking mechanism to allow only one instance of your program running. I guess your System Settings program could check if other instances are running on computer on program launch, if so terminate, otherwise start a new instance.
To throw in one more link, this post seems to cover (my understanding of) what you are trying to do.
http://dotnetperls.com/single-instance-windows-form
I have used semaphor concept to control this. Whenever the form is opened i am writing an entry into registry. Once the form is closed i am removing this entry.
So, whenever i try to open the form, it will check the registry entry. Based on that it will open the form.
Sounds like you need a modified version of the Singleton design pattern. Try having a public static method within the class that internally calls the constructor for the form. Then as someone suggested use a counter variable to keep track of how may times that static method is called thus giving you a metric you can use to ensure only the desired number if instance are created.

"calling thread must be STA" error when trying to access an object from another window

I have an application that does data acquisition over a serial port (modbus), and I use a BackgroundWorker object to call the device because otherwise it takes time and freezes my UI.
The problem arises when I open a new WPF window and set its DataContext to be the object I'm updating (I want to use this window to get a detailed view of the device - status, charts, bargraphs).
Is there a way of using my data object in another window or should I consider making my app a single windowed one?
Thank you
Try checking out the discussion on a similar question that I asked. I think it might help you out. STAThread missing but it is there. I know the title seems a little different, but a lot of the ideas presented might help you out as well.

open a .exe as a window in an mdi form?

I wanted to create a program that would allow me to open instances of a already exsiting program (i just have the exe) as windows inside (i belive its called mdi)
Is that something i could do? can anyone point me to an example?
Thanks
Maybe this is the answer you are looking for here. It can be done...look in the sample on that link given.
Hope this helps,
Best regards,
Tom.
When Windows starts a program its parent is the Desktop Window.
If you could somehow manipulate that, it may work.
However, I doubt it is possible, as why would I want to allow you to run my application in your window? Especially MDI? Besides that - running in a child window isn't quite the same as running in the "main" window.
Having said that there is an application out there (can't think of it's name OH) that does place individual applications in tabs. Pretty nifty if you're not on Windows 7. The folks over at the Business of Software forum might be able to help you find it.
Well, after starting the app and storing its PID, you could start monitor the windows that get created, either thru a CBTHook or by just using a timer and the GetWindows to find when a top level window gets created by the PID in question.
Then you can use SetParent to make that window a child to your MDIChild (I doubt you can make it your MDIChild directly).
That should get you going. What you'll run into after that I really don't know. I guess you must correlate any movement of either your app or the external app so that thir windows appears to be stuck together...
Maybo you could strip away the caption from the external app (Get/SetWindowsLong). That could make it look better...

Categories