In Silverlight's OOB mode, restart the process itself killself

Source: Internet
Author: User
Tags silverlight

Silverlight is currently developing applications that want to automatically restart the system after logging off the Sllauncher.exe, the implementation is through the WMI COM interface, get to the current application of the execution command line (CommandLine), and through the shell run; The code is as follows:

#regionUsing sectionusingSystem;usingSystem.Collections.Generic;usingSystem.Runtime.InteropServices.Automation;usingSystem.Windows;#endregionnamespacekillself{ Public classComobjecthelper {Private Staticlist<string>getprocess () {varresult =Newlist<string>(); Try{dynamic Objlocator= Automationfactory.createobject ("WbemScripting.SWbemLocator"); Dynamic objWMIService= Objlocator.connectserver (".","root\\cimv2"); Dynamic Query=objWMIService.ExecQuery ("Select * from Win32_Process"); foreach(Dynamic Oinchquery) {                    //String value = "ExecutablePath =" + O.commandline + "\ r \ n"; //Console.WriteLine (value);result. ADD (O.commandline+""); }                returnresult; }            Catch(Exception ex) {MessageBox.Show ("Get Process:"+Ex.            Message); }            return NULL; }        Internal Static voidExec (stringcmdline) {            Try{dynamic cmd= Automationfactory.createobject ("Wscript.Shell"); Cmd. Run (CmdLine,1,false); }            Catch(Exception ex) {MessageBox.Show ("Execute Process:"+Ex.            Message); }        }         Public Staticslprocess getself () {string[] procs =getprocess ().            ToArray (); foreach(stringPinchprocs) {                stringCmdLine =P.tolower (); if(CmdLine. IndexOf ("Sllauncher.exe", stringcomparison.ordinal)! =-1)                {                    return Newslprocess (cmdline); }            }            Throw NewNullReferenceException ("SLLauncher.exe process not found"); }    }     Public classslprocess { PublicSlprocess (stringcmdline) {CommandLine=CmdLine; }         Public stringCommandLine {Get;Set; }  Publicslprocess Run () {comobjecthelper.exec (CommandLine); return  This; }         Publicslprocess Kill () {Application.Current.MainWindow.Close (); return  This; }    }}

Invocation part:

?
1 2 3 4 5 6 7 8 9 10 private void Button_Click(object sender, RoutedEventArgs e) {     if (!Application.Current.IsRunningOutOfBrowser)     {         MessageBox.Show("Not Running OutOfBrowser!!");         return;     }         ComObjectHelper.GetSelf().Run().Kill(); }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.