How to autostart windows services in C# - c#

I already made a windows service that should autostart when Windows starts up, but for some reason It does not work. I used the code below:
private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
using (ServiceController sc = new ServiceController(serviceInstaller1.ServiceName))
{
sc.Start();
}
}
After install the service using InstallUtil.exe it starts automatically, but if I make a restart it does not start even when the configuration in the Service Manager is "Automatic".
I already tried changing for "Automatic (Delayed Start) " but nothing changed.
I will appreciate some advice.
Sorry for my poor english, I'm not a native.
Thanks

namespace curUsers
{
[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
public ProjectInstaller()
{
var processInstaller = new ServiceProcessInstaller();
var serviceInstaller = new ServiceInstaller();
//set the privileges
processInstaller.Account = ServiceAccount.LocalSystem;
serviceInstaller.DisplayName = "curUsers";
serviceInstaller.StartType = ServiceStartMode.Automatic;
//must be the same as what was set in Program's constructor
serviceInstaller.ServiceName = "curUsers";
this.Installers.Add(processInstaller);
this.Installers.Add(serviceInstaller);
}
private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
}
private void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
}
}
}
Just try this, all of my windows services are developed in the same way. this one also works well.

I built several windows services a while back. Maybe this will help solve your issue
// serviceInstaller1
//
this.serviceInstaller1.ServiceName = "whoisthere";
this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
this.serviceInstaller1.AfterInstall += new System.Configuration.Install.InstallEventHandler(this.serviceInstaller1_AfterInstall);

Related

Xamarin Forms Bluetooth LE not showing nearby discoverable devices

I am making an app that utilizes bluetooth function such as scanning devices etc. I checked the scan flag and returns true but not showing the discoverable device that I am testing.
I am using Samsung J7 Pro as my app test device and Samsung J7 as the device I want to see in the list of discovered devices.
J7 already set as discoverable and with bluetooth ON.
I based my codes in Monkey.BluetoothLE
Here is what I have:
Declarations
ObservableCollection<BluetoothViewModel> vm = new ObservableCollection<BluetoothViewModel>();
Android.Bluetooth.BluetoothManager _blManager;
Android.Bluetooth.BluetoothManager _blManager;
Robotics.Mobile.Core.Bluetooth.LE.Adapter _bleAdapter;
Functions
public BluetoothPage()
{
InitializeComponent();
lvInfo.ItemsSource = vm;
var appContext = Android.App.Application.Context;
_blManager = (Android.Bluetooth.BluetoothManager)appContext.GetSystemService("bluetooth");
_blAdapter = _blManager.Adapter;
_bleAdapter = new Robotics.Mobile.Core.Bluetooth.LE.Adapter();
_bleAdapter.DeviceDiscovered += _bleAdapter_DeviceDiscovered;
_bleAdapter.ScanTimeoutElapsed += _bleAdapter_ScanTimeoutElapsed;
}
private void btnScanStopBluetooth_Click(object sender, EventArgs e)
{
if (!_bleAdapter.IsScanning)
{
if (!_blAdapter.IsEnabled)
{
_blAdapter.Enable();
DisplayInformation("Turning on bluetooth...");
while (!_blAdapter.IsEnabled)
{
//do nothing until enabled
}
}
vm.Clear();
btnScan.Text = "Stop Scan";
_bleAdapter.StartScanningForDevices();
}
else
{
btnScan.Text = "Start Scan";
_bleAdapter.StopScanningForDevices();
}
}
private void _bleAdapter_DeviceDiscovered(object sender, Robotics.Mobile.Core.Bluetooth.LE.DeviceDiscoveredEventArgs e)
{
count++;
vm.Add(new BluetoothViewModel
{
Name = e.Device.Name,
ID = e.Device.ID.ToString(),
RSSI = e.Device.Rssi.ToString()
});
}
private void _bleAdapter_ScanTimeoutElapsed(object sender, EventArgs e)
{
DisplayInformation("Scan Timeout");
_bleAdapter.StopScanningForDevices();
btnScan.Text = "Start Scan";
}
private void DisplayInformation(string line)
{
lblStatus.Text = line;
}
A listview is bound to "vm" that will display the discovered device.
It does not show anything, and count is always zero but I checked the scan flag using _bleAdapter.IsScanning, it returns true.
EDIT:
I tried other open-source sample programs for Bluetooth such as
xamarin-bluetooth-le (BLE Explorer)
Bluetooth-Xamarin.Forms (DemoBluetooth)
None of them seem to list the device. When I use my built-in bluetooth app under settings, it lists the device. What am I missing here?
Have you granted permission for bluetooth and location?
You have to grant permission in the Manifest/or Settings and depending on the sdk (23+) also asking the user for extra permission.
https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/permissions?tabs=windows

