|
The function "IsRunning" will check if the windows service is running.
I think that when creating a windows service just like creating a application, so I do not know how to create it programatically. We must set the windows service before we release it.
|
|
|
|
|
I feel that using a socet connection between service and application is not a good idea because this connection can be terminated by firewall etc
ps use logging instead
|
|
|
|
|
I.C.
But, just remind once.
I know little about the Logging, I will have some research about that.
|
|
|
|
|
A Windows service almost never has access to the desktop. See this[^] article.
/ravi
|
|
|
|
|
Yes, it can not instantiation a win form and show it.
But I think that the socket could connect the win service and win app.
|
|
|
|
|
To capture Keyboard stream use hooks
Read this http://msdn.microsoft.com/en-us/library/ms632589(VS.85).aspx[^]
Simple example
<br />
using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
using System.Diagnostics;<br />
using System.Runtime.InteropServices;<br />
<br />
namespace hook<br />
{<br />
class Program<br />
{<br />
private const int WH_KEYBOARD_LL = 13;<br />
private const int WM_KEYDOWN = 0x0100;<br />
private static LowLevelKeyboardProc _proc = HookCallback;<br />
private static IntPtr _hookID = IntPtr.Zero;<br />
<br />
static void Main()<br />
{<br />
_hookID = SetHook(_proc);<br />
Application.Run();<br />
UnhookWindowsHookEx(_hookID);<br />
<br />
}<br />
<br />
private static IntPtr SetHook(LowLevelKeyboardProc proc)<br />
{<br />
using (Process curProcess = Process.GetCurrentProcess())<br />
using (ProcessModule curModule = curProcess.MainModule)<br />
{<br />
return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);<br />
}<br />
}<br />
<br />
private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);<br />
<br />
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)<br />
{<br />
if ((nCode >= 0) && (wParam == (IntPtr)WM_KEYDOWN))<br />
{<br />
int vkCode = Marshal.ReadInt32(lParam);<br />
if (((Keys)vkCode == Keys.LWin) || ((Keys)vkCode == Keys.RWin))<br />
{<br />
Console.WriteLine("{0} blocked!", (Keys)vkCode);<br />
return (IntPtr)1;<br />
}<br />
}<br />
return CallNextHookEx(_hookID, nCode, wParam, lParam);<br />
}<br />
<br />
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]<br />
private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);<br />
<br />
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]<br />
private static extern bool UnhookWindowsHookEx(IntPtr hhk);<br />
<br />
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]<br />
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);<br />
<br />
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]<br />
private static extern IntPtr GetModuleHandle(string lpModuleName);<br />
<br />
<br />
<br />
}<br />
}<br />
<br />
|
|
|
|
|
how do i use this code? where do i put in? in the program.cs ?
and is it a windows service?
|
|
|
|
|
Dear All,
I have two DataTables. The first (FirstDataTable) is filled by a query and the second is filled by:
SecondDataTable= FirstDataTable;
after this equal operation, the two DataTables are connected and any change is reflected between the both.
I mean when you change the data in the FirstDataTable by a different query, this change will be reflected to the SecondDataTable automatically.
The question here: How can I stop or reject the connection between the two DataTables.
I mean I do not want to reflect the changes after the equal operation. this is to make a copy of the first data returned by the first run of the query.
Regards.
Kind Regards
OBarahmeh
|
|
|
|
|
obarahmeh wrote: SecondDataTable= FirstDataTable;
This means both tables are referring to the same object in memory. What you can do is, create a deep copy of the FirstDataTable. You can find a lot of examples on web for doing this.
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
|
|
|
|
|
Hello,
I have a barcode reader attached to my computer using USB (specific port).
I want my program to run in the background and once the barcode reader read something (8 chars code), this code will be inserted into clipboard.
I know to insert string into the clipboard but how can i check the interupt from the barcode reader (and only it) and only then the program will insert the code to the clipboard, otherwise it won't do nothing.
How can i do it?
|
|
|
|
|
You need to use SerialPort component and the USB specifications (baud rate, stop bit and parity).
A good start would be here.
Cheer's,
Alex Manolescu
|
|
|
|
|
Hi,
I'm gonna get a SQL backup with below method :
void BackupDatabase(string sConnect, string dbName, string backUpPath)
{
using (SqlConnection cnn = new SqlConnection(sConnect))
{
cnn.Open();
dbName = cnn.Database.ToString();
ServerConnection sc = new ServerConnection(cnn);
Server sv = new Server(sc);
BackupDeviceItem bdi = new BackupDeviceItem(backUpPath, DeviceType.File);
Microsoft.SqlServer.Management.Smo.Backup bk = new Backup();
bk.PercentComplete += new PercentCompleteEventHandler(percentComplete);
bk.Devices.Add(bdi);
bk.Action = BackupActionType.Database;
bk.PercentCompleteNotification = 1;
bk.BackupSetDescription = dbName;
bk.BackupSetName = dbName;
bk.Database = dbName;
bk.LogTruncation = BackupTruncateLogType.Truncate;
bk.FormatMedia = false;
bk.Initialize = true;
bk.Checksum = true;
bk.ContinueAfterError = true;
bk.Incremental = false;
bk.SqlBackup(sv);
}
}
But an exception has occurred :
Microsoft.SqlServer.Management.Smo.FailedOperationException: Backup failed for Server '\\.\pipe\3F103E6E-3FD4-47\tsql\query'
additional info about the Exception:
{Microsoft.SqlServer.Management.Smo.FailedOperationException: Backup failed for Server '\\.\pipe\3F103E6E-3FD4-47\tsql\query'. ---> System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.SqlServer.BatchParser, Version=9.0.242.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. The system cannot find the file specified.
File name: 'Microsoft.SqlServer.BatchParser, Version=9.0.242.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91'
at Microsoft.SqlServer.Management.Common.ServerConnection.GetStatements(String query, ExecutionTypes executionType, Int32& statementsToReverse)
at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand, ExecutionTypes executionType)
at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(StringCollection sqlCommands, ExecutionTypes executionType)
at Microsoft.SqlServer.Management.Smo.ExecutionManager.ExecuteNonQuery(StringCollection queries)
at Microsoft.SqlServer.Management.Smo.ExecutionManager.ExecuteNonQueryWithMessage(StringCollection queries, ServerMessageEventHandler dbccMessageHandler, Boolean errorsAsMessages)
at Microsoft.SqlServer.Management.Smo.BackupRestoreBase.ExecuteSql(Server server, StringCollection queries)
at Microsoft.SqlServer.Management.Smo.Backup.SqlBackup(Server srv)
=== Pre-bind state information ===
LOG: User = MDS-PC\MDS
LOG: DisplayName = Microsoft.SqlServer.BatchParser, Version=9.0.242.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91
(Fully-specified)
LOG: Appbase = file:LOG: Initial PrivatePath = NULL
Calling assembly : Microsoft.SqlServer.ConnectionInfo, Version=9.0.242.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: D:\My Works\C#\Win Form\Reza Restaurant\RezaRestaurant\bin\Release\RezaRestaurant.vshost.exe.Config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v2.0.50727\config\machine.config.
LOG: Post-policy reference: Microsoft.SqlServer.BatchParser, Version=9.0.242.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91
LOG: Attempting download of new URL file:LOG: Attempting download of new URL file:LOG: Attempting download of new URL file:LOG: Attempting download of new URL file:
--- End of inner exception stack trace ---
at Microsoft.SqlServer.Management.Smo.Backup.SqlBackup(Server srv)
at RezaRestaurant.Form_تنظیمات_نرم_افزار.BackupDatabase(String sConnect, String dbName, String backUpPath) in D:\My Works\C#\Win Form\Reza Restaurant\RezaRestaurant\Forms\تنظیمات_نرم_افزار.cs:line 260
at RezaRestaurant.Form_تنظیمات_نرم_افزار.button_تهیه_نسخه_پشتیبان_Click(Object sender, EventArgs e) in D:\My Works\C#\Win Form\Reza Restaurant\RezaRestaurant\Forms\تنظیمات_نرم_افزار.cs:line 177}
Would you please guide me ?
Thanks.
|
|
|
|
|
Check if you have the package installed in the computer. In case it is not, you will need to install it. You can get the package from MSDN.
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
|
|
|
|
|
Do you mean here[^] ? if so, I installed
Microsoft SQL Server 2008 Management Objects
X64 Package (SharedManagementObjects.msi) - 3895 KB
But I receive the exception again.
|
|
|
|
|
SQLServer2005_XMO.msi does that for SQL Server 2005 32-bit. I am not sure about SQL 2008. Probably you can search for similar file for SQL 2008. Also, once you install the package, check for the dll at the following path:
c:\Program Files\Microsoft SQL Server\100\SDK\Assemblies
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
|
|
|
|
|
|
Check the SQL version. In the previous post of yours, you gave link to file for SQL 2008 and now for SQL 2005. Just make sure you have correct files.
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
|
|
|
|
|
Actually I have been installed VS2008 and as I know it installed SQLExpress2008,
d@nish wrote: Just make sure you have correct files.
I installed it and the exception haven't occurred anymore, now the method works well.
|
|
|
|
|
I want to create a new windows service which supposed to send mails every hour.
I know how to send an email message, but I don't know how I can make sure that it happens every hour (what code I should write, where I should place it). Someone can help me?
|
|
|
|
|
You can make use of System.Timers.Timer . Enable it in the OnStart method of your service and set its interval as 1 hour. Then, handle its Elapsed event and perform email sending there.
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
|
|
|
|
|
I voted this as a good answer even though you recommended the use of a timer object instead of using a thread of some kind.
Timer events are the lowest priority message in Windows, and on a busy system, the message is not guaranteed to be sent, much less retrieved.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
That can be a good thing.
|
|
|
|
|
Thanks.
I am not sure how I would employ threads for this. (Is it suspend/start kind of thing that you are referring).
John Simmons / outlaw programmer wrote: Timer events are the lowest priority message in Windows, and on a busy system, the message is not guaranteed to be sent, much less retrieved.
IMHO when you are sending mails, you should not be expected to make it happen instantly. Reason being even though your code works perfect, there is always a chance of email server not being upright (unless you have your own and you guarantee it will always work). So a couple of seconds or even a minute or so should not trouble. I also believe that threads can actually mess up the whole application if not used sensibly and accurately. And IMHO when application goes live, only then one can check how well he has handled threads (just an opinion).
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
|
|
|
|
|
d@nish wrote: I am not sure how I would employ threads for this. (Is it suspend/start kind of thing that you are referring).
Just use a BackgroundWorker that sits and spins. It can't wait more than 4 seconds before cycling (in a loop) because task manager will shut a windows service down that looks like its not active. It can be canceled, so there's no problem with running it in a service. I abhor the use of timers, and I view them with the same disdain I reserve for goto statements.
d@nish wrote: IMHO when you are sending mails, you should not be expected to make it happen instantly. Reason being even though your code works perfect, there is always a chance of email server not being upright (unless you have your own and you guarantee it will always work). So a couple of seconds or even a minute or so should not trouble. I also believe that threads can actually mess up the whole application if not used sensibly and accurately. And IMHO when application goes live, only then one can check how well he has handled threads (just an opinion).
I don't understand your concern. Once an hour you send an email. Period. Whether or not the SMTP server is available and whether or not you even need to care depends on the environment. If the server isn't on your local domain, there's nothing you can do except send the email and hope for the best. It has nothing to do with the threading model you chose to implement the cycle.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
John Simmons / outlaw programmer wrote: Just use a BackgroundWorker that sits and spins.
I don't believe a BackgroundWorker can be used in a Windows service. It's designed to be used in combination with a UI thread.
/ravi
|
|
|
|
|