|
This code:
try
{
...
}
catch (Exception e)
{
} is called "swallowing an exception", because whatever error occurs in the try block will be caught by the catch block and discarded without any attempt to handle it, log it, or in any other way acknowledge that there is a problem that should be fixed, or at least investigated.
This is considered a poor programming practice, as it just masks problems that may become serious later. There can (occasionally) be a good reasons for ignoring specific exceptions, but these should be restricted to specific exception classes (such as ArgumentNullException, or FileLoadException) rather than a blanket "Exception" and commented thoroughly.
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
Ahh... Gotcha.
There actually is code there. I took it out when I posted it here.
|
|
|
|
|
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
The exceptions which could arise in the ReadDaq function will arise in a different thread - the thread of the GUI. You are not able to catch them here, hence you can omit the try-catch-block here. But ReadDaq ought to have its own try-catch.
|
|
|
|
|
Bernhard Hiller wrote: You are not able to catch them here,
No, not quite true (at all). The Invoke mechanism catches an unhandled exception within the target method and rethrows it in the calling thread. In doing this the stack trace containing the site of the original exception is lost. Whether or not something bad happens when the exception is rethrown depends on the caller. For example I've noticed that the thread used by a System.Timers.Timer to call the Elapsed event handler carries on obliviously with no hint of discomfort!
See the remarks section in Control.Invoke[^]
Alan.
|
|
|
|
|
not 100% sure, but you could pass null instead of new Object[]{}. The signature of the Invoke method requires an object array. This is actually used to box/unbox parameters for the threading function. If null is allowed (I think so) this is for me preferable, because it indicates we're passing it to fulfill the method requirement. passing a new Object[]{} not only initializes memory, but also might indicate we "forgot" to do something.
Hope this helps.
|
|
|
|
|
String snmpAgent = "ipAddressOfPrinter";
String snmpCommunity = "public";
SimpleSnmp snmp = new SimpleSnmp(snmpAgent, snmpCommunity);
Dictionary<SnmpSharpNet.Oid, AsnType> result = snmp.Get(SnmpSharpNet.SnmpVersion.Ver1, new string[] { "OidOfPrinter" });
if (result == null)
{
MessageBox.Show("Request Failed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
foreach (KeyValuePair<SnmpSharpNet.Oid, AsnType> entry in result)
{
textBox1.Text = entry.Key.ToString() + " = " + SnmpConstants.GetTypeName(entry.Value.Type) + " (SysDesc.): " + entry.Value.ToString() + "->
here is the connection to the printer. I can get the name, properties, software etc. of the printer, but i cant get the status of the printer(if it is online, offline, toner level, paper status, etc.) can you help me to code this?
Thanks a lot for your ideas and helps.
|
|
|
|
|
Please do not repost the same question, particularly only two hours later!
It's rude, unnecessary, and unlikely to win you friends or get a better answer.
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
Please don't repost the same question; your question isn't more important than the others.
alexalexalex1907 wrote: can you help me to code this?
Coding it is your job, not mine. I can give you pointers, can help debug, or advise on code.
What you are trying to achieve is not something that many people will have experience with. So, asking it on a forum and waiting for an answer will be an exercise that might take a whole lotta time.
The protocol does not care about device-specific capabilities. You'll have to contact the vendor of your printer, and have them confirm that they actually SUPPORT this feature - as I said, there's a chance that the printer does not broadcast it's toner-status.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
I'm getting the following sequence of exceptions:
First-chance exception at 0x791659f0 (clr.dll) in MyProg.exe: 0xC0000005: Access violation reading location 0x30383043.<br />
Unhandled exception at 0x30383043 in MyProg.exe: 0xC0000005: Access violation.
Does this look like a problem with the CLR, rather than with my code? This is all information I have on this exception because the call stack is messed up and I can't see where in the source this is happening.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Richard Andrew x64 wrote: Does this look like a problem with the CLR How to tell? The first message is a fairly common one seen when debugging and is raised when a DLL is needed and is not currently loaded. You can usually ignore such messages. The second one indicates that an access violation occurred somewhere within the program, which usually means that you have a reference somewhere that is going out of bounds. The only answer is a few minutes, or hours, with the debugger.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Quote: The first message is a fairly common one seen when debugging and is raised when a DLL is needed and is not currently loaded.
That's not the case. A first chance exception is the first phase in exception handling and is intended to support debugging. When an exception occurs the debugger (if one is attached) is given first chance at handling it (this first chance mechanism is part of the exception handling process and occurs for all exceptions). The debugger can take any number of measures including stopping it proceeding any further. If the debugger allows the exception to proceed normally or one isn't present it advances to the "second chance". This is the normal application level exception processing. There is no specific DLL not loaded exception in Windows that I am aware of.
Quote: First-chance exception at 0x791659f0 (clr.dll) in MyProg.exe: 0xC0000005: Access violation reading location 0x30383043.
Unhandled exception at 0x30383043 in MyProg.exe: 0xC0000005: Access violation.
Both these messages are caused by the same exception (note that both are access violations at the same address). The first line is the first chance and the second occurs after the second chance if it doesn't handle it.
Steve
modified 12-Oct-12 8:21am.
|
|
|
|
|
Quite right, I was confused and misinformed.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Richard Andrew x64 wrote: Does this look like a problem with the CLR, rather than with my code?
No, it looks like an exception, and without anything to reproduce it'll be hard to place 'blame'. What version of the framework are you using? Did you install all the service-packs?
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
I need to disable my button because when the user presses it twice, I get an InvalidOperationException (backgroundWorker is busy and cannot run multiple tasks concurrently) in my form's code that is calling the form where I disable the button. In my btn_Program_Click method, I have
btn_Program.Enabled = false , so it should be disabled, but the user can still click on it for some reason. Perhaps I'm misunderstanding what Enabled = false should be doing??
Thanks,
Mich
|
|
|
|
|
An easier fix would be just to unhook the click eventhandler before you create your event and just enable your click again in the threading complete handler.
|
|
|
|
|
How is the click eventhandler disabled?
|
|
|
|
|
Suppose you add the event handler like this:
myButton.Click += MyButton_Click; Well, to remove the reference, all you need do is
myButton.Click -= MyButton_Click;
|
|
|
|
|
That should work. Are you sure you're not enabling it again -or- inadvertently trying to start another instrance of your background task?
/ravi
|
|
|
|
|
Hi,
i need your help. i search "how to check toner level in printer using c# and SNMP protocol.
Please give me your ideas.
thanks a lot.
|
|
|
|
|
Check the SNMP-documentation; not all printers will provide the functionality, and I doubt that there will be much copy/pastable solutions out there.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
alexalexalex1907 wrote: check toner level in printer
Read the documentation for the printer (whatever it is) and/or check with whomever the manufacturer is.
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Please stand in front of my pistol, smile and wait for the flash - JSOP 2012
|
|
|
|
|
SNMP is a protocol just like http (just http) is.
Knowing SNMP however tells you absolutely nothing about solving your problem. Just as knowing nothing but http would not allow you to interact with a web site.
So you must learn more about the specific printer, and driver software and version.
|
|
|
|
|
now i wont be able to code this with examples on the internet, right?
String snmpAgent = "ipAddressOfPrinter";
String snmpCommunity = "public";
SimpleSnmp snmp = new SimpleSnmp(snmpAgent, snmpCommunity);
Dictionary<SnmpSharpNet.Oid, AsnType> result = snmp.Get(SnmpSharpNet.SnmpVersion.Ver1, new string[] { "OidOfPrinter" });
if (result == null)
{
MessageBox.Show("Request Failed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
foreach (KeyValuePair<SnmpSharpNet.Oid, AsnType> entry in result)
{
textBox1.Text = entry.Key.ToString() + " = " + SnmpConstants.GetTypeName(entry.Value.Type) + " (SysDesc.): " + entry.Value.ToString() + "->" + SnmpConstants.SMI_APPSTRING;
textBox1.Text += "SysName: "+entry.Key;
}
}
Here is the connection to the printer. I can get the name, software, oid of the printer but i cant check its status(if it is online, paper jam, offline, toner low, etc.) how can i get theese properties?
Thanks a lot
|
|
|
|
|