My windows phone 8 app UI stops responding after using the mediaplayer class to play a song from my phone library, How can I fix this?

My code snippet below:
using (library = new MediaLibrary())
{
MediaPlayer.Play(library.Songs, 0);
}
and then my app user Interface is no longer responsive after playing the song. What can I do to fix this?
I found a solution, I used a dispatcher timer class like this:
private DispatcherTimer m_dispatcherTimer;
public MainPage()
{
InitializeComponent();
m_dispatcherTimer = new DispatcherTimer();
m_dispatcherTimer.Interval = TimeSpan.FromTicks(10000);
m_dispatcherTimer.Tick += frameworkDispatcherTimer_Tick;
m_dispatcherTimer.Start();
using (library = new MediaLibrary())
{
MediaPlayer.Play(library.Songs, 0);
}
}
void frameworkDispatcherTimer_Tick(object sender, EventArgs e)
{
FrameworkDispatcher.Update();
}
and everything worked just fine. ;)

What is an application adapters for hosted application in CCA?

I am new to Microsoft CRM CCA . Currently i am faceing some problems .
i created an winform and hosted it in my Agent Desktop . The winform is supposed to show the contents of a notepad in the winform's text area . How to achieve it ? I have no clue at all as there is not a much documentation on this topic . .....Plz help me out here .
here you go the complete code
using System;
using Microsoft.Uii.Csr;
namespace Microsoft.Uii.QuickStarts
{
public partial class QsHostedControl : HostedControl
{
public QsHostedControl()
{
InitializeComponent();
}
// Necessary constructor
public QsHostedControl(Guid appID, string appName, string initString)
: base(appID, appName, initString)
{
InitializeComponent();
}
private void QSHostedControl_Load(object sender, EventArgs e) {}
// This is the context change event handler.
public override void NotifyContextChange(Context context)
{
// This is the context change handler.
// Populating text fields from context information.
txtFirstName.Text = context["CustomerFirstName"];
txtLastName.Text = context["CustomerLastName"];
txtAddress.Text = context["Street"];
txtID.Text = context["CustomerID"];
// Hands control back over to the base class to notify next app of context change.
base.NotifyContextChange(context);
}
protected override void DoAction(RequestActionEventArgs args)
{
//Check the action name to see if it's something we know how to handle and perform appropriate work
switch (args.Action)
{
case "UpdateFirstName":
txtFirstName.Text = args.Data;
break;
case "UpdateLastName":
txtLastName.Text = args.Data;
break;
}
}
private void updateData_Click(object sender, EventArgs e)
{
// This is how you fire an action to other hosted applications. Your DoAction() code
// in your other application or application adapter will get called via this.
FireRequestAction(new RequestActionEventArgs("QSExternalApplication", "UpdateFirstName", txtFirstName.Text));
FireRequestAction(new RequestActionEventArgs("QSExternalApplication", "UpdateLastName", txtLastName.Text));
FireRequestAction(new RequestActionEventArgs("QSWebApplication", "UpdateFirstName", txtFirstName.Text));
FireRequestAction(new RequestActionEventArgs("QSWebApplication", "UpdateLastName", txtLastName.Text));
FireRequestAction(new RequestActionEventArgs("QSWebApplication", "UpdateAddress", txtAddress.Text));
FireRequestAction(new RequestActionEventArgs("QSWebApplication", "UpdateID", txtID.Text));
}
private void btnFireContextChange_Click(object sender, EventArgs e)
{
// Get the current context and create a new context object from it.
string temp = Context.GetContext();
Context updatedContext = new Context(temp);
// Update the new context with the changed information.
updatedContext["CustomerFirstName"] = txtFirstName.Text;
updatedContext["CustomerLastName"] = txtLastName.Text;
// Notify everyone of this new context information
FireChangeContext(new ContextEventArgs(updatedContext));
// Tell self about this change
NotifyContextChange(updatedContext);
}
}
}
you can find it in sdk also
If you wan to create an hosted application with an adapter then yoou have to use AIF(application intregation frame work) yOU CAN CHECK OUT THIS LINK hosted control
and application adapter

