|
If you want to spend your time on more "productive" things, then you should look at the MS Enterprise Library (free) which already includes tried and proven application blocks (ie. classes) for Logging, Error Handling, and other "common" infrastructure code blocks. Get the Hands - On Labs for a quick and painless way to using these blocks.
|
|
|
|
|
I am getting problem when i launch a UI Application from windows Service Application.
My requirement is while starting up of the service i need to run a UI Application.for Example i have taken notepad.exe.
The below code is the code i have implemented to run the process.
Process p = new Process();
p.StartInfo.FileName = @"C:\WINDOWS\system32\notepad.exe";
bool bretval = p.Start();
Problem here is: It is running the process(notepad.exe) in background.
I am not able to see it.
I have gone through several Articles and i felt that WINDOWS service
will supress the UI. Is there any Work around or any other Option so that
my requirement can be obtained.
Pls help me in this regard.
|
|
|
|
|
You can try to activate "interact with desktop" for your service.
However on Vista or later this flag become obsolete.
Generally a service should not have to do anything with UI, because they also can run if nobody is logged in.
Greetings
Covean
|
|
|
|
|
I Thank you for your awesome reply. It helped me and it worked out.
As u told that this option cannot be implemented in Vista.
I will tell you my requirement which is, I have a USB Device, when ever it is plugged in i need to Open my Application(.exe). It is just like iTunes Application for iPod. I have code for Detecting my USB Device. So i thought of implementing the device detecting code in windows service and if the device is found i can launch my application.
Can you tell me any work around or any other way to Implement this senario so that it will work even in Vista.
Regards,
Prasad.
|
|
|
|
|
I would try the following way.
In your service handler function wait for the SERVICE_CONTROL_SESSIONCHANGE event.
(You have to say the system that your service wants this notification (SetServiceStatus with SERVICE_ACCEPT_SESSIONCHANGE)).
After you received the SERVICE_CONTROL_SESSIONCHANGE (WTS_SESSION_LOGON) event, get the users new session id from the WTSSESSION_NOTIFICATION structure and "save" it.
If you want to start the new process you have to do the following steps.
Use WTSQueryUserToken to get the primary access token. Now try to acquire TOKEN_QUERY, TOKEN_DUPLICATE, and TOKEN_ASSIGN_PRIMARY for this token by using SetTokenInformation.
Last Step: CreateProcessAsUser to start your process.
I'm short in time so I couldn't test it but I think this should do the work.
Greetings
Covean
|
|
|
|
|
Thank you Covean,
Sorry,I could not completely Implement the suggestion given by you.
Here is what i implemented.
PROCESS_INFORMATION pi;
STARTUPINFO si =new STARTUPINFO();
System.IntPtr hToken;
string commandLine, userPart, domainPart, password, lpCurrentDirectory;
// Initialize structs
si.cb = Marshal.SizeOf(si);
userPart = "Prasad";
password = "pass";
domainPart = "";
commandLine = "";
lpCurrentDirectory = @"D:\DemoApplication\bin\Debug";
// Get the user token
if (NativeWin32Methods.LogonUser(userPart, domainPart, password, (int)LOGON_TYPE.LOGON32_LOGON_BATCH, (int)LOGON_PROVIDER.LOGON32_PROVIDER_DEFAULT, out hToken))
{
// Create structs
SECURITY_ATTRIBUTES saProcessAttributes = new SECURITY_ATTRIBUTES();
SECURITY_ATTRIBUTES saThreadAttributes = new SECURITY_ATTRIBUTES();
// Now create the process as the user
if (!NativeWin32Methods.CreateProcessAsUser(hToken, appPath, commandLine,
ref saProcessAttributes, ref saThreadAttributes, false, 0, IntPtr.Zero, lpCurrentDirectory, ref si, out pi))
{
// Throw exception
throw new Exception("Failed to CreateProcessAsUser");
}
}
If you can tell me the modified one i would be very much thank ful to you.
Prasad.
|
|
|
|
|
In your example code I miss the SetTokenInformation part.
Thats all I can say at the first look.
I also received your e-Mail and I will answer if I have the time for (I hope this will be tommorrow ).
Greetings
Covean
|
|
|
|
|
Hi everyone
(I hope this is the appropriate place to ask this)
Performance Counter Instances, is there some way to persist them? Can I set it up in some way so that (if need be) a certain service/process can keep them alive even if all the other services are stopped/dead? Currently, as soon as the counters doesn't perform read/write operations to those Counter Instances within a certain time or if the Process/Service that used them closes, then they no longer exist.
The reason why I need instances is that we need to associated multiple "Entities" to the same kind of Counters, i.e. to know if "Entity A" made a number of Authorizations Per Second, and how much "Entity B" did, etc. which all fall under a Counter "C" (for argument sake).
Now using Instances seems like the way to go but because of the problems mentioned above, it doesn't seem viable.
So the only way we currently can solve this is to recreate the Category and create new Counters which implies a desired "Entity" (which will be expensive and more complicated in the end).
Unfortunately I no longer have the time to explore this further and I have spent a whole lot of time on Performance Counter research. Is there someone who could be able to assist?
Thanks
- Last Attacker
|
|
|
|
|
Hi All,
first i would like to say that i'm new to c# so patience will be appreciated
i have a window application that display 3 graphs and a thread that do some other stuff.
the main looks like that:
static class Program
{
static void Main(){
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault( false );
Thread t = new Thread(new ThreadStart(new
SpecialEventQueueHendlerThread().threadRun));
t.Start();
Application.Run( new Form1() );
t.Abort();
}
}
i noticed that if i put a breakpoint in the thread code the graphs also stop to be updated although it runs in a different thread (the main thread).
my question is:
with no breakpoints, does the graph will not be updated while the thread code is executed?
and if this is the case then what is the meaning of thread-safe if they are not run in paralel mode anyway?
Thanks,
Tomer.
|
|
|
|
|
Hi,
1.
most if not all debuggers, when they stop one thread (e.g. due to a breakpoint) also stop all the other threads in that process; otherwise you would be looking at a moving target, with other threads continuously changing some of the data, making it almost impossible to understand what is going on.
2.
if your "other thread" is not related to anything in the GUI, all is fine. Both will be able to run concurrently, which means Windows will switch from one to the other at any time (and actually can execute both in parallel on a multi-core CPU).
if your other thread wants to touch GUI Controls, you need to use Invoke, see here[^].
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
Hi Luc,
Thanks for your reply, it made some sense in my messed up mind.
the main thread and the other thread only share a Queue<datetime> one is a producer and the other is a consumer.
but i was worried that while the latter is running the main will stop like he did in the breakpoint. (using eclipse to debug a web application over tomcat using Java did not result the same when stopping one thread.)
thanks for the help,
Tomer.
|
|
|
|
|
you're welcome.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
Dear Mr. Tomer,
Like Mr. Luc said, the threads will act as expected when running. If you would like the other threads to keep going while you are debugging (stopping and going) on another thread, I think you can change this in
Tools > Options > Debugging > `Break all processes when one process breaks' You should check that list of options anyway to check out some cool options. Good luck!
In Christ,
Aaron Laws
http://ProCure.com
|
|
|
|
|
Hi,
I have an application in C#.Net.
This application uses a DSO ActiveX Framer control to load the MS Office applications.
I am loading an excel document here.The loading of the excel comes with default 3 sheets.
I enter some data in Sheet1.
And now i got to the Sheet2 and right click on the tab and click 'Insert' and insert a chart.
Now after selecting the option 'Select Data' from sheet2, i click on Sheet1 to select the Chart data range for the box 'Select Data Source'.
Now when i click on the Sheet1, my application hangs and in order to proceed i have to kill my application from the Task manager.
This does not happen with MSOffice 2003 but only with Office 2007.
My application does not directly get any click events of the Excel. There is a timer and we get this timer tick event.
Could any one help me out in this?
|
|
|
|
|
Hi,
Can any one help to develop a method in C#, so that i can use that statement to INSERT - UPDATE - DELETE operations upon Database.
I'd achieved this in JAVA but couldn't C#
|
|
|
|
|
.Net provides namespaces like System.Data.OleDb and System.Data.SqlClient . These have a lot classes that will help doing these operations. Look into them. If you get stuck somewhere while trying, post the query. There are plenty of members here to help.
It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD
|
|
|
|
|
Try the following...
<pre> public int ExecuteNonQuery(string strConnectionString
, string strStoredProcedure
, SqlParameter[] ObjSQLParameter)
{
// declare variables
SqlConnection ObjSqlConnection;
SqlCommand ObjSqlCommand;
int intRowsAffected = -1;
try
{
// prepare a connection
ObjSqlConnection = new SqlConnection(strConnectionString);
// open connection
ObjSqlConnection.Open();
// prepare command
ObjSqlCommand = new SqlCommand(strStoredProcedure, ObjSqlConnection);
ObjSqlCommand.CommandType = CommandType.StoredProcedure;
// asign parameters to command
//SqlParameter collObj ;
foreach (SqlParameter collObj in ObjSQLParameter)
{
ObjSqlCommand.Parameters.Add(collObj);
}
// execute command
intRowsAffected = ObjSqlCommand.ExecuteNonQuery();
}
catch ()
{
intRowsAffected = 0;
}
finally
{
ObjSqlCommand.Dispose();
ObjSqlConnection.Close();
ObjSqlConnection.Dispose();
}
// return affected rows
return intRowsAffected;
}
</pre>
Thanks
Md. Marufuzzaman
Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you.
I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
|
|
|
|
|
Ew:
catch ()
{
intRowsAffected = 0;
}
is a very bad idea; just allow the Exception to be thrown.
And why not use using statements for the command and connection?
|
|
|
|
|
I agree...it's happened due to modifying the code snippets.. Thanks
Thanks
Md. Marufuzzaman
Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you.
I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
|
|
|
|
|
|
Hi,
I have an app that gets data out of the database. This data is updated via a separate application. At the moment the first application has to check every x seconds if there is any new data. I seem to remember reading a while ago something about subscribing to data changes or something so that instead of it being a pull from sql server, sql server actually notifies the app that there is new data and then the app can go get it, and therefore doesn't have to check itself.
Is that right, or did I dream that?
Cheers,
Rob
|
|
|
|
|
I guess you are looking for SqlDependency or SqlNotificationRequest class.
It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD
|
|
|
|
|
Ahhh, that may well be it, thank you very much!
|
|
|
|
|
I agree...But i think using a trigger would be a good idea to keep track any alternation / insertion of data.
Thanks
Md. Marufuzzaman
Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you.
I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
|
|
|
|
|
And how would that send notification to front end?
It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD
|
|
|
|