Hide form at launch - c#

I have a program which only needs a NotifyIcon to work as intended. So I've been trying to get the main form to hide when the program starts.
In frmMain_Load, I tried both
this.Hide();
this.Visible = false;
without success.
They work in other methods, like in the NotifyIcon_MouseClick-method, but I want it to hide at Load.
I saw in another question here at SO where Matias suggested this:
BeginInvoke(new MethodInvoker(delegate
{
Hide();
}));
This works, but when I launch the program I can see the form flashing real fast. It's better than nothing, but I wonder if there is any better solution to this.
Thanks.

// In Your Program.cs Convert This
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
// To This
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 TheForm = new Form1();
Application.Run();
}
// Call Application.Exit() From Anywhere To Stop Application.Run() Message Pump and Exit Application

There is an easy way, if your program has the default Visual Studio generated Program.cs file:
[STAThread]
static void Main()
{
Application.EnableVisualStyles ();
Application.SetCompatibleTextRenderingDefault (false);
Application.Run (new MainForm ());
}
the simple fact of calling Run will, indeed make the form visible. Try doing the following in the properties of your form:
Set WindowState to Minimized
Set ShowInTaskbar to false
This should do the trick!

Don't call Show or ShowDialog on your form, you can have your Application.Run target a custom class that then instantiates a form and doesn't show or creates a NotifyIcon instance and handles everything from there.

You can also put this.hide = true in the form_shown event. I believe that event is fired once only and after the load event. You might see alittle flicker though if your form has a lot of controls and/or the computer is slow.

