|
Yeah well... I know that much...
I meant to ask what happens when we use the lock statement...?
I know that the compiler translate it to somthing like the following:
try
{
Monitor.Enter();
}
finally
{
Monitor.Exit();
} So how is the Monitor class implemented?
|
|
|
|
|
Ah hmm. You could read these articles, they could probably answer your questions. Link1[^], Link 2[^], Link 3[^]
Tarakeshwar Reddy
MCP, CCIE Q(R&S)
|
|
|
|
|
Hmm... Those all discuss how to use locking mechanisms, but again... That's not what I'm looking for...
I would like to know how is the Monitor.Enter() implemented.
Let's say that foo() (in my original post) is Enter() .
How is it guaranteed that two threads would never evaluate the lock Monitor aquired at the same time, and enter the actual lock aquiring statement at the same time?
|
|
|
|
|
From this[^] excellent blogpost by Joe Duffy :-
As you may have guessed, Monitor actually uses Windows events underneath it all. Each CLR thread has a manual-reset event, allocated when it is created (or lazily when the thread first wanders into managed code). When a Wait is issued, this per-thread event is stuck on the tail of a linked list associated with the target object’s sync block. We can use a single event per thread since a thread can only ever be waiting for a single object at a given time. (You can’t do a WAIT_ANY or WAIT_ALL on monitors.) The thread then releases the lock on the object (accounting for any recursion), waits on this event, and then reacquires the lock on the object (again, accounting for any recursion). When a Pulse is issued, the head of the object’s linked list of waiters is popped off and its associated event is signaled. Similarly, PulseAll clears the entire linked list and signals all of the events. Notice I said that Pulse operates on the head of the list: we use a strict FIFO ordering (as of 2.0)....
|
|
|
|
|
Cool
|
|
|
|
|
Calls to System.Threading.Monitor.Enter() and System.Monitor.Exit() marks the critical section of the code. Like,
System.Threading.Monitor.Enter(this);
System.Threading.Monitor.Exit(this);
(System.Threading.Monitor is one of the .NET base class).
Now, what ever thread make call to the Monitor.Enter() first, this call will ensure that the first thread will finishes first (which is marked by calling the Monitor.Exit() method), before any other thread gain access to the code/method.
________________________________
Success is not something to wait for, its something to work for.
|
|
|
|
|
In the end it boils down to using a processor instruction that sets a value and gets the previous value as an atomic operation. In pseudo code this would be:
static int status = 0;
if (status == 0) {
int prevStatus = status.SetNewAndGetPrevious(1);
if (prevStatus == 0) {
status = 0;
} else {
}
}
This is used somewhere deep in the code that creates monitors.
---
It's amazing to see how much work some people will go through just to avoid a little bit of work.
|
|
|
|
|
i have created a list view and have ways to put in information, edit it, and delete one at a time. But i cannot get it to delete more than one at a time. My code is:
if (listView1.SelectedItems.Count > 1)
{
int SelectedItemCount = listView1.SelectedItems.Count;
int usableSelectedItemCount = SelectedItemCount - 1;
if (MessageBox.Show("Are you sure you want to delete these " + SelectedItemCount + " items?", "Confirm Delete",
MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2, 0) == DialogResult.Yes)
{
for (int Removed = 0; Removed < usableSelectedItemCount; Removed++)
{
listView1.Items.Remove(listView1.SelectedItems[Removed]);
}
}
else if (listView1.SelectedItems.Count == 1)
{
if (MessageBox.Show("Are you sure you want to delete?", "Confirm Delete", MessageBoxButtons.YesNo,
MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2, 0) == DialogResult.Yes)
{
listView1.Items.Remove(listView1.SelectedItems[0]);
}
}
Did i mess up by approaching the for loop incorrectly? What can i do to fix it?
Thanks for the help.
(and sorry about the long code)
|
|
|
|
|
This code might help you...
if (listView1.SelectedItems.Count > 0)
{
int SelectedItemCount = listView1.SelectedItems.Count;
int usableSelectedItemCount = SelectedItemCount - 1;
if (MessageBox.Show("Are you sure you want to delete these "
+ SelectedItemCount + " items?",
"Confirm Delete", MessageBoxButtons.YesNo,
MessageBoxIcon.Warning,
MessageBoxDefaultButton.Button2, 0) == DialogResult.Yes)
{
foreach (ListViewItem item in listView1.SelectedItems)
{
listView1.Items.Remove(item);
}
}
else if (listView1.SelectedItems.Count == 1)
{
if (MessageBox.Show("Are you sure you want to delete?",
"Confirm Delete", MessageBoxButtons.YesNo,
MessageBoxIcon.Warning,
MessageBoxDefaultButton.Button2, 0) == DialogResult.Yes)
{
}
}
}
Look at the if condition for greater than one.
Jayant D. Kulkarni
Brainbench Certified Software Engineer in C#, ASP.NET, .NET Framework and ADO.NET
|
|
|
|
|
How can I get a reference of the calling Assembly at Runtime?
I've an assembly (RestrictedAssembly) and I want to ristrict access to it means no user who fullfills my criteria (in my case not having my desired public key token for assembly) can instantiate any type from my assembly.
For better understanding, this is what I am trying to do.
I've generated a public/private keypair file for myself, then I use it to sign all those assemblies which I want to allow Access to my RestrictedAssembly. On Runtime before instantiating any type from RestrictedAssembly, in constructor I am asking the caller to provide a Type object from which he's initiating a call to constructor. For example.
public RestrictedType1(Type o) {
Type t = this.GetType ();
byte [] t1Token = o.Assembly.GetName().GetPublicKeyToken();
byte [] t2Token = t.Assembly.GetName().GetPublicKeyToken();
AccessNotAllowedEx ex = new AccessNotAllowedEx("Not Allowed");
if(t1Token != null && t2Token != null) {
if(t1Token.Length == t2Token.Length) {
for(int i = 0; i< t1Token.Length; i++) {
if (t1Token[i] == t2Token[i])
continue;
else
throw ex;
}
ex = null;
} else
throw ex;
} else
throw ex;
}
I am checking, if the public key tokens for both the calling and called assemblies are identical and if they are, then I am allowing the caller to instantiate my RestrictedType other wise I raise an exception.
Now the problem with this approach is that, suppose I've an Assembly CallerOfRestrictedAssembly and I've signed it with my public/private key pair. Now from CallerOfRestrictedAssembly it is valid to do the following. Since both assemblies have same public key token.
RestrictedType1 res = new RestrictedType1(this.GetType);
However, suppose if some one initiate a call to RestrictedType1 constructor
RestrictedType1 res = new RestrictedType1(typeof(TypeFromCallerOfRestrictedAssembly));
This will cheat the constructor. Thus, I don't want caller to Pass Type argument into Constructor. and depend on it. What I am looking for is to get a reference of Calling Assembly into my RestrictedAssembly at Runtime?
I'll apreciate if some can help with this or any other approach to achieve this level of code restriction? Thanks.
SMK
|
|
|
|
|
Please don't cross post.
---
It's amazing to see how much work some people will go through just to avoid a little bit of work.
|
|
|
|
|
Syed Muhammad Kamran wrote: How can I get a reference of the calling Assembly at Runtime?
You mean something like Assembly.GetCallingAssembly() method?? Don't forget to reference the System.Reflection namespace.
PS.
Even this got ways to be cracked with.
Regards
|
|
|
|
|
There are ways to even crack this.....
You mean like this??
Assembly asm = Assembly.GetAssembly ( Type ( MyRestrictedAssembly ) );
byte[] b = asm.GetName ().GetPublicKey ();
this.GetType ().Assembly.GetName ().SetPublicKey ( b );
TypeFromRestrictedAssembly temp = new TypeFromRestrictedAssembly ( this.GetType ().Assembly );
SMK
|
|
|
|
|
Well, not exactly. You can crack it using MSIL dissassembler. Just disassemble the code, copy/paste it, or remove the few lines that check for the calling assembly and save the dll files. Yes, it isn't that easy, but it's still doable. If you are not expecting hardcore persistant crackers to hut your application, it would be a nice way of protecting someone from using your code by referencing. Another way to protect your program from being used by reflection also could be the Dotfuscator. It's a small freeware tool to obfuscate your cde against disassembling. Still crackable, but a bit harder.
Regards
|
|
|
|
|
using TcpClient, i have made a client Server chat application. Server is running on another computer. in Client side i use the server IP address and PORT number to connect. If the server is running Any Computer the Client should automatically find the server Ip address how it is possible?
//
now i am using like this
Client Side
TcpClient tcp=new TcpClient();
tcp.Connect("192.0.0.3",8888);
//
Sanjit.rajbanshi@wlinktech.com
|
|
|
|
|
You would need to broadcast to all the machines on the network and the machine running the server could send back a reply and you can connect to it.
It is similar to how your DHCP works.
Tarakeshwar Reddy
MCP, CCIE Q(R&S)
|
|
|
|
|
I'm setting up an authentication system for access to a server through a remoting layer.
Using custom channel sinks to do this doesn't seem to be a problem.
The problem is that if someone is authenticated as, say "bob" within the remoting layer, then the remote object also needs to know that the method was called by "bob".
Is there any way to pass extra information like this from the remoting layer to the application layer without it being a parameter of the remote method called? Some sort of shared remoting context object, or the like?
|
|
|
|
|
|
|
Unfortunately you can't call assembly code directly from managed code. There is one workaround I can think of; That is to put your assembly code in a C++ -whether managed or not- dll library and call it from your C# code.
In your C++ project you can put your assebly as inline -but IIRC this doesn't allow interrupts to be called-, or put it as an asm file and link it with your project. I did that sometime ago and it worked.
Regards
|
|
|
|
|
can you please explain me in depth.
nishu
|
|
|
|
|
Forgive me if my reply was too shallow.:-> I'll try to fill you with more details, but forgive I still got to be breif as there are too much details that I can't cover in one post.
Now, the interrupts you are talking about are in Assembly code which is a very low level language very close to machine code. These are some sort of a very low level functions that are hardcoded in the processor. Some sort of a processor specific API that should be called at the lowest level of programming... The machine code.
Before the DotNet emergance, all exe files were in assembly/machine code. Every compiler was to compile any code into assembly/machine code. Now in DotNet the compiler actually compiles you code into MS Intermediate Language, which is not machine code. When you double click a DotNet exe file, the DotNet framework actually does the compilation into the machine code in the memory. This is called JIT -Just In Time- compilation. Why did they do that?? It's quite long to say.
The assembly language itself has something similar to a compiler that is called assembler, in which you could write your assembly code directly and convert it to exe file. When C++ emerged they made a feature in MS C++; That is to write assembly code either within your C++ code in asm blocks -something like the unsafe, or try blocks of C#-. Ans Since they all will turn into machine code in the end, it didn't really matter. Yet using this method -i.e. inline assembly block-, didn't support interrupt calls -remember, interrupts are like functions hardcoded in the processor-.
The other way is to write your assembly code as you would in an assembler. Put it in a asm file, and link -something like referencing- that asm file in your C++ project. This was the way if you want to write large code blocks in assembly, and keep the main bulk of your program in C++. Again all the .cpp and .asm file wil be mixed up in one big .exe file written in machine code.
Now, in C# that is not possible. As your C# code is converted it MSIL language which is different that the conventional machine code. So you can't mix up assembly with MSIL...
Oh!! And sorry! You can't call BIOS interrupts in any other language than assembly.
PS.
This is a forum post. All the info above were ment to be as accurate as possible, but I had to omit a lot of details -Yes I did... Don't ROTFL-. So any inaccuracy in the info was not meant, and anyone is welcomed to explain/correct any mistake.
Regards
|
|
|
|
|
you gave very deeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeep
info..I wanted to know the libraries that can be used to create c++ dll files coz you had done this before.
anyway thanx for your deeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee....................................................................................................................................................................................................p information
nishu
|
|
|
|
|
Your welcome.
You don't need any special libraries to make a C++ library. You can make an MFC library and do as I said. The way I did it was to make an empty project totally from the scratch. and change the compile output to dll. This had more work to do, but give you more control -also the way to do it in MFC gave me headache-. I'll look in my HDD for that old project. If I find it I'll give you more details. There were some compiler options that had to be set -I don't remember them right now-.
PS.
Do you know how to program in Assembly language?
Regards
|
|
|
|
|
at first i tried using methods, like Change(string name), but it didn't work across forms. How would i go about transfering this information?
Thanks.
|
|
|
|