|
Please show your ability!
I'd like to use 5 threads running concurrently. They use the same method AccessData to add 2 random numbers in the ArrayList of 1000 elements.
Is there anything wrong in my coding?
How to update the ArrayList after each thread?
How to terminate if there is only 1 number left in the ArrayList?
Thanks so much
using System;<br />
using System.Collections;<br />
using System.Text;<br />
using System.Threading;<br />
<br />
namespace A1_SS<br />
{<br />
class Program<br />
{<br />
static object lockObject;<br />
static ArrayList space;<br />
<br />
static void Main()<br />
{<br />
<br />
space = new ArrayList();<br />
<br />
for (int i = 1; i <= 1000; i++)<br />
{<br />
space.Add(i);<br />
<br />
}<br />
<br />
lockObject = new object();<br />
<br />
<br />
Thread t1 = new Thread(new ParameterizedThreadStart(Program.AccessData));<br />
t1.Start(space);<br />
<br />
Thread t2 = new Thread(new ParameterizedThreadStart(Program.AccessData));<br />
t2.Start(space);<br />
<br />
Thread t3 = new Thread(new ParameterizedThreadStart(Program.AccessData));<br />
t3.Start(space);<br />
<br />
Thread t4 = new Thread(new ParameterizedThreadStart(Program.AccessData));<br />
t4.Start(space);<br />
<br />
Thread t5 = new Thread(new ParameterizedThreadStart(Program.AccessData));<br />
t5.Start(space);<br />
}<br />
<br />
Thread.Sleep(20 * 1000);<br />
}<br />
<br />
<br />
<br />
public static void AccessData(object param)<br />
{ <br />
<br />
ArrayList list = (ArrayList)param;<br />
<br />
lock (lockObject)<br />
{<br />
<br />
<br />
Random rand = new Random();<br />
<br />
int x = rand.Next(1, list.Count);<br />
list.Remove(x);<br />
Console.WriteLine("x = " + x);<br />
<br />
int y = rand.Next(1, list.Count);<br />
list.Remove(y);<br />
Console.WriteLine("y = " + y);<br />
<br />
int newNumber = x + y;<br />
Console.WriteLine("newNumber = " + newNumber);<br />
<br />
list.Add(newNumber);<br />
<br />
<br />
<br />
foreach (object obj in list)<br />
Console.WriteLine(obj);<br />
<br />
<br />
}<br />
<br />
<br />
<br />
}<br />
}<br />
}<br />
eric
|
|
|
|
|
Lots of problems in here.
1. Why are you using lock(lockObject), when lockObject is not being modified or even used by any thread at all!!! If you want to allow one thread at a time to modify your Array List, you should use lock(list).
2. In the code:
int x = rand.Next(1, list.Count);
list.Remove(x);
You are assuming that x is always present in the list. Whereas it is a possibility that x might have been removed by any other thread. Instead of removing the actual entry, you should use the indexes by using RemoveAt, and the number should not be x, it should be list[x].
3. The code written in the function will only execute five times, once per thread. So the list of 1000 will not get exhausted with this. You should use a loop to iterate through the list.
|
|
|
|
|
Thanks so much
But what I mean for x is that I store a random number in ArrayList to x variable, and take that number out of the ArrayList.
Please help
eric
|
|
|
|
|
Ok
I got it. I try now
Thanks Tehnoon
eric
|
|
|
|
|
Hello there!
I've been strugling to find a way of obtaining the value of the (Name) property as a string at runtime (you know, the "label1" piece of text that you get when droping a label on your form).
I need this to do some custom serialization for a component that doesn't implement any other identification property, and I can't seem to be able to do it.
Before I go and extend the component to provide such a property (thus forcing everyone who uses it to type one in), anybody tried to get the (name) property or knows how to do it?
Thanks in advance,
Calin, Toronto
|
|
|
|
|
If you drag and drop a button with the name button1, you can programmatically access its id or name by using the button1.Name property. It's not that difficult ;P
|
|
|
|
|
Hi, Tahnoon, and thank you for your reply.
As I said in my message, this involves a component (not a control). As we all know, the Name property is a property of the Control class, easily accessible for buttons, labels, etc.
However, when you need to access that Name property on a class that doesn't inherit the Control class, thus does not implement the Name property, what do you do?
Thank you again,
Calin, Toronto
|
|
|
|
|
Anyone plz tel me How to change column width in crystalreport at runtime
|
|
|
|
|
I think it's not possible to set width runtime but for displaying the data we can decrease the font size of that paticular column for data to display, which i am following.
Try Try Try....
|
|
|
|
|
I have made several derived buttons and have a backgroundimage on a customized form. My problem is when i use
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
in constructor and set the Color.Tranparent i get flickering when i moveover, -up and so forth.
I have tried doublebuffering and caching and it doesn't seem to eliminate that annoying flickering.
Can anyone help out ?
|
|
|
|
|
Try to capture space under the button to bitmap, on create, move or resize button events. And then draw this bitmap as button background.
|
|
|
|
|
Hi,
I am designing a page where I have two controls . One is TreeListView and one is listbox.
I like to populate the listview with the data from Database based on treelistview item selected.
I will click/check on one of the checkbox in TreeListView and that is used as input to fetch data from database.
Now my problem is when i click on a checkbox in treelistView the treeLIstViewItem checked property is not set till the TreeLIstView is displayed again. So i can not read the checked item(s) and cannot use it to fetch data from Db for the listbox.
I can see that next time i click on another item in treelistview the perviously selected/ checked
item Property is set but the item i clicked just now i cannot read it.
I have multiple child nodes for each treelistview item.
Is there any way i can read the value of the checked items before teh treelistview is displayed on clicent side.
Please help. I am to complete this task latest by tomorrow.
Any help will be appreciated.
Thanks!
|
|
|
|
|
i have moved the application to another machine, i moved the database successfully but the problem that when i open the report it displays the last query before moving i tried to use
rptcust.SetDatabaseLogon(UserName,PassWord,ServerName,DataBase);
but no changes happened and the new added records does not appear in the report.
|
|
|
|
|
I think u to have once again set the connection for ur database in the report design which would propably work.
TRY TRY TRY.....
|
|
|
|
|
vamsimohan21 wrote: set the connection for ur database in the report design
of course it will work. but it is not a solution
|
|
|
|
|
Hi,
I'm getting a strange error when trying to run my form with DirectX on another computer (on my own it's fine). I get:
Additional information: Request for the permission of type System.Security.Permissions.SecurityPermission, mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 failed.
This problem has popped up ever since I added a panel that uses DirectX. To see if it could be something I did, I ran a simple, beginner directx file (from Tom Miller's Managed DirectX 9 book) on my computer and then copied those files to the other computer, and it again didn't work, although this time giving me the error:
An unhandled exception of type 'System.Security.SecurityException' occurred in Lights.exe
Additional information: Security error.
I then added messageboxes to see how far the thing ran before having issues. It didn't go far. I start with the usual Main() entry point:
Application.Run(new Tabs());
Then
private Tabs()<br />
{<br />
MessageBox.Show("begin");<br />
InitializeComponent();<br />
panel1.Visible = false;<br />
tabControl1.Visible = true;<br />
...}
Then under
InitializeComponent()<br />
{<br />
MessageBox.Show("start-ic");<br />
...<br />
}
When running the program, the "begin" messagebox appears ("start-ic" doesn't), and a Just-In-Time Debugger pops up. I run a new instance of Visual Studio .NET, the line after InitializeComponent() gets the yellow highlighting, and the System.Security.SecurityException warning comes up (the long version from above).
I've found info from people having the same exception as my first one, but they're all getting those errors when trying to connect to a database or the web which I definitely don't do.
Any ideas why this could be happening? It works fine on another computer if I create a plain form.
Thanks for any help/ideas!!!
Mel
-- modified at 16:31 Wednesday 12th April, 2006
|
|
|
|
|
1. Make sure the Managed DirectX runtime is installed on the target machine.
2. Make sure you're not running the application from a network share.
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: I luv teh choco
The apostle Paul, modernly speaking: Epistles of Paul
Judah Himango
|
|
|
|
|
Hi!
You can set .NET security settings from a control panel: Microsoft .NET Framework 1.1/2.0 Wizards.
Could it be that you're running your program from a non-local disk? In that case, the framework won't allow the code to do everything locally running code is allowed to do. But you can adjust the permissions using the afforementioned control panel.
Regards,
mav
--
Black holes are the places where god divided by 0...
|
|
|
|
|
Thanks! That worked.
Mel
|
|
|
|
|
Hi, I'm building a chat program using Asyncronous Sockets, but want to secure or encrypt the transfer between client and server. At the moment I'm using TcpListenter and TcpClient sockets and all messages are routed through the server.
Can anyone advise on the best way to secure the transfer simply and efficiently (so it doesn't degrade speed too much)?
I'm also looking into authentication (login) between client/server and wondered if there is any information on how to implement this in c#. I've initially looked at using MD5, but is there any better ways for secure login...?
Any help appreciated!
|
|
|
|
|
Have you looked into the System.Security.Cryptography namespace? There are several encryption algorithms that do most of the work for you that can be useful in encrypting data. There are also some encrypted stream objects you could use for encrypting data between client and server.
|
|
|
|
|
Thanks very much, I'll look into those. Would you recommend any in specific? I'm hoping to not compromise in speed, as the messages are already routed through the server.
|
|
|
|
|
Any cryptograhpy is pure overhead. There will be a speed hit as you have to encrypt it on one end and decrypt it on the other end. But generally speaking, it shouldn't be too much of a performance hit. Best way to know is to try some out and see the results.
|
|
|
|
|
Lastly, in your view, is it relatively easy to implement an encrypted network stream?
Thanks for all the help Judah, don't really have time to test due to deadlines, but will give it a go.
|
|
|
|
|
I would venture a guess and say yes, since it's still a System.IO.Stream underneath, with the same familiar methods. But it really depends on your surrounding code; if lots of code is expecting it to act one way, you're going to have to make lots of changes. OTOH, if your code is isolated to the point where only the sending and receiving methods are expecting a regular old stream, you'll only need to change it in those 2 places.
|
|
|
|