|
hi
my Code IS:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace RaceCondition
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string s=" ";
Thread t1, t2;
private void button1_Click(object sender, EventArgs e)
{
t1 = new Thread(f1);
t2 = new Thread(f2);
t1.Start();
t2.Start();
while (t1.IsAlive||t2.IsAlive)
{
Thread.Sleep(50);
}
for (int i = 0; i < s.Length; i++)
{
if (s[i]=='n')
{
listBox1.Items.Add("");
}
else
{
listBox1.Items[listBox1.Items.Count-1]+=s[i].ToString();
}
}
}
Random r = new Random();
void f1()
{
Thread.Sleep(r.Next(2));
System.Threading.Monitor.Enter(s);
for (int i = 1; i <= 100; i+=2)
{
Thread.Sleep(r.Next(2));
s += 'n';
Thread.Sleep(r.Next(2));
s += "T1 ";
Thread.Sleep(r.Next(2));
s += i.ToString() + " ";
Thread.Sleep(r.Next(2));
s += i.ToString() + " ";
Thread.Sleep(r.Next(2));
s += i.ToString() + " ";
Thread.Sleep(r.Next(2));
System.Threading.Monitor.Exit(s);
}
Thread.Sleep(r.Next(2));
}
void f2()
{
Thread.Sleep(r.Next(2));
System.Threading.Monitor.Enter(s);
for (int i = 2; i <= 100; i += 2)
{
Thread.Sleep(r.Next(2));
s += 'n';
Thread.Sleep(r.Next(2));
s += "T2 ";
Thread.Sleep(r.Next(2));
s += i.ToString() + " ";
Thread.Sleep(r.Next(2));
s += i.ToString() + " ";
Thread.Sleep(r.Next(2));
s += i.ToString() + " ";
Thread.Sleep(r.Next(2));
System.Threading.Monitor.Exit(s);
}
Thread.Sleep(r.Next(2));
}
}
}
WHat is the problem of this code
I want to use Monitor TO Reslove Race Condition
|
|
|
|
|
Hi,
I haven't studied that in any detail, I do have a few comments though:
1.
you have a Monitor.Exit inside a for loop, whereas the Monitor.Enter is executed only once? That means most of the loop is not protected by the monitor.
2.
Thread.Sleep() takes an integer parameter representing the requested delay in milliseconds. However, its resolution is really system-dependent, and never that good. It typically will round up to a multiple of some 15 milliseconds. If you want details, I suggest you read Timer surprises, and how to avoid them[^].
3.
Rather than constructing a poll loop to wait for the end of one or more threads, I suggest you read up on Thread.Join()
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
I'm sure to some of you, this is pretty simple, and I think I know what it does, but I haven't been able to find the answer on the net.
What exactly do the '?' and ':' mean in this bit of code?
(dssubcat.Tables[0].Rows[0]["gallery_img"]) == System.DBNull.Value ? "" : dssubcat.Tables[0].Rows[0]["gallery_img"])
Does that line say that if that item in the DataSet is equal to the DBNull value, then return "" and if it doesn't return the item?
modified on Saturday, May 22, 2010 5:30 PM
|
|
|
|
|
This is known as the ternary operator, and you are right about what your assumption of the logic here is. Basically, it follows the pattern:
(some boolean test) ? (part that gets executed if true) : (part that gets executed if false). It's very easy to get carried away with this operator and produce some code that looks like this:
value < lowerBounds ? value > 0 ? Console.WriteLine("We have a problem") : Console.WriteLine("Value less than or equal to zero") : Console.WriteLine("The value is OK"); If you must use it, use it with care; at some point you may be responsible for trying to figure out what you were smoking when you wrote the code.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Honestly, I'm never that concerned with lessening the number of lines or getting rid of a few extra key strokes that I would probably ever use it. I just saw it in a question and wanted to make sure I understood for sure what it was.
Thanks!
|
|
|
|
|
You are welcome, and you are right that saving key strokes is no excuse.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
I use this construct a lot, but never nest them - that's where you get into maintenance trouble down the road.
I've seen code that had these nested as many as FIVE deep! MY EYES!
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
Hi,
You already have your answer so I won't expand on that but I thought this may be worth mentioning.
I don't often use them because of readability issues they create. They are very useful however where it is not possible to use an if/else block such as when constructor chaining. For example, these are the constructors for an ImapClient class that I'm working on - the 3rd constructor needs to assign a port depending on the value of another parameter. I could put an if/else block inside the constructor itself but then I lose the benifit of only having code in one place that all other constructors call into. Using ?: solves this.
public const int DefaultPort = 143;
public const int DefaultPortSsl = 993;
public static readonly ImapSecurity DefaultSecurity = ImapSecurity.Tls;
private string hostname;
private int port;
private ImapSecurity security;
public ImapClient(string hostname)
: this(hostname, DefaultPort, DefaultSecurity)
{ }
public ImapClient(string hostname, int port)
: this(hostname, port, DefaultSecurity)
{ }
public ImapClient(string hostname, ImapSecurity security)
: this(hostname, security == ImapSecurity.Ssl ? DefaultPortSsl : DefaultPort, security)
{ }
public ImapClient(string hostname, int port, ImapSecurity security)
{
this.hostname = hostname;
this.port = port;
this.security = security;
}
public enum ImapSecurity
{
None,
Tls,
Ssl,
}
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
I have same problem with dll(Delphi):
function _SignString(hProv: HCRYPTPROV; InputStr: PByte;
InputStrLen: DWORD; var ResultStr: PByte;
var ResultStrLen: DWORD): Boolean; stdcall;
[DllImport("sspCrypto.dll", EntryPoint = "_SignString")]
public static extern bool SignString(
IntPtr hProv,
IntPtr InputStr,
uint InputStrLen,
[Out]out IntPtr ResultStr,
[Out]out uint ResultStrLen);
function return true, but ResultStr has no correct importance
|
|
|
|
|
I've never used Delphi, so I'm not sure how to read that exactly. There are basically two possibilities:
1. most likely
if the Delphi code is supposed to allocate and fill native memory with a result string, then what you are getting is really a pointer into the native world, and you must use one of the Marshal.PtrToStringXYZ() methods to get the corresponding managed string.
2. alternatively
if the Delphi code needs a pointer to an existing buffer it is supposed to fill, then you should replace the last but one parameter in the prototype and in the call by a StringBuilder instance, which must have been created (with new) and given sufficient capacity (the parameter in its constructor).
[ADDED]
If the native function wants stdcall, your prototype should say so, using CallingConvention=CallingConvention.StdCall within the parentheses of the DllImport attribute.
[/ADDED]
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
modified on Saturday, May 22, 2010 6:04 PM
|
|
|
|
|
How can use Peterson Algoritm For many Process?
|
|
|
|
|
These[^] might help.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
How can Write this In C#?
I want Sample CODE
|
|
|
|
|
shahramkeyboard wrote: How can Write this In C#?
I want Sample CODE
Then go to rentacoder.
The forums are here to help you with specific problems you have and not to provide complete applications for people. Can you possibly imagine how many requests for code we get each week? If we were to try and solve them for all the requests, we wouldn't have time to do anything else. Imagine, also, how long it would be before companies realised that they could get things done for free here just by posting a request for source code. Now consider how few the number of people are here who actively participate and offer help in the forums - there are less than 100 good coders offering advice in the forums; and, almost without exception, they are all professional developers - they have lives to lead outside.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
how can Simulate Of TSL instruction For Resolve of Race Condition?
i Want to Use Thread without Race Condition By Simulate TSL
how i can?
|
|
|
|
|
By reading some of these[^] links.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
How can Write this In C#?
I want Sample CODE
|
|
|
|
|
shahramkeyboard wrote: I want Sample CODE
Honestly, you sound like a spoiled child here. I want, I want, I want. I want Salma Hayek to come and towel me down with her hair after a swim in a deserted lagoon. I want to win the national lottery. I want to be a prize winning author. None of these are going to happen, so I don't go on whinging about it.
I assume you have some experience as a coder. Read the texts. Formulate an algorithm from this and come up with a design. Then write the code. Don't think you are going to get somebody else to do it for you - that's just being lazy.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
As the subject suggests, is it possible to store a List<someobject> in a SQL database using stored procedures or any other ADO.NET code?
|
|
|
|
|
|
I second that: yes yes.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
Okay any tips on how to do this? Would I just pass it through as a single parameter and specify a data type? I only did a few google searches but it didn't help much. I'll keep searching though. Just a correction on my first post; I meant a generic List(T) (I know it's not supposed to be round brackets)
|
|
|
|
|
there probably is an infinite number of ways to do it, and maybe only a single good one.
you could serialize the List<T> to a stream, maybe an XML stream, and store that in a single field of your database. That stores the data, but makes it completely unavailable, until you read it all back into a list.
the one, structured, way to do it would take an entire table:
a List<T> contains objects of type T, which probably holds a number of data fields.
a database table holds a number of rows and columns.
hence each listed T instance should be stored as a row.
Etienne_123 wrote: I meant List and not just plain list
do you ever use the preview button, and fix what does not look right? you need to encode those < signs, otherwise they are interpreted like HTML here. There are some widgets to help you.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
Thanks I will use your comments. Yes I always use the preview button. That's why I said I know that 'T' shouldn't be in round brackets. I was in a hurry and wasn't sure how to encode those signs
|
|
|
|
|
Hi,
Does anyone know about a application like ".NET Reflector" that decompiles an entire DLL?
Thanks
|
|
|
|
|