|
OK I understand, please only two questions more:
1) I am using ThreadPool for doing a repetitive job
in the way WaitCallBack WCK = new WaitCallBack(Method1);
ThreadPool.QueueWorkItem(WCK, List1);
Question: is needed that Method1 be Synchronized ?
2) If The Method1 call a Synchronized Method, and this second method call a third Method is needed that the third method be also synchronized ?
regards
|
|
|
|
|
FJJCENTU wrote: is needed that Method1 be Synchronized ?
Methods don't need to be synchronized, accesses to shared data should.
As I said: all data that is going to be read/written by several threads at once must be protected
Here is a simple example of how things CAN and eventually WILL go wrong:
List<int> power1=new List<int>();
List<int> power2=new List<int>();
public void Add(int x) {
power1.Add(x);
power2.Add(x*x);
}
Then two threads are created that generate random numbers x and call Add, hoping to obtain a list of numbers, and in parallel a list of their squares. Everything will run fine; suddenly the operating system will decide to temporarily halt thread 1 (which was at point A), and resume thread 2.
So the lists will contain
power1 a b c x1 x2 ...
power2 a*a b*b c*c x2*x2 ...
and it is only when thread1 becomes active again that x1*x1 gets added to list power2.
The solution could be like this:
object powerLock=new object();
public void Add(int x) {
lock(powerLock) {
power1.Add(x);
power2.Add(x*x);
}
}
so whenever some thread is inside the critical region protected by powerLock, no other thread needing the data protected by powerLock can but in.
FJJCENTU wrote: a third Method is needed...
as I said, it is not methods that get protected, it is data. And the example should make that clear.
Luc Pattyn
Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
I know how to build a dynamic array and initilize the array. This is not my problem. That part of my code is working just fine and there is to much code to change the logic. So using the consepts of dynamic arrays... I run into a issue. I know it is simple but I can not find information about it.
The issue is this... If my sql statement returns to results the array is not built. Which cause the applciation to error. Is there a way with out throwing a lot of try catch's in my code to do something like
x = DynamicArray.Length
And if the array was not initialized for it to return a numeric value of -1 instead of it erroring saying the array is not initialized?
|
|
|
|
|
Do you mean something like
x = DynamicArray == null ? -1 : DynamicArray.Length;
?
|
|
|
|
|
Quite possibly but I don't understand what the statement is doing exactly. THe '?' and everything after how does that work in c# language?
I am guessing essentially what the statement is saying is:
x = -1 if the dynamic array is null by placeing the -1 value in to the Dynamic array as a result when it is called.
If this is the case I see many examples I could use this functionality. Is there something I can read or you could explain so I can adapt that to future logic?
|
|
|
|
|
x = DynamicArray == null ? -1 : DynamicArray.Length;
is just an alternate (shorter) syntax for:
if (DynamicArray == null) x = -1; else x = DynamicArray.Length;
|
|
|
|
|
Oh it's just the ternary operator
result = condition ? true-value : false-value
It just selects one of the values based on whether the condition is true or not.
So basically it's
if (condition)
result = true-value;
else
result = false-value;
But as an expression, instead of a statement
|
|
|
|
|
Nope x = DynamicArray == null ? -1 : DynamicArray.Length; is shorthand for
int x = -1;
if(DynamicArray != null)
x = DynamicArray.Length;
|
|
|
|
|
Thanks I think I have a better Idea and something new to play with. 
|
|
|
|
|
Hm you raise an interesting point - is the second argument always evaluated?
|
|
|
|
|
No, but the ternary operator syntax is unclear if you haven't seen it before.
|
|
|
|
|
So... let me get this straight.. does it act like the expression form of
result = arg1;
if (!condition)
result = arg2;
Or like:
result;
if (condition)
result = arg1;
else
result = arg2;
|
|
|
|
|
The second one - see also my comment below.
|
|
|
|
|
Good good, my world is back to normal, thank you
|
|
|
|
|
Like the Second:
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int x = true ? ++a : ++b;
Console.WriteLine("a={0}, b={1}", a, b);
int y = false? ++c : ++d;
Console.WriteLine("c={0}, d={1}", c, d);
outputs:
1, 0
0, 1
Meaning that the "false" value of the ternary operator never gets evaluated (infact VS highlights it as unreachable).
|
|
|
|
|
Ok so it's like I've always thought, but then, why did you post that other way?
|
|
|
|
|
for (int i = 0; i < NewServerVersionTableArray.Length)
{
if (NewServerVersionTableArray[i].AssistSuiteApplication.MasterApplication = true.ToString())
{
string MyAppLocation = NewServerVersionTableArray[i].AssistSuiteApplication.PAppLocWS;
string MyAppName = NewServerVersionTableArray[i].AssistSuiteApplication.FAppNameWS;
Process MyApplicationOpen = NewServerVersionTableArray[i].AssistSuiteApplication.MasterApplication = true.ToString() ?
File.GetAccessControl(MyAppLocation + "\\" + MyAppName)
Process oAccess = Process.Start(MyMSAccessPath, MyAppLocation + "\\" + MyAppName)
:
null
}
}
Basically if MasterApplicaiton = true to use the tirshery command to try and open a file?
|
|
|
|
|
What is it supposed to do?
|
|
|
|
|
Essentially
Check a boolean variable that identifies if a application is told it is to startup.
If the boolean value is true then startup the application. If the boolean value is false skip and continue on to the next applicataion (essentially do nothing).
|
|
|
|
|
 So essentially here is how I changed the code around...
