Hi,
When I run my code, and click a button, the msgs are dispalyed in the rich textbox.
When I tried to move the scroll bar on the richtextbox while the process was still running, the whole form became "not responding."
What should I do to use my scroll bar to see all the displays while the process is still running?
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.
One of a set of tools we're offering as a way of saying thank you for being a part of the community.
The GUI runs on the main thread of the application. Once you do an infinite loop or sleep (in your cause for 10 seconds), the main thread become locked and the GUI doesn't redraw itself.
This is why your form is not responding.
In my opinion Any action that takes 500 Milliseconds should be performed on another thread.
Your form should start a new thread that will do the long process job, while in the meanwhile the parts in the form that need to be disabled get disabled.
When the thread finished doing what it supposed to do then it re-enables what it needs in the form and do whatever action in needs to do in the form, all of that while considering thread safety and preventing cross-thread access to the GUI
(You will need to use delegates to do this.Invoke when this.InvokeRequired is true)
I see your point saragani. I guess you mean I need to use the method(in this case, Thread.Sleep(10000)) with a new thread between AddHi(i) and AddBye(i). I guess I also need to make AddBye(i) wait until Thread.Sleep(10000) is all done.
Could you show me some example or code? I checked msdn, but still confusing how to apply cross-thread solution in my case.
0
IzzyTwinklyAuthor Commented:
oh I have some codes now. never mind my previous comment. Thanks!
I have a qucik question from your code, anyoneis.
Could you explain 'this.Invoke((MethodInvoker) delegate { this.button1.Enabled = true; });' little bit? I guess that you are make the button enable, but I want to know more detailed steps with Invoke, MethodInvoker, etc...
It is just a shorthand for the same thing you did with GreetingDelegate - it reduces all of the delegate declarations and little event functions that you have to write.
I like this article: http://www.yoda.arachsys.com/csharp/csharp2/delegates.html#anonymous.methods
where he gives this example:
using System;
delegate void SomeAction();
class Test
{
static void Main(string[] args)
{
SomeAction instance = delegate { Console.WriteLine(args[0]); }; instance();
}
}
The reason the above works is becuase the compiler knows what kind of delegate it needs (SomeAction.). In my cvase, it did not, so I had to tell it with a cast. I hjave seen others use "Action" as the delegate type in this situation.
this.Invoke((MethodInvoker) delegate { this.button1.Enabled = true; });.
David
0
IzzyTwinklyAuthor Commented:
Thank you so much.
I might need to open another question about cross threaded operation. =)
0
Question has a verified solution.
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
http://msdn.microsoft.com/en-us/library/system.windows.forms.application.doevents.aspx