|
To clarify further:
The class which contains your lock (and method), should be instantiated only once, preferably before starting your threads. You should then pass this class object to your threads, so that they all look at the same object.
Do NOT instantiate the class from within the threads, as this will create separate objects (and separate locks)
class LockObject
{
private Object MyLock = new Object();
void WriteMessage(string msg)
{
lock(MyLock)
{
...
}
}
public void Dowrite()
{
while(true)
{
WriteMessage("Whatever");
}
}
}
</code>
<code>class Test
{
static void Main()
{
Thread[] threads = new Thread[10];
LockObj = new LockObject();
for (int i = 0; i < 10; i++)
{
Thread t = new Thread(new ThreadStart(LockObj.DoWrite));
threads[i] = t;
}
for (int i = 0; i < 10; i++)
{
threads[i].Start();
}
}
}
|
|
|
|
|
Good clarification Michel. This states very nicely what I obviuosly failed to say
|
|
|
|
|
Those who can, do; Those who can't, teach.
|
|
|
|
|
Could he make class A a static class and not need to deal with the instantiation or is this frowned on in threading?
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I suppose he could, but this should only be used when writing a complete program, where you control every aspect of the project.
If on the other hand, you're writing a class library, where you want to ensure that some classes/methods are thread safe, it's best to keep the mutex objects together with the data they're supposed to be protecting.
I usually have one static object in the program, which contains references to resources that must be shared between threads. If a thread needs to instantiate a new shared object, it will use a method in that class, and the new() statement will be protected by a lock. In this way, I can ensure that objects which should be instantiated once are initiated only once.
This is basically an implementation of the singleton pattern.
http://www.yoda.arachsys.com/csharp/singleton.html[^]
|
|
|
|
|
Thanx a lot.
Very helpful.
I will try to achieve same logic in my code.
regards
|
|
|
|
|
Thanx for your time and help.
I modified the code like you said, and now its working correctly. As it suppose to.
Just one more issue if give me helping hand.
About threading could you suggest me good book(simple in language and understanding)
Also when i wrote any code i always wonder "IS this really Good or Bad(i just done the job in ugly way)"
How can i self analyze, optimize my code.
Like resource monitoring, memory consumption. where to start/self-learn these topics.
|
|
|
|
|
Not much of a reader (at least not those kinds of books, the last IT book I read was Windows Internals by Russinovich and Solomon, and THAT would give you splitting headache, especially if it fell on your head )
Why don't you post the question in General IT issues, or in the Lounge?
|
|
|
|
|
hi,
i am having problem in date time function while i am doing windows application.i am always getting date along with constant time 12:00:00AM.Which i dont want it.i want the exact time .plz give me some idea how can i do this in windows application.
THANKS IN ADVANCE.
|
|
|
|
|
DateTime.Now;
Harvey Saayman - South Africa
Software Developer
.Net, C#, SQL
you.suck = (you.Occupation == jobTitles.Programmer && you.Passion != Programming)
1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111
|
|
|
|
|
Where are you getting this date time value from? If it is from a database, then you may need to correct this in your query.
|
|
|
|
|
I'm not sure I understand. DateTime.Now will get the current date and time from the system. If you just want the time part and not the date, then you can use DateTime.TimeOfDay.
A good summary of time and Date handling in .NET is at http://dotnetperls.com/datetime-1[^]
CQ de W5ALT
Walt Fair, Jr., P. E.
Comport Computing
Specializing in Technical Engineering Software
|
|
|
|
|
show code!
|
|
|
|
|
I needed 2 instances of a complete string array and I did write like this
string[] bigArray = new string[int.MaxValue];
Now, I sure got Out of memory exception, which make sense. But I couldn't find any way to make a big array. I need it to get the value very quickly.Like I write this
int somevalue = 15632427;
string tvalue = bigArray[somevalue];
I have these types of values in thousands, I tried to iterate each item which were stored in a List but it was too slow.
The last thing left is to use Dictionary but it can't beat array. Now, I'm just thinking if any of you guys know any better way ?
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%
R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
-----------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|
|
What version of the framework are you using? If you are using 3.5 you can use Linq to find items in a collection rather iterating
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
I'm using 2.0, I usually use it for public application as lots of people don't get updated.
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%
R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
-----------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|
|
Then you are making it more difficult on yourself
Most will get it with Windows update because they accept the defaults
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Mark Nischalke wrote: Then you are making it more difficult on yourself
Yes it is, but not that much I can't handle.
Mark Nischalke wrote: Most will get it with Windows update because they accept the defaults
But some guys don't update. And I hate when they send me email or shout in my site about app crashing.
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%
R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
-----------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|
|
Keeping in mind that linq just performs the iteration on your behalf. It will still be just as slow.
If you have a large keyspace, and the keyspace is sparsely filled, and you wish to look up values by key quickly. Then you should definitely, definitely, use a dictionary instead of a list or an array.
|
|
|
|
|
Xmen W.K. wrote: string[] bigArray = new string[int.MaxValue];
Are you really planning on using 2.1 Billion strings in your app? I seriously doubt it.
|
|
|
|
|
As I wrote above, I needed to retrieve value quickly. I knew what I was doing, just asked for better way. So please read the question properly.
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%
R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
-----------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|
|
A dictionary is designed to retrieve a key quickly.
|
|
|
|
|
Read the last line[^]
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%
R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
-----------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|
|
A dictionary isn't much slower than an array for lookup values and it also gaurantees constant time lookup no matter how many entries are in the dictionary.
Besides, You were going to try a list? Lists are shitloads slower than a dictionary for lookup performance. You need to get a basic understanding of data structures;
Dictionary is pretty much your only choice.
|
|
|
|
|
oh common man, what are you ? dont you get it yet ? I just asked if there is better way or NOT ?
thats it... I know what is List, Dictionary or Array. So please if you have anything better then come with that not with what I've said above.
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%
R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
-----------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|