|
Don't know much about the RichTextBox, but you can hide the control instead of suspending layout, and show it again instead of resuming layout. This even works on COM controls, so I think it shouldt do for all other controls, too.
|
|
|
|
|
hi
can we fill the list directly with help of data set if yes plz do let me know ji
Thx
Work Hard and Test your Luck
|
|
|
|
|
Which kind of "list" ?
you can get the Rows of a Table as an Array, and you can make an "for each row"...
but not sure what do you mean by "list", "list box" ?.
Greetings
Braulio
|
|
|
|
|
Is it possible to do drawing somewhere on our Fome with Managed DirectX , for example on a Panel or PictureBox control? If so, is there any quick tutorial anywhere?
Don't forget, that's Persian Gulf not Arabian gulf!
|
|
|
|
|
Yes there is.
See the examples supplied with the DirectX9 SDK
|
|
|
|
|
But there, all drawing is done on whole the Form , but not on smaller areas such as a Panel .
Don't forget, that's Persian Gulf not Arabian gulf!
|
|
|
|
|
at C# of environment use DateDiff() function
|
|
|
|
|
C# does not need a DateDiff() function.
You can add and substract DateTime objects, which results in a TimeSpan object:
TimeSpan ts = DateTime.Now - dtAnytime;
|
|
|
|
|
Hi, I am trying to get my head around safe thread synchronization.
I think the code below is not thread safe as the Start() method may be called several times which would cause the event to be fired asynchronously.
Any comments?
public class ThreadTest1
{
public void Start()
{
Thread thread = new Thread( new ThreadStart(ThreadMethod));
thread.Start();
}
private void ThreadMethod()
{
while (true)
{
Thread.Sleep(1000);
OnEvent(EventArgs.Empty);
}
}
public event EventHandler Event;
protected virtual void OnEvent(EventArgs e)
{
if (Event != null)
{
Event(this, e);
}
}
}
Would the code below be safe?
I have wrapped the event code in a locked block.
public class ThreadTest2
{
public void Start()
{
Thread thread = new Thread( new ThreadStart(ThreadMethod));
thread.Start();
}
private void ThreadMethod()
{
while (true)
{
Thread.Sleep(1000);
DoEvent();
}
}
private void DoEvent()
{
lock (this)
{
OnEvent(EventArgs.Empty);
}
}
public event EventHandler Event;
protected virtual void OnEvent(EventArgs e)
{
if (Event != null)
{
Event(this, e);
}
}
}
Any comments would be appreciated
|
|
|
|
|
I don't see anything shared between threads here. So there is no need to use Lock statement here, as I understand.
Don't forget, that's Persian Gulf not Arabian gulf!
|
|
|
|
|
I think that the problem is that the event code is called by many threads. I want the event code to be executed by only one thread at a time.
If I had replaced the code:
OnEvent(EventArgs.Empty);
in the first example with the following:
MyForm.Text = "A Caption";
then the rules for safe thread practices would clearly be broken.
|
|
|
|
|
Here, if you have only one CPU, it's impossible for two events be invoked simultaneously! It seems you have misunderstood the usage of lock statement? or am I wrong?!
Don't forget, that's Persian Gulf not Arabian gulf!
|
|
|
|
|
Well you cant write code assuming only one CPU. I have dual CPUs.
Perhaps I have not put my question clearly enougth.
I have a method that is called from many threads. I want to ensure that only one thread has access to the method at any one time.
Heres another example:
public class ThreadTest1
{
public void Start()
{
for (int i = 0; i < 100; i++)
{
Thread thread = new Thread( new ThreadStart(ThreadMethod));
thread.Start();
}
}
private void ThreadMethod()
{
for (int i = 0; i < 100; i++)
{
Testing();
}
}
private void Testing()
{
Console.WriteLine("Enter");
Thread.Sleep(10);
Console.WriteLine("Exit");
}
}
The console output ... or part of it at least, looks like this:
Enter
Exit
Enter
Enter
Exit
Exit
The output I want is this:
Enter
Exit
Enter
Exit
Enter
Exit
How would I modify the code above to do this?
|
|
|
|
|
Although the execution of multiple events cannot be performed simultaneously on a single CPU, they can certainly be interlaced since they are executing on separate threads. The usage of synchronization techniques still applies. Furthermore, making assumptions about the hardware and how it relates to thread synchronization is a dangerous road to go down.
Brian
|
|
|
|
|
Thanks for these comments.
From the comments made earlier I was starting to think that my understanding of the need for synchronization was completely wrong. Your comments have really cleared things up for me.
|
|
|
|
|
I would intentionally leave the ThreadTest class unsafe for multithreading. Inform the subscribers of this class' event that it is executed on another thread. Let the subscribers handle their own synchronization. Remember, the ThreadTest class is not safe for multithreading so that means subscribing and unsubscribing to the event is also not safe multithreading. Here's how the code might look...
public class ThreadTest
{
public event EventHandler MyEvent;
public void Start()
{
Thread thread = new Thread(new ThreadStart(this.ThreadMethod));
thread.Start();
}
private void ThreadMethod()
{
while (true)
{
Thread.Sleep(1000);
if (MyEvent != null) MyEvent(this, EventArgs.Empty);
}
}
}
public class Subscriber
{
public void Run()
{
ThreadTest test = new ThreadTest();
test.MyEvent += new EventHandler(this.OnEvent);
test.Start();
test.Start();
test.Start();
}
public void OnEvent(object source, EventArgs e)
{
lock (this)
{
}
}
}
|
|
|
|
|
Thanks for the suggestions Brian. This is just the sort of help I was after.
|
|
|
|
|
Hummmm, but because of one reason, you may need to use lock in your first example.
Consider below situation:
A thread(say thread_A ) reaches here:
if (<code>Event </code>!= null){
<code>Event</code>(<code>this</code>, <code>e</code>);
}
thread_A checks and sees that Event is not null , and goes to fire Event (this, e). But before calling it, its time slice terminates. Now suppose that in our main application, by using -= operator the Event gets null. Now it's thread_A 's turn to continue. But an exception will be fired by calling Event (this, e). This will not happen be using below code:
lock(this){
if (<code>Event </code>!= null){
<code>Event</code>(this, e);
}
}
I am wondered why below code doesn't lock the Event and the exception would occur:
lock(<code>Event</code>){
if (<code>Event </code>!= null){
<code>Event</code>(this, e);
}
}
Don't forget, that's Persian Gulf not Arabian gulf!
|
|
|
|
|
Your example is interesting but I think that your suggested solution is flawed. Placing the lock around the event raising code e.g.
lock(this) {
if (Event != null) {
Event(this, e);
}
}
will not stop our main application from using -= operator on the Event. The lock places the event raising code in a critical section. It does not stop other threads accessing the Event property.
What do you think?
|
|
|
|
|
I don't think;), I tried it and it worked seamlessly!
Don't forget, that's Persian Gulf not Arabian gulf!
|
|
|
|
|
OK ... How about posting your test code.
|
|
|
|
|
I just need your e-mail to send source code for you.
Don't forget, that's Persian Gulf not Arabian gulf!
|
|
|
|
|
You were lucky
|
|
|
|
|
Brian Gideon wrote:
You were lucky
I can post the source code for you and you will see you are more lucky than me after running it and having no exception! ;P (need for your mail)
Don't forget, that's Persian Gulf not Arabian gulf!
|
|
|
|
|
TimK wrote:
What do you think?
You are correct.
|
|
|
|