If your program doesn't require a form to run, then the best method is to not have a form at all.
Setup your NotifyIcon in the Program code, and enter a loop until you want to exit the program by setting some value, or calling some method.
In this example setting UserExitCalled to true (Program.UserExitCalled = true) will cause the program to quit.
Here is a brief example:
static class Program {
internal static Boolean UserExitCalled;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// Setup your tray icon here
while (!UserExitCalled) {
Application.DoEvents(); // Process windows messages
Thread.Sleep(1);
}
return;
}
}
Here the full program class from one of my system tray applications as a working example.
// *********************************************************************
// [DCOM Productions .NET]
// [DPDN], [Visual Studio Launcher]
//
// THIS FILE IS PROVIDED "AS-IS" WITHOUT ANY WARRANTY OF ANY KIND. ANY
// MODIFICATIONS TO THIS FILE IN ANY WAY ARE YOUR SOLE RESPONSIBILITY.
//
// [Copyright (C) DCOM Productions .NET All rights reserved.]
// *********************************************************************
namespace VisualStudioLauncher
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Threading;
using VisualStudioLauncher.Common.Objects;
using VisualStudioLauncher.Forms;
using System.Drawing;
using VisualStudioLauncher.Common.Data;
using System.IO;
static class Program
{
#region Properties
private static ProjectLocationList m_ProjectLocationList;
/// <summary>
/// Gets or Sets the ProjectsLocationList
/// </summary>
public static ProjectLocationList ProjectLocationList
{
get
{
return m_ProjectLocationList;
}
set
{
m_ProjectLocationList = value;
}
}
private static ShellProcessList m_ShellProcessList = null;
/// <summary>
/// Gets or Sets the ShellProcessList
/// </summary>
public static ShellProcessList ShellProcessList
{
get
{
return m_ShellProcessList;
}
set
{
m_ShellProcessList = value;
}
}
private static NotifyIcon m_TrayIcon;
/// <summary>
/// Gets the programs tray application.
/// </summary>
public static NotifyIcon TrayIcon
{
get
{
return m_TrayIcon;
}
}
private static bool m_UserExitCalled;
/// <summary>
/// Gets a value indicating whether the user has called for an Application.Exit
/// </summary>
public static bool UserExitCalled
{
get
{
return m_UserExitCalled;
}
set
{
m_UserExitCalled = value;
}
}
// TODO: Finish implementation, then use this for real.
private static ApplicationConfiguration m_ApplicationConfiguration = null;
/// <summary>
/// Gets the application configuration
/// </summary>
public static ApplicationConfiguration ApplicationConfiguration
{
get
{
if (m_ApplicationConfiguration == null)
m_ApplicationConfiguration = ApplicationConfiguration.LoadConfigSection(#"./settings.config");
return m_ApplicationConfiguration;
}
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
if (args.Length > 0)
{
if (args[0].ToLower() == "-rmvptr")
{
for (int i = 1; i < args.Length; i++) {
try {
if (File.Exists(Application.StartupPath + #"\\" + args[i])) {
File.Delete(Application.StartupPath + #"\\" + args[i]);
}
}
catch { /* this isn't critical, just convenient */ }
}
}
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
SplashForm splashForm = new SplashForm();
splashForm.Show();
while (!UserExitCalled)
{
Application.DoEvents();
Thread.Sleep(1);
}
if (m_TrayIcon != null)
{
m_TrayIcon.Icon = null;
m_TrayIcon.Visible = false;
m_TrayIcon.Dispose();
GC.Collect();
}
}
#region System Tray Management
public static void SetupTrayIcon()
{
m_TrayIcon = new NotifyIcon();
m_TrayIcon.Text = Resources.UserInterfaceStrings.ApplicationName;
m_TrayIcon.Visible = false; // This will be set visible when the context menu is generated
m_TrayIcon.MouseDoubleClick += new MouseEventHandler(m_TrayIcon_MouseDoubleClick);
if (Orcas.IsInstalled)
{
m_TrayIcon.Icon = Orcas.Icon;
}
else if (Whidbey.IsInstalled) {
m_TrayIcon.Icon = Whidbey.Icon;
}
else {
m_TrayIcon.Icon = SystemIcons.Warning;
m_TrayIcon.Text = "Visual Studio is not installed. VSL cannot run properly.";
}
}
static void m_TrayIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
{
return;
}
SettingsForm settingsForm = new SettingsForm();
settingsForm.Show();
}
#endregion
}
}

Related

winform doesn't apear when code is run

i'm building a quick and dirty program to basically turn on a light in my room from a website as a code kata. during the programming i decided i would temporarily use a winform to test instead of hooking the physical light up (and having all sorts of possible problems there). but when i run my program the winform doesn't show, I've tried to run the executable but still nothing. when debugging i can see all the code works fine it's just that the winform doesn't show up. here is all the code:
form1.cs:
using System;
using System.Drawing;
using System.IO;
using System.Net;
using System.Threading;
using System.Windows.Forms;
namespace alarm_light_test
{
public partial class Form1 : Form
{
WebClient client = new WebClient();
public Form1()
{
InitializeComponent();
while (true)
{
if (CheckData())
{
TurnSirenOn();
client.DownloadString("**link to .php file to reset the .txt file**");
Thread.Sleep(5000);
TurnSirenOff();
}
else
{
Thread.Sleep(1000);
}
}
}
public bool CheckData()
{
bool retval = false;
Stream stream = client.OpenRead("**link to online .txt file**");
StreamReader reader = new StreamReader(stream);
String content = reader.ReadToEnd();
if(content == "1")
{
retval = true;
}
return retval;
}
public void TurnSirenOn()
{
pictureBox1.BackColor = Color.Green;
}
public void TurnSirenOff()
{
pictureBox1.BackColor = Color.Red;
}
}
}
program.cs:
using System;
using System.Windows.Forms;
namespace alarm_light_test
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Form1.Designer.cs
namespace alarm_light_test
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(13, 13);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(235, 235);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(260, 260);
this.Controls.Add(this.pictureBox1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.PictureBox pictureBox1;
}
}
In your constructor public Form1(), you have a loop for-ever (a.k.a an infinite loop).
The while(true) will prevent the constructor from finishing the construction of the form object, and therefore will not be able to display anything.
public Form1()
{
InitializeComponent();
while (true) // This loop will never exit, and will run forever
{
...
}
}
Edit: Sample of how to run your code Asynchronously, therefore allowing the form to fully initialise and display as expected.
public Form1()
{
InitializeComponent();
DoWorkAsynchronously();
}
private async Task DoWorkAsynchronously()
{
await Task.Run(() =>
{
while (true)
{
if (CheckData())
{
TurnSirenOn();
client.DownloadString("**link to .php file to reset the .txt file**");
Thread.Sleep(5000);
TurnSirenOff();
}
else
{
Thread.Sleep(1000);
}
}
});
}

occasionally problems with invokerequired in c#

I'm having problems with the invokerequired method from time to time and wanted to aks if someone knows that problem or can it explain to me. I have some code:
splashScreen splashObj = splashScreen.GetInstance();
if (thrd == null)
{
thrd = new Thread(new ThreadStart(loadingScreenStart));
thrd.IsBackground = true;
thrd.Start();
}
else
{
if (splashObj.InvokeRequired)
{
splashObj.Invoke((MethodInvoker)delegate()
{
splashObj.Show();
}
);
}
else
{
splashObj.Show();
}
}
if (splashObj.loadLabel.InvokeRequired)
{
splashObj.loadLabel.Invoke((MethodInvoker)delegate()
{
splashObj.loadLabel.Text = "Checking Username...";
}
);
}
else
{
splashObj.loadLabel.Text = "Checking Username...";
}
public void loadingScreenStart()
{
splashScreen splashObj = splashScreen.GetInstance();
Application.Run(splashScreen.GetInstance());
}
the splashscreen code:
public partial class splashScreen : Form
{
public Form1 form1;
public splashScreen()
{
InitializeComponent();
//form1 = frm1;
}
public splashScreen(Form1 frm1)
{
form1 = frm1;
}
private void splashScreen_Load(object sender, EventArgs e)
{
}
private static splashScreen m_instance = null;
private static object m_instanceLock = new object();
public static splashScreen GetInstance()
{
lock (m_instanceLock)
{
if (m_instance == null)
{
m_instance = new splashScreen();
}
}
return m_instance;
}
}
So I'm showing a splashscreen while the rest of the code is loading in the background. Messages are getting showed like "Checking Username...", "Checking Password...", "Loading..." and so on.
This code works fine but from time to time it seems like the code execution is faster than the thread and then I get an error that the method was called from an other thread than the one it was created on. Why does this happen? Is there any solution for this kind of problem? That happens maybe once in twenty executions.
Multi-threading is hard, so you should try making it as simple as possible. One, there's no reason to have any of the splashscreen code outside of the splashscreen class itself. Second, you should always know what you're doing, so InvokeRequired is just a code smell that says "I have no idea who's going to call this method".
void Main()
{
SplashScreen.ShowText("Loading 1");
Thread.Sleep(1000);
SplashScreen.ShowText("Loading 2");
Thread.Sleep(2000);
SplashScreen.Done();
Thread.Sleep(2000);
SplashScreen.ShowText("Loading 3");
}
// Define other methods and classes here
public partial class SplashScreen : Form
{
private static SplashScreen instance;
private static readonly ManualResetEvent initEvent = new ManualResetEvent(false);
Label loadLabel;
private SplashScreen()
{
// InitializeComponent();
loadLabel = new Label();
Controls.Add(loadLabel);
Load += (s, e) => initEvent.Set();
Closing += (s, e) => initEvent.Reset();
}
private static object syncObject = new object();
private static void InitializeIfRequired()
{
// If not set, we'll have to init the message loop
if (!initEvent.WaitOne(0))
{
lock (syncObject)
{
// Someone initialized it before us
if (initEvent.WaitOne(0)) return;
// Recreate the form if it was closed
instance = new SplashScreen();
var thread = new Thread(() => { Application.Run(instance); });
thread.Start();
// Wait until the form is ready
initEvent.WaitOne();
}
}
}
public static void ShowText(string text)
{
InitializeIfRequired();
instance.Invoke((Action)(() =>
{
if (!instance.IsDisposed) instance.loadLabel.Text = text;
}
));
}
public static void Done()
{
// Is it closed already?
if (!initEvent.WaitOne(0)) return;
lock (syncObject)
{
// Someone closed it before us
if (!initEvent.WaitOne(0)) return;
instance.Invoke((Action)(() => { instance.Close(); }));
}
}
}
This is a snippet for LINQPad, you'll want to remove the comment on InitializeComponent as well as the loadLabel control (which should be on the designer instead).
This way, you have the logic of the splash screen completely isolated from the rest of the application. To show a splashscreen text, just call SplashScreen.ShowText. To make the splashscreen go away, call SplashScreen.Done.
Note how there's no need to use InvokeRequired - there's no way any legitimate call of SplashScreen.ShowText (or Done) would ever not need marshalling to the UI thread of the splash screen.
Now, this is not perfect. It's something I wrote in about 10 minutes. But it's (probably :)) thread-safe, follows best practices a lot better than the original, and is much easier to use.
Also, there's cases where I would use higher-level constructs, e.g. Lazy<T> and Task - but since that wouldn't really help here (starting the new messaging loop, having to recreate the form if it was closed...), I opted for the simpler solution instead.
Note that I'm using Invoke rather than BeginInvoke or similar - this is quite important, because otherwise it would be possible to queue a close, followed by a ShowText that would work on a disposed form. If you intend to call ShowText from multiple threads, it would be safer to also lock around the whole ShowText body. Given the usual use case for splash screens, there's some thread-safety that's unnecessary, but...
Ok so this problem still exists, it can happen when your thread is calling the form before it shows; both InvokeRequired and BeginInvoke may fail.
A solution is to start your thread on form event Shown:
/// <summary>
/// Testing form
/// </summary>
public partial class MyForm : Form
{
/// <summary>
/// Work work
/// </summary>
private Thread _MyThread;
/// <summary>
/// New test form
/// </summary>
public MyForm()
{
// Initialize components
this.InitializeComponent();
}
/// <summary>
/// First time form is show
/// </summary>
private void MyForm_Shown(object sender, EventArgs e)
{
// Create and start thread
this._MyThread = new Thread(this.MyThreadWork);
this._MyThread.Start();
}
/// <summary>
/// Doing some work here
/// </summary>
private void MyThreadWork()
{
// Counting time
int count = 0;
// Forever and ever
while (true)
{
this.PrintInfo((count++).ToString());
Thread.Sleep(1000);
}
}
/// <summary>
/// Printing info into form title
/// </summary>
private void PrintInfo(string info)
{
// Needs to sync?
if (this.InvokeRequired)
{
// Sync me
this.BeginInvoke(new Action<string>(this.PrintInfo), info);
}
else
{
// Prints info
this.Text = info;
}
}
/// <summary>
/// Good bye form, don't miss the work work
/// </summary>
private void MyForm_FormClosed(object sender, FormClosedEventArgs e)
{
// Kill worker
this._MyThread.Abort();
}
}
Some will just add a Thread.Sleep before first PrintInfo but common, what sleep time do you would use? 1, 10, 100? Depends on how fast your form will show.