Bring up keyboard in C# for Windows 8 tablet

I am working on a UI for a Windows 8.1 tablet, which has a full version of Windows on it. There is a keyboard icon at the bottom of windows 8.1, which brings up a keyboard, and I want that to automatically trigger after clicking a numericUpDown box. I then would also like it to close after leaving or clicking off of the box.
I am basically just trying to focus it when it is clicked, but this does not seem to bring up the keyboard. Also, note, I am setting some other numericUpDown box to the one in the function so I can call it outside, so I hope that doesn't make it difficult to see what's going on, let me know if you need any clarifications and thank you for the help. Here is what I have so far:
copiedNUD.Click += CopiedNudPass_Focus;
//copy copied nud
CopiedNudPass = copiedNUD;
...
void CopiedNudPass_Focus(object sender, EventArgs e)
{
CopiedNudPass.Focus();
}
I tried looking around a bit, but some of the solutions weren't too clear to me. I really appreciate the help. Thank you.
I figured it out. Here is my code specifically for a tablet with window 8 or higher:
copiedNUD.Click += CopiedNudPass_Focus;
//copy copied nud
CopiedNudPass = copiedNUD;
...
//Launch keyboard
void CopiedNudPass_Focus(object sender, EventArgs e)
{
Version win8version = new Version(6, 2, 9200, 0);
if (Environment.OSVersion.Version >= win8version)
{
string progFiles = #"C:\Program Files\Common Files\Microsoft Shared\ink";
string keyboardPath = Path.Combine(progFiles, "TabTip.exe");
Process.Start(keyboardPath);
}
}
//Close keyboard
void CopiedNudPass_LostFocus(object sender, EventArgs e)
{
Version win8version = new Version(6, 2, 9200, 0);
if (Environment.OSVersion.Version >= win8version)
{
Process[] oskProcessArray = Process.GetProcessesByName("TabTip");
foreach (Process onscreenProcess in oskProcessArray)
{
onscreenProcess.Kill();
}
Refresh();
}
}
My only problem right now is that when the keyboard closes my main form in the background gets cut off and I tried to refresh it using Refresh(); , but that did not seem to work :(.
Here is a better closing function:
After killing the process for TabletKeyboard(TabTip.exe) application doesn't bring back to its original size in wpf
Here is my new close code:
//Close keyboard
void CopiedNudPass_LostFocus(object sender, EventArgs e)
{
Version win8version = new Version(6, 2, 9200, 0);
if (Environment.OSVersion.Version >= win8version)
{
uint WM_SYSCOMMAND = 274;
uint SC_CLOSE = 61536;
IntPtr KeyboardWnd = FindWindow("IPTip_Main_Window", null);
PostMessage(KeyboardWnd.ToInt32(), WM_SYSCOMMAND, (int)SC_CLOSE, 0);
}
}
I also had to add a reference to WindowsBase and add external functions to the project. The steps and additional code are in the url I linked to in this post. Here's how you add a reference for WindowsBase to get using System.Windows.Interop; to work:
Right click on project
Highlight Add and click Reference
Ensure you have Framework selected under Assemblies
Scroll down and check in "WindowsBase" and hit ok
Add using System.Windows.Interop; at the top of your code and your done
This worked for me (in Java & eclipse RCP)
text.addFocusListener(new FocusListener()
{
#Override
public void focusLost(FocusEvent arg0)
{
LogUtil.logInfo("Closing OSK");
try
{
if(Settings.getBoolean(Settings.OSK_USETABTIP)) {
Runtime.getRuntime().exec("cmd /c taskkill /IM tabtip.exe");
} else {
Runtime.getRuntime().exec("cmd /c taskkill /IM osk.exe");
}
}
catch (IOException e)
{
LogUtil.logError(e.toString());
}
}
#Override
public void focusGained(FocusEvent arg0)
{
try
{
String sysroot = System.getenv("SystemRoot");
if(Settings.getBoolean(Settings.OSK_USETABTIP)) {
LogUtil.logInfo("Opening TabTip");
ProcessBuilder pb = new ProcessBuilder("C:/pathtotabtip/tabtip.exe");
pb.start();
} else {
LogUtil.logInfo("Opening OSK");
ProcessBuilder pb = new ProcessBuilder(sysroot + "/system32/osk.exe");
pb.start();
}
}
catch (Exception e)
{
LogUtil.logError(e.toString());
}
}
});

Automatically starting a service when the installer finishes [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to automatically start your service after install?
I have a Visual Studio 2008 C# .NET 3.5 service installer project (MSI) running on Windows 7 x64.
I subscribe to the ServiceInstaller.OnAfterInstall notification to start my service when the installation finishes.
[RunInstaller(true)]
public partial class MyInstaller : Installer
{
private System.ServiceProcess.ServiceInstaller my_installer_;
private void InitializeComponent()
{
// ...
this.my_installer_.AfterInstall += new System.Configuration.Install.InstallEventHandler(this.OnAfterInstall);
// ...
}
private void OnAfterInstall(object sender, InstallEventArgs e)
{
using (System.ServiceProcess.ServiceController svc =
new System.ServiceProcess.ServiceController("MyService"))
{
svc.Start(); // completes successfully
}
}
}
Though the function succeeds without exception, my service is never running when the installer finishes.
The event log shows no failures related to service startup and if I go to the services manager, I can start the service manually (or restart the PC and it will start automatically).
What do I need to do to automatically start my service when the installer process finishes?
Using AfterInstall event
Create AfterInstall event in your Service Installer class and start service using ServiceController.
public ServiceInstaller()
{
InitializeComponent();
this.AfterInstall += new InstallEventHandler(ServiceInstaller_AfterInstall);
}
void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e)
{
ServiceController sc = new ServiceController(serviceInstaller1.ServiceName);
sc.Start();
}
Using Committed event
public ServiceInstaller()
{
InitializeComponent();
this.Committed += new InstallEventHandler(ProjectInstaller_Committed);
}
void ProjectInstaller_Committed(object sender, InstallEventArgs e)
{
ServiceController sc = new ServiceController(serviceInstaller1.ServiceName);
sc.Start();
}
Or you can override OnCommitted event
protected override void OnCommitted(System.Collections.IDictionary savedState)
{
base.OnCommitted(savedState);
new ServiceController(serviceInstaller1.ServiceName).Start();
}
Other than above please check following
Installer Start type : Automatic
Account :Local System
Other than the service installer you need to have setup project which created by giving primary output of above service installer.
in the setup create Custom action at least on install by giving service installer project output.
More information from here.
I assume that Start returns immediatly, and Starts the Service in the background. Check the Docs: http://msdn.microsoft.com/en-us/library/yb9w7ytd.aspx

Categories

Resources