|
Hello to all.
I'm new in this message board, and I couldn't find this subject elsewhere.
The thing is, I need to know if I can use the lock satement inside an overloaded method, i.e.:
public static string MyString = 0;
private static object objMyLock = new object();
public static void DoSomething(int Index, string Field)
{
lock (objMyLock)
{
MyString = Field + Index.ToString();
}
}
public static bool DoSomething(string Name, string Field)
{
lock (objMyLock)
{
MyString = Field + Name;
}
}
Does the lock Statement used like this, ensures that the variable MyString is changed by one thread at a time?
Thank you!
NMFPacheco - Portugal
|
|
|
|
|
"Does the lock Statement used like this, ensures that the variable MyString is changed by one thread at a time?"
Yes it should ensure.
But I wouldn't work with static objects / methods / etc..
|
|
|
|
|
Thanks for your reply.
"But I wouldn't work with static objects / methods / etc.."
For any special reason? Unsafe code/other, or simply not the best way?
|
|
|
|
|
At first I think, to implement a the basis of little modules/classes, is a better
way to solve problems.
Classes are in general more flexible through inheritance, access rights etc.
Example:
public class MultiThreadingClass
{
public MultiThreadingClass()
{
}
private object m_objSync = new object();
private int m_nValueA = 0;
private int m_szText = string.Empty;
public SyncObj
{
get
{
return m_objSync;
}
}
public Value
{
get
{
lock(objSync)
{
return m_nValue;
}
}
set
{
lock(objSync)
{
m_nValue = value;
}
}
}
public Text
{
get
{
lock(objSync)
{
return m_szText;
}
}
set
{
lock(objSync)
{
m_szText = value;
}
}
}
}
Thats not the best way to implement it, but out of this class you can be sure of the values (thread-safe). And if you need extern sync you just have to call lock([classInstance].SyncObj);
One more profit is, that you can have more than one instance of this class.
I hope I could explain you a little bit what I meant.
|
|
|
|
|
Thank you Covean,
I did understand and agree with you. But, in my specific case, I can not change the class where I am using the lock.
Besides that, the question is, if it is possible to use the same lock for covering all overloads of the same method.
Nuno
|
|
|
|
|
Yes if you use everywhere the same sync object.
In your case a static sync object maybe is also the better way.
|
|
|
|
|
i have a client application i which i need to connect to the remote machine on click of a button and localhost on every minute.is it possible. plz help me.
|
|
|
|
|
Can you be more specific as to how you want to connect to a machine ?
Is it a database connection you wish to establish ?
|
|
|
|
|
Bjorn Spruit wrote: Is it a database connection you wish to establish ?
No its not a database connection. it is a socket connection between the client and the server of the remote machine
|
|
|
|
|
Yes.. On every client request create a new thread and run the task in its own thread until it returns to its tread pool.
Just like what IIS does for every requests.
|
|
|
|
|
can u be more specific im not getting any idea about it.
you mean to say when client send or recieve a msg run a new thread and reconnect to the server
|
|
|
|
|
i have tried running threads in this format. but this also works only if button is clicked and only for 4 times but
i wish 1 client to connect to the server when button is clicked and other client will connect and close for each minute.
the code is:-
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 4; i++)
{
Thread thread = new Thread(new ParameterizedThreadStart(round));
thread.Start();
thread.Join();
Thread.Sleep(100);
}
}
private void round(object val)
{
System.Net.IPAddress myIP;
System.Net.IPEndPoint myServer;
Socket socket;
string ip = (string)val;
try
{
myIP = System.Net.IPAddress.Parse(ip);
myServer = new System.Net.IPEndPoint(myIP, Int32.Parse("20000"));
socket=new Socket(AddressFamily.InterNetwork,SocketType.Stream ,ProtocolType.Tcp);
socket.Connect(myServer);
NetworkStream netStream=new NetworkStream(socket);
Byte[] byteMessage=new Byte[640];
string sendMessage="hello server";
byteMessage=System.Text.Encoding.Default.GetBytes( sendMessage.ToCharArray());
netStream.Write(byteMessage,0,byteMessage.Length);
netStream.Flush();
netStream.Close();
socket.Close();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
|
|
|
|
|
I'm writing strings at a pre-determined location to a bitmap.
Though, it seems that if the location isn't "snapped to the grid", blurring occurs.
Simple example:
public Bitmap WriteToBitmap(Bitmap bitmapToWriteTo, float X_Pos, float Y_Pos)
{
Graphics g = Graphics.FromImage(bitmapToWriteTo);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.TextRenderingHint = TextRenderingHint.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBilinear;
g.DrawString(this.text, this.font, new SolidBrush(Color.Black), X_Pos, Y_Pos, this.stringFormat);
return bitmapToWriteTo;
}
Nothing else is being done to the bitmap object or any of the other objects involved.
So you may consider the code above as being used as is.
The problem is, however, that using it like this, causes blurring in the text when drawn.
There is a solution to stop the blurring and that is by changing the TextRenderingHint:
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
Now, this causes the text being drawn to be "snapped to a grid".
However, this messes up my positioning, making it look chaotic.
My question is this, how can I get the result of TextRenderingHint.AntiAliasGridFit, without losing the positioning.
And, to what "grid" is the text being snapped to and am I able to manipulate it ?
|
|
|
|
|
Here's the solution as I can tell it at this point.
Dropping the following code solved the bluring for me:
g.SmoothingMode = SmoothingMode.AntiAlias;
g.TextRenderingHint = TextRenderingHint.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBilinear;
From what I understood by reading Microsoft supplied information and the experiences from other developers, it's the Glyph hinting that causes the problem.
I increased the DPI of the originating Bitmap and dropped the hinting.
Problem solved.
Some positioning adjustments are required, but nothing I can't fix.
Hope this will help others as well.
|
|
|
|
|
Sorry, its not to the problem itself, but I'm wondering.
You say "it's the Glyph hinting that causes the problem".
But on MSDN it says that TextRenderingHintAntiAlias does no hinting.
From MSDN: "TextRenderingHintAntiAlias:
Specifies that a character is drawn using its antialiased glyph bitmap and no hinting. Stem width differences may be noticeable because hinting is turned off. "
|
|
|
|
|
True, that's what they say.
I suggest you try it out and see if there's Glyph manipulation.
Set the TextRenderingHint property of your Graphics object:
g.TextRenderingHint = TextRenderingHint.AntiAlias;
See how it's displayed.
Then remove it and compare the two.
Even though it's already visibly apparent, I would like to urge you to print the two results and compare them.
In my humble opinion, the AntiAlias touches the Glyph.
But I could be mistaken.
If I am mistaken and somehow I seem to have missed what the default does as opposed to what AntiAlias does, then please inform me.
Don't forget to add an informative link, not just for myself, but for others coming across this problem.
Thanks in advance.
Edit:
I indeed stand corrected.
Here's a link explaining how it works:
http://msdn.microsoft.com/en-us/library/ms534404%28VS.85%29.aspx
By setting the TextRenderingHint to AntiAlias, you kill the default corrective Glyph hinting the Graphics object does by itself.
Quote:
"TextRenderingHintSystemDefault
Specifies that a character is drawn using the currently selected system font smoothing mode (also called a rendering hint)."
Quote:
"TextRenderingHintAntiAlias
Specifies that a character is drawn using its antialiased glyph bitmap and no hinting. Stem width differences may be noticeable because hinting is turned off."
Thank you Covean for this insight
modified on Thursday, October 22, 2009 9:07 AM
|
|
|
|
|
I can see the difference. And yes I also think it touches the Glyph, but there so no hinting.
The Problem with the "no hinting" is "Stem width differences may be noticeable because hinting is turned off."
Edit:
Oh I read your edit too late.
|
|
|
|
|
Not a problem, your thoughts in this have been insightful.
|
|
|
|
|
The plot thickens...
I was somewhat bored and still not satisfied with how the Graphics.DrawString() was working for me.
So I went on a Google hunt for some answers.
After looking around I came to a forum where a user had somewhat similar problems.
There he had linked a convo he had with a Microsoft MVP.
For more information please check this link:
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/362ab21b-1dc4-4140-a39a-a366beea9e40[^]
Quote:
"They couldn't fix the bugz in Graphics.DrawString() because way too much .NET 1.x code depended on its quirks. That's what Application.SetCompatibleTextRenderingDefault() is all about too. If you really want to use GDI+, you'll have to find the workarounds for the bugs yourself. Right now, using Graphics.DrawString() in a Paint event is a bug."
Quote:
"Graphics.DrawString() is too broken to be usable. It got replaced by TextRenderer.DrawText()."
To simplify things:
If you care for precision work, in particular with text rendering, then use the old GDI.
GDI+ is bugged and unusable for precision work.
If you don't care for precision work and you don't use text rendering (or very little of it or the quality isn't important), then use the new GDI+.
|
|
|
|
|
I faced the same problem, thanks
modified 1-Aug-19 21:02pm.
|
|
|
|
|
hi
How to pass any text value from form to Crystal Report ?
ex: i need to pass "hellow" from Form1 to my Crystal Report , how to do it ?
(working in C#)
thank's in advance
|
|
|
|
|
You create paramaters in crystal report. via that parameters u can pass
___________
rptCount = New ReportDocument
rptCount.Load(Server.MapPath("reportname.rpt"))
''Get the collection of parameters from the report
crParameterFieldDefinitions = rptCount.DataDefinition.ParameterFields
''Access the specified parameter from the collection
crParameter1 = crParameterFieldDefinitions.Item("Param1")
crParameter2 = crParameterFieldDefinitions.Item(“Param2")
''Get the current values from the parameter field. At this point
''there are zero values set.
crParameter1Values = crParameter1.CurrentValues
crParameter2Values = crParameter2.CurrentValues
''Set the current values for the parameter field
crDiscrete1Value = New ParameterDiscreteValue
crDiscrete1Value.Value = Request.Form(“param1value“)
crDiscrete2Value = New ParameterDiscreteValue
crDiscrete2Value.Value = Request.Form(“param2value“)
''Add the first current value for the parameter field
crParameter1Values.Add(crDiscrete1Value)
crParameter2Values.Add(crDiscrete2Value)
''All current parameter values must be applied for the parameter field.
crParameter1.ApplyCurrentValues(crParameter1Values)
crParameter2.ApplyCurrentValues(crParameter2Values)
|
|
|
|
|
Hello,
I have this structure :
Firstname; adresse; lastName
Aubenque; 20 wallstreet; Julien
Aubenque; 23 wallstreet; Julien
Bachelot; 22 londres street ; Maud
Aubenque; 25 wallstreet; Julien
Bachelot; 24 londres street ; Maud
Bachelot; 28 londres street ; Maud
Aubenque; 20 wallstreet; Julien
Bachelot; 22 londres street ; Maud
I would get the lines where the firstname and lastname was the same, help met to resolve this great problem, thank you verry mutch.
|
|
|
|
|
I didn't understand your problem. Can you explain it?
|
|
|
|
|
I am sorry, ok
I would get all line where the lastname and lastname was the same, i will get two elements for exemple in arraylist :
the first :
Aubenque; 20 wallstreet; Julien
Aubenque; 23 wallstreet; Julien
Aubenque; 25 wallstreet; Julien
Aubenque; 20 wallstreet; Julien
the second :
Bachelot; 22 londres street ;Maud
Bachelot; 24 londres street ; Maud
Bachelot; 28 londres street ; Maud
Bachelot; 22 londres street ; Maud
Thank you verry mutch
|
|
|
|