|
I have been programming in C# for a long time. My engineer has programmed this way on the 1st option. I'm trying to figure out and need to revise the code to work with USB device. I've came up with the solution with loss/recovery of the USB device with WndProc process.
|
|
|
|
|
I have been programming in C# for a long time.
Then why did you ask this question? If what you said is true, this should have been obvious to you.
|
|
|
|
|
I would have to agree with the 2nd option.
|
|
|
|
|
|
Tried to cast the control. Still nothing.
|
|
|
|
|
You have to cast it to the type that contains the property your trying to use, not the one that doesn't.
|
|
|
|
|
Just to add to the other responses:
Your DeviceMonitor is a UserControl, which means that it is derived from Control (it has to be, otherwise it can't be displayed - even Form is derived from Control via a couple of other classes). That means it inherits (and you can access) all the properties of a Control in your class. But it doesn't work the other way...
DeviceMonitor devMon = new DeviceMonitor();
Control deviceControl= devMon;
this.Controls.Add(deviceControl); devMon contains a reference to your DeviceMonitor instance, so you can access all the properties and methods of the DeviceMonitor class via devMon:
if (devMon.isConnected)
{
...
} But deviceControl is a Control reference: it can contain a Control instance, or an instance of any class which is derived from Control, but it can only access the properties and methods of the Control class because that is the only class type it can guarantee that deviceControl contains. It is perfectly legal and correct to say:
DeviceMonitor devMon = new DeviceMonitor();
Control deviceControl= devMon;
this.Controls.Add(deviceControl);
deviceControl = new TextBox(); So the compiler will not let you access a derived class property or method via the base class variable.
It's a bit like cars: a Car has four wheels and an engine, but a Ford Fiesta also has Cruise Control, which a Ford Model T doesn't. If you declare a variable as a Car, then you can't access myCar.CruiseControl because the Car may be a Model T.
BTW: You don't need to use the deviceControl in your code - you can use a derived class anywhere you can use the base class. So this will work as well:
DeviceMonitor devMon = new DeviceMonitor();
this.Controls.Add(devMon);
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
|
|
|
|
|
|
Quite a performance hit if you do that a lot.
|
|
|
|
|
And what happens if that object does not have a property named isConnected ? You have received some good advice here from people who are trying to help you, so why implement a bad answer from somewhere else?
Use the best guess
|
|
|
|
|
Heres one way:
DeviceMonitor devMon = new DeviceMonitor();
Control deviceControl= devMon;
this.Controls.Add(deviceControl);
...
if((deviceControl as DeviceMonitor).isConnected)
{
...
}
a better way:
DeviceMonitor devMon = new DeviceMonitor();
this.Controls.Add(devMon);
...
if (devMon.isConnected)
{
...
}
modified 31-Jul-13 17:37pm.
|
|
|
|
|
I have background threads that are doing things and based on logging when the computer goes to sleep, the threads cause the application to hang because of timing I would assume. I would like to abort the threads when the machine goes to sleep and bring them back when it wakes up, or something else if there is another way?
I tried inserting the code below yesterday, but I did not see any messageboxes below when I came in this morning and the application was hung again. This is my first time encountering power management issues, so I'm new to this override code.
Thanks for reading.
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_POWERBROADCAST:
switch (m.WParam.ToInt32())
{
case PBT_APMQUERYSUSPEND:
if (reminderThread.IsAlive == true)
{
reminderThreadTerminate = true;
MessageBox.Show("reminderThreadTerminate set to true");
}
break;
case PBT_APMRESUMESUSPEND:
if (reminderThread.IsAlive == false)
{
reminderThread.Start();
MessageBox.Show("reminderThreadTerminate started");
}
else if (reminderThread.IsAlive == true)
{
reminderThreadTerminate = true;
Thread.Sleep(2000);
reminderThread.Start();
MessageBox.Show("reminderThreadTerminate was still running, it was killed and then restarted");
}
break;
case (PBT_APMQUERYSUSPENDFAILED):
case (PBT_APMSUSPEND):
case (PBT_APMRESUMEAUTOMATIC):
case (PBT_APMRESUMECRITICAL):
case (PBT_APMBATTERYLOW):
case (PBT_APMPOWERSTATUSCHANGE):
case (PBT_APMOEMEVENT):
break;
}
break;
default:
break;
}
base.WndProc(ref m);
}
|
|
|
|
|
I moved the code to the areas below and it still froze this morning. If anyone has any ideas, I'd appreciate the direction.
case (PBT_APMQUERYSUSPENDFAILED):
//value passed when system is suspended
case (PBT_APMSUSPEND):
//value passed when system is resumed automatically
case (PBT_APMRESUMEAUTOMATIC):
|
|
|
|
|
Win 8/64 Visual Studio 2012 v.3, WinForms C#, FrameWork 4.5: no use of explicit threading in the project.
I've come to rely on using 'Stopwatch for a timer, and I am surprised to find, in a current project, a huge difference between what I observe, and what Stopwatch reports.
I have a very complex generic class, whose generic Types include other generic Type classes. I am able to create any number of instances of the outer class without a problem, and verify their contents.
Using Stopwatch to time the creation of the instances seems to give results consistent with what I observe. I'm not bothering, in this case, to do a "dry run" to pre-JIT.
Where what I observe really varies from what Stopwatch returns is: in the case where I loop through all the created instances in the outer class, and fill a StringBuilder with a complete kind of "report" on all the instances of the outer class, and all the various instances of the inner classes.
The inner classes have public Guid properties, set when their constructor is called (via 'private set).
If I create 10,000 instances of the outer class, and each of (in this case #2) of its inner classes have 10 objects each initialized as they are created. I end up with potentially 20k Guids, 100k each of the inner class contents. So, the resulting "report" (adding in tab characters, and other formatting) will come out to about 250k lines of text, containing about 4mb of characters.
From pressing the button that generates the "report" to seeing the report-TextBox filled, takes around thirty seconds, but Stopwatch is reporting the operation took 300~800 milliseconds.
Hypotheses:
1. there's something about using Stopwatch in the scope of a Button_Click EventHandler that leads to errors. Threading issue ? Doubtful, imho, since object creation seems to time okay.
2. there's something about using StringBuilder, and then converting to 'string, and setting the 'Text property of a TextBox that compromises Stopwatch. Wrapping TextBox refresh in 'SuspendLayout and 'ResumeLayout seem to have no appreciable effect.
Here's the code as stripped-down as possible to indicate how the report is being generated, and Stopwatch is being used:
sb.Clear();
watch2.Reset();
watch2.Start();
for (int i = 0; i < KVPList.Count; i++)
{
}
tbReport.Text = sb.ToString();
watch2.Stop();
tbEventReport.Text = "Report Displayed: Time(ms): " + watch2.Elapsed.Milliseconds Appreciate any suggestions, or ideas; very high-precision timing is not really needed here.
thanks, Bill
~
“This isn't right; this isn't even wrong." Wolfgang Pauli, commenting on a physics paper submitted for a journal
|
|
|
|
|
The problem isn't with Stopwatch at all. It's with sending 4MB of text to TextBox. Try opening a file that large in Notepad and you'll see the same result. It just takes forever for the TextBox to render something that large.
|
|
|
|
|
Dave,
I wonder what happened to you on this one: the general slowness of filling a standard WinForms TextBox control, or NotePad, with a large amount of Text is not relevant here at all.
How, I wonder, did such an inane response get three up-votes ?
The reason I am doing this stuff with showing a lot of text is related to software development; it has nothing to do with production mode, and some final software a user would interact with. I am examining the results of building a very large collection of a complex object, and that means I want to inspect the result in detail at run-time (much easier than setting a break-point in Visual Studio, and drilling down through little windows you have to scroll-in endlessly to reach item 899,233 out of 1 million).
The relevant issue is what I perceived as an inaccurate result using Stopwatch.
Alan's answer below points out the simple (and stupid) mistake I made in syntax.
Using Win 8/64, Visual Studio 2012 v.3, FrameWork 4.5, compiling to "AnyCPU," in Debug mode: using relatively old hardware (6420 CPU at 2.13mhz., 4gb. ram):
fyi: I can fill a TextBox with fifty-megabytes of text in around 33 seconds.
fyi: Using a RichTextBox I can fill it with fifty megabytes of text in under 3 seconds
fyi: converting a StringBuilder containing 50 mb. to a string: itself takes about 2 seconds.
fyi: NotePad ? I use UltraEdit, which can open a 50 mb. text file in under 3 seconds.
But, knowing someone with your skills can have an off-day, as I did; knowing you are imperfect, as I am: well, I feel less lonely
your friend, Bill
~
“This isn't right; this isn't even wrong." Wolfgang Pauli, commenting on a physics paper submitted for a journal
|
|
|
|
|
My machine is a bit slower than yours. I can't do 50MB in 30 second.
But, you're correct. I lost my head had on this one.
|
|
|
|
|
A man who knows he lost his head, and gracefully admits it, is a man who has, not only a head, but a heart
appreciatively, Bill
~
“This isn't right; this isn't even wrong." Wolfgang Pauli, commenting on a physics paper submitted for a journal
|
|
|
|
|
|
Ooh, that's difficult to spot. The mistake is subtle, easy to make and I'm sure I've done the same thing.
Stopwatch.Elapsed is a TimeSpan and TimeSpan.Milliseconds is just the millisecond component. If the measured value is less than 1 second then the result will be correct but either Stopwatch.Elapsed.TotalMilliseconds or Stopwatch.ElapsedMilliseconds would be what you really wanted.
Alan.
|
|
|
|
|
Alan, you nailed it ! And, how gently you broke the news, thanks
I was relieved to search all my code for the last several years, and find not one instance where I had made the same mistake made here: but, not relieved enough to get over the chagrin of making such a stupid mistake now.
I owe you an elephant-ride in the jungle in northern-Thailand.
yours, Bill
~
“This isn't right; this isn't even wrong." Wolfgang Pauli, commenting on a physics paper submitted for a journal
|
|
|
|
|
To suppress the warning CA2100 i am trying to create a parameterized query. By the following code.
string sheet_name=listBox_Select_a_sheet.SelectedItem.ToString();
OleDbCommand cmd = MyConnection.CreateCommand();
cmd.CommandText = "SELECT count(*) FROM [?]";
cmd.Parameters.AddWithValue("?", sheet_name);
cmd.ExecuteNonQuery();
The value of the ? is not replaced.
I have also tried the following
OleDbCommand cmd = MyConnection.CreateCommand();
OleDbParameter para = new OleDbParameter("?", sheet_name);
cmd.Parameters.Add(para);
cmd.CommandText = "SELECT count(*) FROM [?]";
and
string strSql = "SELECT count(*) FROM [@Name]";
cmd.Parameters.AddWithValue("@Name", sheet_name);
My aim is to replace the ? with the parameter.
And one more doubt is can we use parameterized query to replace a table name?
|
|
|
|
|
Parameters do not work for table identifiers. They only work for columns or other identifiers that use data types, such as stored procedure parameters.
|
|
|
|
|
Thank you for your kind reply. That was very helpful. One more thing need to be cleared. How can we suppress the warning CA2100 : Microsoft.Security for the following code?
string sheet_name=listBox_Select_a_sheet.SelectedItem.ToString();
string strSql = "SELECT count(*) FROM sheet_name";
using (OleDbCommand cmd = new OleDbCommand(strSql, MyConnection))
{
cmd.ExecuteNonQuery();
}
|
|
|
|
|
Turn off Code Analysis when you compile. It's not a compiler warning but a code analysis warning. This is to remind you that you should not use user input directly when building your SQL statement as a string.
|
|
|
|