|
Heath Stewart wrote:
I guess maybe I should just be a park ranger out in Washington now that Redmond has decided us programmers are absolete.
Hey, if you did, who could blame you - this[^] view looks awesome.
- Nick Parker My Blog | My Articles
|
|
|
|
|
Well, when you an Megan come up to visit (BTW - have a thread about my new job in my blog) we can take you there! And since I'm absolete now with that pesky DoesEverything class, we'll probably just have a cabin (so it's close to work) there!
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Ok, would this be the way to go?
<br />
object [] arg = {6};<br />
ManagementClass mc = new ManagementClass("Win32_DesktopMonitor");<br />
mc.InvokeMethod("SetPowerState", arg);<br />
problem is that I get the an exception:
An unhandled exception of type 'System.Management.ManagementException' occurred in system.management.dll
Additional information: This method is not implemented in any class
|
|
|
|
|
If you followed my directions (downloaded the WMI extensions for VS.NET) and generated a typed class for the Win32_DesktopMonitor CIMv2 class, you might see immediately what was wrong: SetPowerState takes two parameters: a UInt16 and a DateTime .
Change the first line to object[] arg = {6, DateTime.Now}; and it should work.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hmm... changing the line now gives this execption:
An unhandled exception of type 'System.Management.ManagementException' occurred in system.management.dll
Additional information: Type mismatch
I'm not sure if I understood your direction 100%. After having installed the WMI extensions, I opened the Server Explore and browsed to Servers|MyComputerName|Management Clas|Desktop Settings and then I right clicked and chose "Generate Managed Class"
|
|
|
|
|
That's not the right class. You have to right-click on "Management Classes" and add a new class: root\CIMv2\Win32_DesktopMonitor. Generate a new class from that.
Try object[] arg = new object[] {(ushort)6, DateTime.Now}; . It really shouldn't matter since non-decimal numeric types are implicitly convertible, but it's worth a try. This could present a problem since the method is discovered by the types of parameters, and since just 6 would actually be an int (System.Int32 ), that could be the source of the problem.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
|
Since the dialog is showing the friendly names, the logical conclusion would be to find something that resembles "Win32_DesktopMonitor", like "Monitors". Add that and generate a class for your project.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
hi there,
I developed a small application which will clean up all record(say 4000 or 5000 record) from a dummy table and will insert the updated version of that cleaned record from some other table. This operation is happening oncloseing event handler of that perticular application.
I am using asynchronous programming style for inserting records and synchronous for deletion.
When user click on Exit button, he is actually starting the above mentioned operation.The main problem is he can exit from application once the deletion(synchronous) completes. Here that insertion may or may not on progress depending upon the record size to insert. Why cause insertion is in asynchronous fasion.
In this senario my insertion operation will terminate with inserting few records from actuall collection and will give error say "Communication Error".because the application is closed.
one more thing is i can only do this much operation on the time of application exit not before.
i am including main part of my code block.
//required namespaces
namespace PlanningBoardSystem.GUI
{
public delegate void DataRowInsertionDel(DataRow Dtr);
public class BatchRecordInsertion
{
string DSN=ConfigurationSettings.AppSettings["sourcename"].ToString();
AsaTransaction Astrns;
string Uid="dft";
string Pwd="dft";
AsaConnection Conn;
private DataRow[] Dtable;
public void MyCallBack(IAsyncResult Iar)
{
DataRowInsertionDel DRID=Iar.AsyncState as DataRowInsertionDel;
DRID.EndInvoke(Iar);
}
public void InsertRecord(DataRow Dtr)
{
try
{
//Astrns=Conn.BeginTransaction();
string InsertCmd="Insert into tem values('"+Dtr["process_id"]+"','"+Dtr["record_type"]+"','"+Dtr["process_dec"]+"')";
AsaCommand Asa=new AsaCommand(InsertCmd,Conn);
//Asa.Transaction=Astrns;
Asa.ExecuteNonQuery();
}
catch(Exception Ex)
{
//Astrns.Rollback();
MessageBox.Show(Ex.Message.ToString());
}
}
public BatchRecordInsertion(DataRow[] Dtable)
{
this.Dtable=Dtable;
try
{
Conn=new AsaConnection("Data Source='"+DSN+"'; uid='"+Uid+"'; pwd='"+Pwd+"'");
Conn.Open();
string Delcmd="Delete from tem";
AsaCommand Asa=new AsaCommand(Delcmd,Conn);
//Asa.Transaction=Astrns;
Asa.ExecuteNonQuery();
}
catch(Exception Ex)
{
MessageBox.Show("Error While Opening Database Connection","Database Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
DataRowInsertionDel InsertDel=new DataRowInsertionDel(InsertRecord);
AsyncCallback AsCall=new AsyncCallback(MyCallBack);
for(int j=0;j<<1000;j++)
for(int i=0;i<<5;i++)
{
DataRow Dtr=Dtable[i];
IAsyncResult IAR=InsertDel.BeginInvoke(Dtr,null,null);
}
}
}
}
//here i used one outer loop. don't worry about that. i have only five record in
Dtable. i want few more record.that's why i used that loop//
" How will i handle this sort of situation ?"
" how can i ensure all insertion operations are completed or not " ?
" How i will hold the exit operation upto the completion of all insertion process " ?
"If you know efficient way than this idea of inserting bulk amount of records(no validation required) while keeping performance in mind. Then tell me how ? "
********************
S r e e j i t h N a i r
Bangalore - India
********************
|
|
|
|
|
hi sreejith ,
I have not used ASynchronous method in .NET. Based on my experience
with Win32 threads and Posix thread package , what i feel is
u have retrieve the id of the main thread . then invoke
join method.
join will wait for all the child threads to finish before exiting the parent
process.
This is just a guess . But , i feel it may work
Praseed Pai
www.praseedpai.com
|
|
|
|
|
Asynchronous programming is what you use when you want one operation to run while another continues. In this case, it's the closing of the form that is being allowed to continue. If you use the End* method of your asynchronous call, it will block, but then what's the point of using asynchronous programming (answer: there isn't a reason)?
The simple solution is: don't use asynchronous programming here. The call must block that cleans up your table and then updates whatever data source you're talking about. After all that's done, then you allow the form to close.
If you want to provide feedback, however (a good idea, since the user might want to know why the form he/she just closed isn't closing), that's when you use asynchronous programming. Start a new dialog to show progress (or use a ProgressBar or something in the current form that's being closed. Run the operation you want in a separate thread and update the progress (be sure to use Control.Invoke to update the UI on the thread on which it was created). You must prevent the forum from closing, however. The easiest way is to use another dialog and call ShowDialog (a blocking call for a modal dialog) on it. If you use the current form, you'll need to call the corresponding End* method for your async Begin* call. This will block until the async operation is done (which is currently updating your ProgressBar or whatever).
There are some articles here on CodeProject that discuss these in depth, and may even present a few alternatives (the concept will be the same, but implementation may differ). I recommend you try searching for such articles if you want specifics, but hopefully this response was helpful.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
hi heath
heath you are correct. we can use asynchronous when we are not expecting the result to the consecutive operation. here the senario is same. only thing is i need to refresh table just before user closes the application or save the application. if i put this much code block in save,then it will work. but user may save different time while working with the application. So all time it will execute asynchronous code block. doing this is won't make too very sence here.Here i want to refresh only once with final updation. Amount of data to refresh is huge(minimum say 5000 record(15 feilds)). So i don't want to disturb user for his all saving operation.
You told me asynchronous is not a perfect match for this senario. That is correct and i know it very well. But heath, if i want to do synchronous (normal) style of record insertion it will take 30 to 40 seconds to complete. But if i go for asynchronous(as per my logic) it will take only 4 to 5 seconds.
heath tell me which other way i can adopt for this sort of situation except synchronous to make my operation duration 4 to 5 second.
thanks
sreejith
********************
S r e e j i t h N a i r
Bangalore - India
********************
|
|
|
|
|
The amount of time it takes to update your database is rather static (might differ a little due to various conditions). It's just that here your asynchronous call is executed and execution is returned immediately to the caller (your Form.Closing handler).
If this is not your main application Form , add the code to update the database elsewhere (perhaps on the original caller), as well as a way to get a deep copy of the data from the Form that is closing. Then execute the method asynchronously and the effect of the Form closing won't matter.
Also, for this Form - if not the main application form (i.e., that which is directly or indirectly passed to Application.Run ) - if you use Form.Show , you don't need to dispose of the Form so don't - the async operation should run even though the form is hidden (depending on how you're accessing data that was input into the form). If you're using ShowDialog , then you're supposed to dispose your Form once you get what you need from it (otherwise you'll have a memory leak). In this case, you're back to what I said previous: you have to block at some point and wait for that async operation to complete with having done little else (i.e., the form was hidden), so there's really no point to using asynchronous operations, here.
A better design is to prompt the user to save before closing and take your time. This is what most other applications do. Open Microsoft Word, type a character or so and close the application - your prompted. Do the same with Notepad, Excel, AutoCAD, etc. Why not take this approach? At least the user no knows what's going on. Add that with a ProgressBar (perhaps a step for each record updated, depending on how you update your data source) and you'll have a much more user-friendly application.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
hi,
Heath i have a doubt. When we run an application i mean a standalone .exe. is this perticular exe is comming in one appdomine or not ?
********************
S r e e j i t h N a i r
Bangalore - India
********************
|
|
|
|
|
An EXE - if invoked by the executable loader - has at least one AppDomain in which it's loaded an run. I say that because an EXE .NET assembly can be loaded just like a DLL (the metadata is loaded, regardless of the PE/COFF executable).
It can create additional AppDomains, though.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
So how can i know how many threads are there in my appdomin without main thread.
If it is possible how will i know whether that perticular thread status.
sorry for this disturb
**************************
S r e e j i t h N a i r
**************************
|
|
|
|
|
Process.GetCurrentProcess().Threads.Count
I don't see what good that will do, though. You're code isn't the only code starting threads. Windows applications use many threads in the background as needed.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
hi,
Heath i have a doubt. When we run an application i mean a standalone .exe. is this perticular exe is comming in one appdomin or not ?
********************
S r e e j i t h N a i r
Bangalore - India
********************
|
|
|
|
|
There are only a few reasons why anyone should embrace asycronous response patterns and this doesn't appear to be one of them. You should design the batch processing into the SQL server instead of in the client application.
You should avoid it if possible because it makes problems more complex. Trying to do this "mixed mode" thing becomes a nightmare. You should either keep everything syncronized or asycronous because bridging between causes all sorts of fun issues (like race conditions and deadlocks). Simple tasks like deleting rows become much harder. If you want to delete, you must syncronize against the asycronous handle you setup elsewhere to do insertions. If you want to exit you have to syncronize against the asycronous handle you setup elsewhere to do insertions. Heck even adding rows you will have to be careful.
Asycronous programming is not for the faint of heart which is why I constantly recommend against using it unless you have to. The point of a SQL server is to keep transactions sane and syncronized. Why thawart this?
|
|
|
|
|
Hi,
Is it possible to port the code developed in C# on to a Solaris machine??
Is there any cross platform product that makes it possible??
If somebody has idea how to do it, please reply.
Regards,
Satya
|
|
|
|
|
Mono[^]
"If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell
Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!
|
|
|
|
|
Hi,
I want to use C# features and port it to Sun Platform, is this possible??
Regards,
Satya
|
|
|
|
|
|
Hy everyone!
Yeah, shame on me, but it seems I actually do have a blackout , although this should be something of C#s basics:
I do have two clases
<br />
public class Key<br />
{<br />
public int keyId;<br />
public string telephoneNo;<br />
}<br />
public class Telephone<br />
{<br />
public Key[] keys;<br />
public string telephoneId;<br />
}<br />
o.k. that's it. But now I do want to create an array of Telephoneobjects (Lentgth 1) - which of course contains the telephoneId field - which contains an array of Keyobjects - which of course contains the keyId and the telephoneNo.
This finally should allow the following assignment for example
<br />
mynumbers[0].telephoneId="12345";<br />
mynumbers[0].keys[0].keyId=12;<br />
mynumbers[0].telephoneNo="12345";<br />
Yes, I know I should have known myself but I guess I do have kind of blackout at the moment!
Thanks for your patience for helping to get rid of my blackout!
Stephan.
|
|
|
|
|
Telephone[] telephones = new Telephone[1];
telephones[0] = new Telephone();
Also, you should not declare fields as public , it violates the encapsulation rules in Object Oriented design. You should wrap the behaviour in properies instead - this allows you to change the internal structure of the class without modifying the external interfaces.
"If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell
Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!
|
|
|
|