Run one instance of program

i have one problem with this ?!
i use this way for run only one instance of program.
it's do very good.but when i use this way in other app .
when i run one of them programs via shortcut from desktop , both programs invoke and show in desktop.
note : both programs run in windows system try .
static bool ok;
static Mutex mutex = new Mutex(true, "{123Newsoft-Cleaner Portable Program123}",out ok);
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
//Application.EnableVisualStyles();
//Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new Form1());
if (mutex.WaitOne(TimeSpan.Zero, true))
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var mainForm = new Form1c();
try
{
mainForm.Visible = false;
mainForm.WindowState = FormWindowState.Normal;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Application.Run(mainForm);
}
else
{
NativeMethods.PostMessage((IntPtr)NativeMethods.HWND_BROADCAST, NativeMethods.WM_SHOWME, IntPtr.Zero, IntPtr.Zero);
}
//---------------- in main form
protected override void WndProc(ref Message M_C)
{
if (M_C.Msg == NativeMethods.WM_SHOWME)
{
ShowMe();
}
base.WndProc(ref M_C);
}
//*************
private void ShowMe()
{
if (WindowState == FormWindowState.Minimized)
{
Show();
WindowState = FormWindowState.Normal;
}
// get our current "TopMost" value (ours will always be false though)
bool top = TopMost;
// make our form jump to the top of everything
TopMost = true;
// set it back to whatever it was
TopMost = top;
}
This is already well supported by the .NET Framework. You want to use the WindowsFormsApplicationBase class. Set the IsSingleInstance property to true. You can override the OnStartupNextInstance method to do anything you like when another instance gets started. Like restoring the window of the first instance. Rewrite your Program.cs file to look like this:
using System;
using System.Windows.Forms;
using Microsoft.VisualBasic.ApplicationServices; // Add reference to Microsoft.VisualBasic
namespace WindowsFormsApplication1 {
class Program : WindowsFormsApplicationBase {
[STAThread]
static void Main(string[] args) {
var app = new Program();
app.Run(args);
}
public Program() {
this.IsSingleInstance = true;
this.EnableVisualStyles = true;
this.MainForm = new Form1();
}
protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs) {
if (this.MainForm.WindowState == FormWindowState.Minimized) this.MainForm.WindowState = FormWindowState.Normal;
this.MainForm.Activate();
}
}
}
To add to what Hans Passant wrote, I added an extra method on the main form that handles restoring the window and activating it. This was to wrap the invoke required condition of the form.
So the added method on the form is:
/// <summary>
/// Recovers this instance of the form.
/// </summary>
public void RestoreFromTray()
{
if(this.InvokeRequired)
{
this.Invoke(new Action(RestoreFromTray) );
return;
}
this.Visible = true;
this.WindowState = FormWindowState.Normal;
this.Activate();
}
Then in Hans' method, I changed the override to simply:
protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
{
((formClassName)this.MainForm).RestoreFromTray();
}
Where formClassName is the class name of the form.

