4

I'm creating some desktop application using WPF(C# .net). I want to create it as computer startup application. I can do it add shortcut link to startup folder.

My question is, I want to do startup this application after one minute in computer start.How to do this one?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
hmlasnk
  • 1,160
  • 1
  • 14
  • 33
  • I don't think you'll benefit much from adding a delay mechanism in your code. Any .NET application will still have wait for the program, framework and other dependencies to be loaded into memory just to get started. It will probably slow down start up times in the end, and that doesn't include actually running your code yet. You're better off writing a script to launch your application after some delay or using the task scheduler to execute your program after a delayed start. – Jeff Mercado Sep 17 '11 at 06:43
  • Thanks for the comment. My application run when get internet connection.so I want to run my application after about one minutes on computer.can you help me? – hmlasnk Sep 17 '11 at 07:50
  • So the goal of the delay is to wait until you have a working internet connection rather than loading at startup in a reasonable amount of time? In that case, it might just be easier to just have a polling loop and just attempt to connect to some known internet server prior to really starting your code. If you can connect, then you can just continue on with your code, otherwise try again until you can (and quit after a certain amount of time if necessary). If you'd rather not do that, then my advice to use a script or the task scheduler would be your best shot. – Jeff Mercado Sep 17 '11 at 07:59
  • Your app needs to be resilient to the connection not being available. Waiting an arbitrary amount of time in the off chance that the connection will be available is the wrong solution. Startup immediately and make your app resilient to connection outages. – David Heffernan Sep 17 '11 at 08:25

2 Answers2

4

If really you want to know if an internet connection is available, maybe it is better to check that, rather than delay for an arbitrary amount of time.

See check whether Internet connection is available with C#.

tsimbalar
  • 5,790
  • 6
  • 37
  • 61
3

The easiest solution would be to put a call to Thread.Sleep() when your application starts up. As you mentioned in a comment, you don't want the delay to always happen - only when the application is launched when the computer first starts. To accomplish this, the code can look for a command line argument that specifies that it's running as part of the computer starting. If you see that command line argument, you do the sleep. Otherwise, you can just start right away. When you make the shortcut that goes in the Startup folder, pass some special token....in my example, I use the string "startup".

In your App.xaml.cs file, you can put code like this:

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        //If a command line argument was passed and it was some special string,
        //   then we want to do the sleep.  Otherwise, we don't sleep and just
        //   continue on right away.
        if (e.Args.Length == 1 && e.Args[0] == "startup")
        {
            //Sleep for 60 seconds
            System.Threading.Thread.Sleep(60 * 1000);
        }

        //Continue on...
        base.OnStartup(e);
    }
}

That way, you first window won't be created for 60 seconds. The application still starts right away but effectively doesn't do anything right away. If that is good enough for you, I think this would be the best solution.

If you're trying to not even launch your application for 60 seconds (perhaps to avoid the overhead of loading your .exe in memory plus whatever loading is done for the CLR), you could consider writing a simple script file (perhaps a batch file, etc.) that pauses for 60 seconds and then executes your program. Then put that script file in the startup folder instead of your application.

Stephen McDaniel
  • 2,938
  • 2
  • 24
  • 53
  • Thanks for your reply. But i want to do delay run in my application when only computer start up. Other times not should delay. So this solution not good for me. Because every time delay 1 minute from this solution. Can you help me? – hmlasnk Sep 17 '11 at 07:47
  • Oh, I didn't consider that. But it's easy enough to only do the delay when the app is started from the shortcut in the Startup folder. I've edited my answer to address this. – Stephen McDaniel Sep 17 '11 at 07:54