|
It's cool!
I would strongly suggest that the manufacturers will have a better idea than anyone here: it may be a feature specific to that smart card, and no other.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Manfred R. Bihy: "Looks as if OP is learning resistant."
|
|
|
|
|
Yes I hope so, they haven't reply my message yet. I'm confuse, so I try to post here, who knows someone in here can help me.
Thanks for your suggestion...
|
|
|
|
|
I have a class:
public class SomeClass<t> : ISomeInterfaceBase
{
.
.
public delegate void MessageHandler(object sender, T message);
public event MessageHandler Handler
{
}
.
.
}
so its a generic class and has a delegate that uses the T type as a param. I thought that was a good idea so users don't have to cast to their T type.
But an issue arose:
MainWindow -> subscribes to the event
MainWindow pops up Window1
Window1 -> subscribes to the event too
Window1 closes (and doesn't unsubscribe)
Now when the event triggers, it still calls the one in Window1 too.
So I figured I'd use weak references.
public event MessageHandler Handler
{
add
{
lock (_lstHandlers)
{
foreach (WeakReference weakRef in _lstHandlers)
{
if ((MessageHandler)(weakRef.Target) == value)
return;
}
_lstHandlers.Add(new WeakReference(value));
}
}
when the event needs to be triggered, I loop through _lstHandlers and check if isAlive = true and then send it, otherwise, I remove that event.
Problem is, isAlive never goes false when I close window1.
I looked into WeakEventManager, but that doesn't seem like it will jive with a generic delegate. Seems like you need to have a WeakEventManager for every generic type thats created?
|
|
|
|
|
You can't rely on the Garbage Collector to play along like that - it could decide to collect those delegates at any point in time since they were not referred to anymore, possibly infinitely long into the future.
Window1 should unsubscribe before closing (in the OnClosing event, for example?)
|
|
|
|
|
Yeah, Window1 *should* unsub... but sometimes people don't. Sure they are being lazy and its really a "bug", but the intention is to protect against that.
|
|
|
|
|
Ooh so it's out of your control, well that changes matters..
I don't see any solutions then, except a couple of really really dirty things.
You can, with reflection, extract the Target Object from a delegate object. You can then check whether it is a Window1 and whether it has closed yet.
You could also try to subscribe to the OnClosing of Window1 (through that Target Object) and when it's triggered delete all delegates which have Window1 as Target Object.
|
|
|
|
|
SledgeHammer01 wrote: I looked into WeakEventManager, but that doesn't seem like it will jive with a
generic delegate. Seems like you need to have a WeakEventManager for every
generic type thats created?
Sounds like you need to roll your own generic weak event manager class - could be an interesting and useful excercise!
|
|
|
|
|
Before I waste time going down this road, is that going to solve the original issue (of Window1 closing and still having a listener)? It would seem that wrapping a delegate in a WeakReference should theoretically have the same effect, no?
I'm not really liking WeakEventManager to be honest with you. Seems like everybody has to implement the IWeakEventListener interface.
I'm just really curious why my previous idea didn't work... of storing the WeakReferences myself???
|
|
|
|
|
I'm not sure to be honest whether it will solve your problem. Every weak reference solution seems to have pros and cons. The event model employed by .NET is flawed in this regard and relies on consumers unsubscribing correctly.
Another option maybe to forget about events in the normal way and go back to generating old fashioned windows messages or something similar so interested parties can override WndProc and catch the event there - no direct subscription... Maybe not practical or possible in your situation, certainly not as elegant as events, but worth a thought?
|
|
|
|
|
Hmm...
I found a lightweight solution (a WeakDelegate<t> class and a WeakEvent<t> class that work together). Seems like it will work how I want.
They store a weak ref to the owner class and use dynamic invoke to execute the method. So they can tell when the owner class is dead.
The dynamic invoke is probably a bit slow though... I might look into switching that to something faster.
http://tomlev2.wordpress.com/2010/05/17/c-a-simple-implementation-of-the-weakevent-pattern/[^]
|
|
|
|
|
You may still execute the event in the time between the closing of the window and the GC destroying it though
|
|
|
|
|
For an item of hardware I have written a Shared library to make it easy to use the API for this hardware. The library sets up data structures and handles, buffers etc for simple program use.
For example to arrange data to be transmitted I call routings like:-
UINT16 TransmitData( UINT unit, UINT addr, TXBUFFER * tx_buffer );
However in VS2008 I used C++ in a MFC dialog application, which all worked well.
Having moved to VS2010, I now see that the intellisence does not work with this product.
So my thoughts were to use C# for the Windows forms and link in the Shared Library.
I have built the Shared Library in VS2010 so I need to understand how to access these function in a C# program. Is this using ‘Wrappers’ and how does the libraries API001.LIB and API001.h files link in?
For a C++ MFC program I included the headers and LIB files with the PATH set up for the DLL’s.
The libray is built using C.
Many thanks,
Andy
|
|
|
|
|
you would need a DLL file and P/Invoke technology; you can't use any .h files or .lib files.
Maybe read this[^] first.
Things are easiest when all arrays (or objects) get allocated by managed (C#) code, then passed to the native world; that works easier than the other way around!
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
You can create a C++/CLI class library and include the .lib and .h there. Then create a managed class in C++ that wraps the functions. Finally, make a reference from the C# application to the C++/CLI project.
|
|
|
|
|
Hi
I always thought IDictionary checks key when adding elements by calling GetHashCode overrides - but I did a simple experiment, seems like it only call Equals (i.e. Even if two instances with same hash code added to a Hashtable or Dictionary, it won't blow up so long "Equals" return false)
<br />
public class Complex<br />
{<br />
public int a, b, c;<br />
<br />
public override bool Equals(object obj)<br />
{<br />
if (obj is Complex)<br />
{<br />
Complex other = (Complex)obj;<br />
if (other.a == this.a <br />
&& other.b == this.b <br />
&& other.c == this.c)<br />
{<br />
return true;<br />
}<br />
} <br />
<br />
return false;<br />
}<br />
<br />
public override int GetHashCode()<br />
{<br />
return a.GetHashCode() ^ b.GetHashCode();
}<br />
}<br />
...<br />
Complex c1 = new Complex();<br />
c1.a = 1;<br />
c1.b = 2;<br />
c1.c = 3;<br />
Complex c2 = new Complex();<br />
c2.a = 1;<br />
c2.b = 2;<br />
c2.c = 5;<br />
<br />
System.Collections.Hashtable SomeDict = new System.Collections.Hashtable();<br />
SomeDict.Add(c1, DateTime.Now);<br />
SomeDict.Add(c2, DateTime.Now);<br />
I digged around MSDN[^] and found that
"Override the Equals method whenever you implement the equality operator (==), and make them do the same thing. This allows infrastructure code such as Hashtable and ArrayList, which use the Equals method, to behave the same way as user code written using the equality operator."
So this confirms Hashtable or Dictionary uses only "Equals", not "GetHashCode" --- then what's "GetHashCode" for? Doesn't seems like "Hashtable" or "Dictionary<k,v>" are using it at all..?!?
Another REF (but this talks about performance only): http://www.codethinked.com/an-overview-of-system_collections_generic[^]
dev
modified on Tuesday, April 19, 2011 5:41 AM
|
|
|
|
|
The hashcode is only used balance the hash table.
What doesn't work is if A.GetHashCode() != B.GetHashCode() but A.Equals(B). That is, it may work accidentally but all bets are off.
That means that GetHashCode could return 0 and everything will still work. Performance will suffer though.
edit: useful link: Eric Lippert on GetHashCode[^]
|
|
|
|
|
Excellent explanation, Thanks for that.
Thanks
Md. Marufuzzaman
I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
|
|
|
|
|
The key value is what is used for definining the functionality; the hash value is used for optimizing the performance.
IDictionary requires all keys to be different, that is all. Different keys may result in the same hash value, as the key can have any size, and a hash is just a 32-bit integer, so you can't have an infinite number of different hash values.
And please use PRE tags, I don't read unformatted code...
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
CORRECTION - Dictionary did throw an ArgumentException if insert two items (where "Equals" returned true) gets inserted into dictionary twice (just set "j" to a bigger number, say 2)
However, below I've tried to implement GetHashCode in two ways:
ATTEMP 1 (correct because "GetHashCode" aligned with "Equals") --- 46 seconds to recall the elements by Key
ATTEMP 2 (Incorrect because not aligned with implementation in "Equal") --- 49 seconds (Seems like negligible and diff really arise from the addition XOR operation in GetHashCode)
You can try to implement GetHashCode = a^b, it'd be done in no time.
I'm not really sure why we need "GetHashCode" still - (unconvinced that its uses it's primarily internal to Dictionary's internal retrieval and impact only speed)
<br />
using System;<br />
using System.Collections.Generic;<br />
using System.Linq;<br />
using System.Text;<br />
<br />
namespace TestDictWithDuplKeys<br />
{<br />
public class SomeClass<br />
{<br />
public int a = 0;<br />
public int b = 0;<br />
public int c = 0;<br />
public int d = 0;
<br />
public override bool Equals(object obj)<br />
{<br />
SomeClass cObj = null;<br />
<br />
if (obj != null && obj is SomeClass)<br />
{<br />
cObj = (SomeClass)obj;<br />
if (this.a == cObj.a && this.b == cObj.b && this.c == cObj.c)<br />
{<br />
return true;<br />
}<br />
else<br />
{<br />
return false;<br />
}<br />
}<br />
else<br />
{<br />
return false;<br />
}<br />
}<br />
<br />
public override int GetHashCode()<br />
{<br />
int HashCode = a ^ b ^ c;
return HashCode;<br />
}<br />
}<br />
class Program<br />
{<br />
static void Main(string[] args)<br />
{<br />
IDictionary<SomeClass, string> SomeDict = null;<br />
SomeClass o = null;<br />
const int MAXITEMS = 1000000;<br />
try<br />
{<br />
#region Add to dictionary, with duplicate keys!<br />
SomeDict = new Dictionary<SomeClass, string>(MAXITEMS);<br />
for (int j = 0; j < 1; j++)<br />
{<br />
for (int i = 0; i < MAXITEMS; i++)<br />
{<br />
o = new SomeClass();<br />
o.a = i;<br />
o.b = i * 2;<br />
o.c = i * 3;<br />
o.d = i * 4;<br />
<br />
SomeDict.Add(o, Guid.NewGuid().ToString());<br />
}<br />
<br />
Console.WriteLine("Pass #" + j);<br />
}<br />
#endregion<br />
Console.WriteLine("Finished adding elements to dict....");<br />
<br />
#region Retrieve from dictionary<br />
DateTime Start = DateTime.Now;<br />
foreach(KeyValuePair<SomeClass, string> Entry in SomeDict)<br />
{<br />
string Value = SomeDict[Entry.Key];<br />
}<br />
#endregion<br />
DateTime End = DateTime.Now;<br />
<br />
Console.WriteLine("Duration(sec):" + End.Subtract(Start).TotalSeconds);<br />
}<br />
catch (Exception Ex)<br />
{<br />
Console.WriteLine(Ex.Message);<br />
Console.ReadLine();<br />
}<br />
<br />
return;<br />
}<br />
}<br />
}<br />
dev
|
|
|
|
|
Hello Everybody,
I am developing an application in which have we are providing Full Company Address to access or print.They are fixed and not need to be change.
So i am confusing where to place these FullAddress (XMLFILE,DataSet,Database,Or Arraylist).
So pls suggest me where is the best location to place these 1000 FullAddresses.
If you can think then I Can.
|
|
|
|
|
If the application uses a database, it would be simple to add an address table with a column companyID that links to another table company . Then you could retrieve the address as and when you need it. It would be no good keeping it in a In-Memory structure such as DataSet / ArrayList as you would need to Hard Code the values in the code. A viable alternative is to persist the addresses using XML, if the application is not using a database. Obviously, this would depend on the application itself.
Hope this helps.
...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....
|
|
|
|
|
eg_Anubhava wrote: They are fixed and not need to be change.
Experience shows, this is unlikely.
Where to place them is a tricky one. I'd narrow it down to XML or a database, that way they can be changed/added to with little effort (and used, for example, in word for mail-merge) and without needing a re-compile.
You might want to google "LINQ To SQL" or "Entity Framework" for two simple ways of getting the data out in a C# app. LINQ to SQL is probably easier for your requirements.
Hope this helps!
|
|
|
|
|
Dear Sir,
Think that you have 1000 Rules of Government that you want Store. Then what will you do. Because there is no need to be insert.
and there no need to install SQL for client machine.
The Client Just want to View and Print and Filter these records.
If you can think then I Can.
|
|
|
|
|
Well, I'd do it in a database anyway, databases are designed to store data. XML is a less helpful choice, but both either can then be used by other sources (as opposed to hard coding).
eg_Anubhava wrote: Think that you have 1000 Rules of Government that you want Store. Then what will you do. Because there is no need to be insert.
Until the 1001th rule is added.
|
|
|
|
|
But For Database Option Database should be installed on Client Computer.
If you can think then I Can.
|
|
|
|