Cross-thread cross-form. Display a splash screen with a progress bar

My Solution:
So I managed to find another tutorial http://www.codeproject.com/KB/dotnet/Yet_Another_Splash_Screen.aspx and the sourcecode seemed to make more sense to me. Here is the code i'm using now. Main() is left untouched.
Splash.cs
`
public partial class Frm_Splash : Form
{
delegate void ProgressDelegate(int percent);
delegate void SplashShowCloseDelegate();
/// <summary>
/// To ensure splash screen is closed using the API and not by keyboard or any other things
/// </summary>
bool CloseSplashScreenFlag = false;
/// <summary>
/// Base constructor
/// </summary>
///
public Frm_Splash()
{
InitializeComponent();
progress_Splash.Show();
this.ClientSize = this.BackgroundImage.Size;
}
public void ShowSplashScreen()
{
if (InvokeRequired)
{
// We're not in the UI thread, so we need to call BeginInvoke
BeginInvoke(new SplashShowCloseDelegate(ShowSplashScreen));
return;
}
this.Show();
Application.Run(this);
}
/// <summary>
/// Closes the SplashScreen
/// </summary>
public void CloseSplashScreen()
{
if (InvokeRequired)
{
// We're not in the UI thread, so we need to call BeginInvoke
BeginInvoke(new SplashShowCloseDelegate(CloseSplashScreen));
return;
}
CloseSplashScreenFlag = true;
this.Close();
}
/// <summary>
/// Update text in default green color of success message
/// </summary>
/// <param name="Text">Message</param>
public void Progress(int percent)
{
if (InvokeRequired)
{
// We're not in the UI thread, so we need to call BeginInvoke
BeginInvoke(new ProgressDelegate(Progress), new object[] { percent });
return;
}
// Must be on the UI thread if we've got this far
progress_Splash.Value = percent;
// Fade in the splash screen - looks pro. :D
if (percent < 10)
this.Opacity = this.Opacity + .15;
}
/// <summary>
/// Prevents the closing of form other than by calling the CloseSplashScreen function
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SplashForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (CloseSplashScreenFlag == false)
e.Cancel = true;
}
}`
Form1.cs
public partial class Frm_Main : Form
{
Frm_Splash frm_Splash = new Frm_Splash();
public Frm_Main()
{
this.Hide();
Thread splashthread = new Thread(new ThreadStart(frm_Splash.ShowSplashScreen));
splashthread.IsBackground = true;
splashthread.Start();
InitializeComponent();
CenterToScreen();
}
private void Frm_Main_Load(object sender, EventArgs e)
{
if (PassedAll() == true)
FillMovieLB();
if (FillMovieProgress == 100)
{
//Throw in this sleep so the user can see the progress bar reach all the way to the end.
Thread.Sleep(1000);
this.Show();
frm_Splash.CloseSplashScreen();
this.Activate();
}
}
Original Question
G'day all,
I'm very new to programming in C# and i'm having a problem with the http://www.codeproject.com/KB/cs/prettygoodsplashscreen.aspx tutorial and implementing it within my application. I'm finding it a little difficult to understand what the problem is. I know there is alot of stuff about getting this splash screen to work but I can't get my head around it.
When I start the program, the Frm_Main will display, you can see the listbox being populated, because i've placed it in BackgroundWorker.DoWork(), and then afterwards my frm_Splash will show after the work is done. Obviously, the way it should be working is, frm_Splash will show during the work being done on Frm_Main, and the progress bar will show the progress of the loading (this part I haven't implemented yet).
Edit: I may not have been clear, but the question is: How can I get my splashscreen to display while the work is being done and before the main form is displayed?
Thanks everybody. :)
Here is my code:
static Frm_Splash frm_Splash = new Frm_Splash();
public delegate void ShowFormDelegate();
public void ShowForm()
{
frm_Splash.Show();
}
public Frm_Main()
{
InitializeComponent();
CenterToScreen();
if (PassedAll() == true)
{
back_loadprog.RunWorkerAsync();
}
}
private void back_loadprog_DoWork(object sender, DoWorkEventArgs e)
{
Invoke(new ShowFormDelegate(ShowForm));
Invoke(new FillMovieLBDelegate(FillMovieLB));
}
Here, have some code... Works for me.
Splash Form:
namespace Screens.Forms
{
public partial class Splash : DevExpress.XtraEditors.XtraForm
{
public Splash()
{
InitializeComponent();
}
string RandomLoadingMessage()
{
string[] lines ={
"Pripremam warp pogon",
"Moj drugi ekran za učitavanje je brži, probaj njega",
"Verzija programa koju imam u testiranju imala je smiješnije poruke"
};
return lines[new Random().Next(lines.Length)];
}
public void RandomizeText()
{
lblMessage.Text = RandomLoadingMessage();
}
private void Splash_Load(object sender, EventArgs e)
{
RandomizeText();
}
private static Splash _splash;
private static bool _shouldClose;
static void ThreadFunc()
{
_splash = new Splash();
_splash.Show();
while (!_shouldClose)
{
Application.DoEvents();
Thread.Sleep(100);
if (new Random().Next(1000) < 10)
{
_splash.Invoke(new MethodInvoker(_splash.RandomizeText));
}
}
for (int n = 0; n < 18; n++)
{
Application.DoEvents();
Thread.Sleep(60);
}
if (_splash != null)
{
_splash.Close();
_splash = null;
}
}
static public void ShowSplash()
{
_shouldClose = false;
Thread t = new Thread(ThreadFunc);
t.Priority = ThreadPriority.Lowest;
t.Start();
}
internal static void RemoveSplash()
{
_shouldClose = true;
}
internal static void ShowSplash(List<string> fromTwitterMessages)
{
ShowSplash();
}
}
}
Show it with:
Splash.ShowSplash();
Do the work you need, then when done:
Splash.RemoveSplash();
You need to take this a step further back to your Main() function of the application.
In general you could do this:
Create a ManualResetEvent or better ManualResetEventSlim if you are on .NET 4
Start a new thread displaying your SplashScreen, use Application.Run
In your SplashScreen you should create a time which polls the created ManualResetEvent
frequently, a nice animation could be placed here also
If the event is set you should close the form
Back in your Main() do your stuff like creating forms etc.
When finish set the event, so that the SplashScreen can be closed
To be sure that your MainForm is not shown before your SplashScreen is closed you can use another event

