|
I want to get a (int) i=i+1 if I click a button every time ;
that is say , if I click onece i==1;
if I click twice ,i==2; and so on.
thank the one help me!
|
|
|
|
|
Hi Lockepeak,
What do you mean with ' click once ' and click ' twice.
Do you mean single click and double click, because in that case you can easily do :
this.button.Click += new System.EventHandler(this.method1);<br />
this.button.DoubleClick + new System.EventHandler(this.method2);
If you mean click once, and then add 1 to i , you could do :
private void method1(object sender, EventArgs e)<br />
{<br />
i=(i+1);<br />
}
assuming that you int i referenced in your application.
|
|
|
|
|
the answer:
If you mean click once, and then add 1 to i , you could do :
private void method1(object sender, EventArgs e)
{
i=(i+1);
}
assuming that you int i referenced in your application.
the method can't get the right anwser. whatever I click several times the value allways begin value
|
|
|
|
|
well, I'm not sure what you're doing wrong but I just made a test project and it works perfect. Every single time I click the button , 1 is added to int i . What I think the problem is , is that you don't have the integer referenced properly in your application. You have to reference it throughout the entire form so that it remembers the previous value:
namespace Blah<br />
{<br />
public partial class Form1 : Form<br />
{<br />
int i = 0;<br />
public Form1()<br />
{<br />
InitializeComponent();<br />
}<br />
<br />
private void button3_Click(object sender, EventArgs e)<br />
{<br />
i = (i + 1);<br />
MessageBox.Show(i.ToString());<br />
lbl.Text = i.ToString();
}<br />
<br />
}<br />
}<br />
}
if you reference int i anywhere else in the project, it will reset the value as soon as you dispose the messagebox. Intead of the messagebox, you could use a label to see the number increase everytime you click the button.
Hope this helped.
|
|
|
|
|
When the button onclick event fires have this in your code i++;
|
|
|
|
|
Why does the simple exercise below throw a StackOverflowException? I didn't think I was looping or doing anything recursive.
namespace ArrayTests
{
class HasArray
{
public int[] ExampleArray
{
set
{
ExampleArray = value;
}
}
public HasArray()
{
int [] ExampleArray = new int[2] { 1, 2 };
}
}
class Program
{
static void Main(string[] args)
{
HasArray concreteHasArray = new HasArray();
int[] otherArray = new int[2] { 3, 4 };
concreteHasArray.ExampleArray = otherArray;
}
}
}
|
|
|
|
|
But, you were doing something recursive. In ExampleArray property, through set method, you were setting value to the same property. To see this by yourself, set breakpoint on beginning of Main() method and press F11 key several times. You can correct your HasArray class by adding one attribute of integer array type to it which holds the value for ExampleArray property.
This is what I mean:
class HasArray
{
private int[] _exampleArray;
public int[] ExampleArray
{
get { return _exampleArray; }
set { _exampleArray = value; }
}
public HasArray()
{
_exampleArray = new int[2] { 1, 2 };
}
}
|
|
|
|
|
Hi Cristian,
The problem is here:
public int[] ExampleArray
{
set
{
ExampleArray = value;
}
}
In the Setter curly Brackets you reset the property (Same Name), so when you call concreteHasArray.ExampleArray = otherArray;
you conccurently setting the ExampleAray.
Solution:
<br />
namespace ArrayTests<br />
{<br />
class HasArray<br />
{<br />
private int[] _ExampleArray ;
public int[] ExampleArray<br />
{<br />
set<br />
{<br />
_ExampleArray = value;
}<br />
get{return _ExampleArray;}
}<br />
public HasArray()<br />
{<br />
ExampleArray = new int[2] { 1, 2 };<br />
}<br />
}<br />
<br />
class Program<br />
{<br />
static void Main(string[] args)<br />
{<br />
HasArray concreteHasArray = new HasArray();<br />
int[] otherArray = new int[2] { 3, 4 };<br />
concreteHasArray.ExampleArray = otherArray;<br />
}<br />
}<br />
}<br />
Hope this helps
Nassos
GanDad
|
|
|
|
|
public int[] ExampleArray
{
set
{
ExampleArray = value;
}
}
Basically, what you're saying here is:
"In order to assign a value to ExampleArray, you first assign the value to ExampleArray by assigning a value to ExampleArray by assigning a ......"
The "standard" property format is as follows:
private int _myVar;
public int MyProperty
{
get
{
return _myVar;
}
set
{
_myVar = value;
}
}
You see, the variable is what holds the actual data... The property is just a way to access it.
|
|
|
|
|
What method do you think is better when there are many threads?
Regards,
Analizzatore Cloro
|
|
|
|
|
Ooooooh....
I thought I was just saving myself the time to set up private and public parts. Now I see it is recursive, of course! Thanks!
|
|
|
|
|
I'd like to know if there is any mechanism to know where the threads that ThreadPool object manages, are finished.
The idea is having a loop that calls the ThreadPool to execute some method, and then block that main thread until all threads are finished.
Is that possible?
Regards,
Diego F.
|
|
|
|
|
Diego F. wrote: The idea is having a loop that calls the ThreadPool to execute some method, and then block that main thread until all threads are finished.
Then why would you even use threading at all??
The whole point behind threading is to move work to a seperate thread so your primary thread can continue to respond to requests.
The thread pool doesn't raise any events when a thread is complete. Your threaded code has to supply that, or some other method to signal completion.
|
|
|
|
|
Well, in my case, I don't mind to block the main thread. The help of ThreadPool is that I need to run a method many times (let's say hundreds), and if I can run it separately, I would think that it saves time.
Am I wrong? Other way, you are rigth. It has no sense the use of threading.
Regards,
Diego F.
|
|
|
|
|
Hi,
AFAIK there is no built-in mechanism for this.
I see at least three possibilities:
1.
You could try to count yourself; use Interlocked.Increment each time you send
a piece of work to a thread, and have it call Interlocked.Decrement when it is done.
Then have your main thread do a busy wait (with Sleep!) on the same counter.
disadvantages:
- if a thread exits abnormally (or forgets to decrement) your main
thread will hang forever.
- the wait loop is not nice.
2.
Since .NET 2.0 there is BackgroundWorker class, which supports cancellation and
offers events. You could use its IsBusy property and/or its ProgressChanged event.
3.
And of course you could use regular threads, keep them in a list, and periodically
check their status. (Dont forget to clear the list when you conclude all has been
done; so the threads get garbage collected).
|
|
|
|
|
Thank you for your ideas.
What method do you think is better when there are many threads?
Regards,
Diego F.
|
|
|
|
|
I am not sure:
- I have not used BackgroundWorker yet;
- I have limited experience with ThreadPool; IMHO it lacks some functionality.
- most often I use regular threads, so I can easily manage them (e.g. change their
priority).
Diego F. wrote: when there are many threads?
what is many ?
It is my view that executing the same work in more than a couple of threads does
not improve performance: all these threads start to compete with one another
(they all want CPU cycles, or all want disk access, or...). So having 2 or 3 or 4
seems a maximum.
The one good reason I can come up with for really many similar threads is for
encapsulating independent jobs (as would be the case in a server with many clients).
BTW: when using real Threads, you can also do the following:
- for each new thread you create, add it to a list
remark: you probably want to set IsBackground true
- create a loop over that list, performing a Thread.Join();
when that loop finishes, all threads are done (does not matter in what order!).
advantage: no need to count
remark: clear the list when done
- you might even create an extra thread to run that loop, so it does the waitforall
logic, while your main thread can go on doing whatever it likes to do...
Hope this helps.
|
|
|
|
|
Would this post[^] also be by your hand or is this person just copying and pasting your words?
|
|
|
|
|
Hi, I am trying to write a program in C# that will ask for a password before running any program. Suppose if any one wants to run MS Word he has to enter the password. Now my question is how can I set the password check before executing a program?
Please let me know if any of know how.
Johnny
|
|
|
|
|
That's not possible.
Well, it is, but only if you have kernel development experience and know how to use a technique called "DLL Injection" to hook into the kernel and intercept the calls to CreateProcess. You also can NOT do this in and managed code language, like C# or VB.NET. You'd have to write this hook in C++ only.
This hook would have to have access to a database that contains the passwords for various processes. It would also have to have information on the dozens of other processes to NOT ask for a password for because these processes get launched spontaneously in the normal course of operation of Windows.
You would also need a manager application for this database so you can add/remove/change applications and their passwords.
The fact that you even asked this question shows that you don't have the requisite knowledge of Windows internals to pull this off.
|
|
|
|
|
Dave Kreskowiak wrote:
Well, it is, but only if you have kernel development experience and know how to use a technique called "DLL Injection" to hook into the kernel and intercept the calls to CreateProcess. You also can NOT do this in and managed code language, like C# or VB.NET. You'd have to write this hook in C++ only.
Is this going to remain a viable option going into Win64? I know that MS has sharply reduced the ability to hook into the kernel, but don't have the kernel specific knowledge needed to know if this is an affected area or not.
--
You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer
|
|
|
|
|
dan neely wrote: Is this going to remain a viable option going into Win64? I know that MS has sharply reduced the ability to hook into the kernel, but don't have the kernel specific knowledge needed to know if this is an affected area or not.
Hell, I have no idea! I'm really no expert on the kernel in's-'n-out's. I've just seen what it takes to hook API functions, I haven't actually done it myself.
|
|
|
|
|
That is quiet hard to achieve. This basically means that you have to override the Windows execute functions. When you click on a program binary ( exe ) Windows initiates the execution of the program and executes the code in that binary. There is no function that can neither interrupt the execution nor interrupt the executed code in the binary.
A possible way around it , is to disable all binaries, or set the permission of the binaries ( eg. winword.exe ) so that only a particular user is allowed to execute it. Then write yourself a little wrapper that is allowed to execute the binaries after you prompted for a password.
Cheers,
|
|
|
|
|
the site appears down at the moment, but the secure process utility from Stardock's Thinkdesk Suite[^] might be able to do what you want.
--
You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer
|
|
|
|
|
Dear all,
I'm trying to insert a double value in a database. To my understanding a double value can exist of eg. 23.45
When I insert a double value ( 23.45 ) into my postgres database, the seperator dissapears and the value in the table becomes 2345. The table in the database is of type Double Precision.
I have to use Double, instead of decimal since a library is demanding a double value as input.
Can anyone point me in the right direction ? How can I store the value so that the dot is there as well. ( 23.45 )
Kind regards,
|
|
|
|