I think I am correct in the logic.
Can you confirm?
for (int i = 0; i < NewServerVersionTableArray.Lengthl; i++ )
{
string MyAppLocation = NewServerVersionTableArray[i].AssistSuiteApplication.PAppLocWS;
string MyAppName = NewServerVersionTableArray[i].AssistSuiteApplication.FAppNameWS;
string MyMSAccessPath = PeerAssistLoader.Properties.Settings.Default.DefaultMSAccess;
if (File.Exists(MyMSAccessPath))
{
Process MyOpenApp = StandardProcedures.Convert_TrueFalse(NewServerVersionTableArray[i].AssistSuiteApplication.MasterApplication) ? Process.Start(MyMSAccessPath, MyAppLocation + "\\" + MyAppName) : null;
bool result = MyOpenApp == null ? StandardProcedures.ErrorLogWriter("Unable to Open the Application: " + MyMSAccessPath + " " + MyAppLocation + "\\" + MyAppName) : StandardProcedures.DebugLogWriter("Opened the Application: " + MyMSAccessPath + " " + MyAppLocation + "\\" + MyAppName);
bool result = MyOpenApp == null ? StandardProcedures.DebugLogWriter("Unable to Open the Application: " + MyMSAccessPath + " " + MyAppLocation + "\\" + MyAppName) : StandardProcedures.DebugLogWriter("Opened the Application: " + MyMSAccessPath + " " + MyAppLocation + "\\" + MyAppName);
}
}
|
|
|
|
|
Better yet would this be the correct way of doing this?
Process MyOpenApp = StandardProcedures.Convert_TrueFalse( NewServerVersionTableArray[i].AssistSuiteApplication.MasterApplication) ?
Process.Start(MyMSAccessPath, MyAppLocation + "\\" + MyAppName) : null
|
|
|
|
|
Pure Laziness.
int x = -1;
if(DynamicArray != null)
x = DynamicArray.Length;
Is shorter than
int x;
if(DynamicArray == null)
x = -1;
else
x = DynamicArray.Length;
When I started to reply, no-one had (at that point) explained what the ternary operator did, I wasn't trying to make a point that my OP was better or more accurate or anything, someone just beat me to the punch. If I had to implement the length property , and it needed to return -1 in the manner requested in the top post I'd actually do it like this:
public const int UninitializedLength = -1;
public int Length
{
get
{
if(DynamicArray == null)
return UninitializedLength;
return DynamicArray.Length;
}
}
|
|
|
|
|
Not true; Ian's answer above is more nearly the correct one. The value of x is not set until the expression has been evaluated, at which time it is set to the value of the 'true-value' or the 'false-value' as appropriate. Since true-value and false-value may also be expressions, even ternary expressions, only one of the values will ever be evaluated, as is the rule for all if/else expressions. Also in the expression
x = e ? true : false;
the type of x must have been previously declared, it is not inferred from the context.
|
|
|
|
|
x = DynamicArray == null ? -1 : DynamicArray.Length;
Let me see if I understand this somewhat...
could I use this in such a way... no i don't think so I think I might need to
What i was think was this...
<array not="" initilized="">
private void Procedure1(ref MyArray[] DynamicArray)
{
<array thru="" logic="" determined="" not="" to="" create="" array="">
dynamicarray.length = DynamicArray == null ? -1 : DynamicArray.Length;
}
if (dynamicarray.length = -1)
{
MessageBox.Show("No Array Created")
}
|
|
|
|
|
No, the length is read-only (and even if it weren't, it would probably have checked for the length to be >= 0)
PS: you don't need to ref an array unless you intend to replace the array entirely
|
|
|
|