How to make an application HAVE a form but not BE a form?

I want my C# .NET application to have a form but not be a form.
When I normally startup a windows forms application, it's like the form is the master of everything else that follows:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
Instead, I'd like to startup my program, which is then able to show a form, but is not a form itself. In other words, I don't want the master controller of the applicatin being the form, I'd like it instead to be a non-visual logical container, which has the capability to show forms, but isn't a form itself.
I'm not sure if I'm posing the question in a clear way, but I'd like to hear thoughts.
You can just use Application.Run() to get a message-loop running. But you'll need to do something to listen for input - perhaps a systray etc.
You could use an ApplicationContext instead. That gets you the necessary message loop that will keep a form alive, once you decide to create one. Make your Program class look similar to this:
static class Program {
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
AppContext = new ApplicationContext();
Application.Run(AppContext);
}
public static void Quit() {
AppContext.ExitThread();
}
public static ApplicationContext AppContext;
}
Beware that the app will not close automatically when you close the last window. Calling ExitThread explicitly is required.
It's fairly common to create a separate Bootstrapper component which you could move the display of the main form to:
using System;
using System.Windows.Forms;
namespace Example
{
internal static class Program
{
[STAThread]
private static void Main()
{
new Bootstrapper().Run();
}
}
public class Bootstrapper
{
public void Run()
{
// [Application initialization here]
ShowView();
}
private static void ShowView()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
As Mark_Gravell alluded to, Application.Run() blocks until the Form1 closes. You can open your forms on a separate thread, but that thread will be basically consumed by the form. And when you want the exe to exit, you'll have to manually kill each thread. See the following code. (It doesn't create a console window. I got this by creating a default WinForms app and changing the Program class)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApplication1
{
static class Program
{
static List<Thread> threads = new List<Thread>();
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
for (int i = 0; i < 10; i++)
{
StartThread();
System.Threading.Thread.Sleep(500);
}
//kill each thread so the app will exit, otherwise, the app won't close
//until all forms are manually closed...
threads.ForEach(t => t.Abort());
}
static void StartThread()
{
Thread t = new Thread(ShowForm);
threads.Add(t);
t.Start();
}
static void ShowForm()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Create the app as a console app and then call Application.Run as Marc said when you need a form.
You can also create your own ApplicationContext
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(AppController.Instance);
}
And within AppController.cs
namespace MyApplication
{
public class AppController
{
static AppController _AppController;
public LoginWIndow LoginWIndow;
//Constructor
public void AppController()
{
//Do what you will here, Start login form, bind events, w.e :)
if(true) //Your check
{
ShowLoginWindow();
}
}
public void ShowLoginWindow()
{
LoginWIndow = new LoginWIndow();
LoginWIndow.ClosedForm += new FormClosedEventHander(ExitApplication);
LoginWIndow.Show();
}
public void ExitApplication(Object Sender, FormClosedEventArgs Args)
{
//Some shutdown login Logic, then
Application.Exit();
}
static AppController Instance
{
get
{
if(_AppController == null)
{
_AppController = new AppController();
}
return _AppController;
}
}
}
}

Categories

Resources