|
|
Hi all,
Well i am trying to get message over textbox which is in a loop and contains many messages. But then the problem was that my form was not responding if i switch to any other application n return back to it. So it use to hang. So some1 told me to use BackGroundWorker Class. And i did that and the below code is working absolutely fine...just hv a look below..
<br />
namespace BackgroundThread<br />
{<br />
public partial class Form1 : Form<br />
{<br />
public Form1()<br />
{<br />
InitializeComponent();<br />
}<br />
private BackgroundWorker worker = null;<br />
<br />
private void Form1_Load(object sender, EventArgs e)<br />
{<br />
worker = new BackgroundWorker();<br />
worker.DoWork += new DoWorkEventHandler(worker_DoWork);<br />
worker.RunWorkerAsync();<br />
<br />
}<br />
<br />
void worker_DoWork(object sender, DoWorkEventArgs e)<br />
{<br />
for (int i = 0; i < 100000; i++)<br />
{<br />
if (InvokeRequired)<br />
Invoke(new Change(OnChange), i);<br />
}<br />
<br />
private void OnChange(int i)<br />
{<br />
textBox1.Text += i.ToString() + ",";<br />
Application.DoEvents();<br />
Thread.Sleep(100);<br />
}<br />
<br />
private delegate void Change(int i);<br />
<br />
}<br />
}<br />
<br />
Now comes the problem part. Suppose now i have a button over form and after clicking that it should write messages over the text box after the aboce code finish executing. But i dont know how to include that in a thread...just have a look below...
<br />
<br />
private void startBtn_Click(Object sender, EventArgs e)<br />
{<br />
int Count = 0;<br />
while(true)<br />
{<br />
if(count > 1000)<br />
{<br />
break;<br />
}<br />
<br />
Thread.Sleep(1000);<br />
textBox1.Text += "Message" + Environment.NewLine;<br />
textBox1.Refresh();<br />
count++;<br />
<br />
}<br />
<br />
The above code again makes the form not responding after i press the start button. <br />
Well i know its a threading problem and i dont have much experience in threading. <br />
<br />
Well actually the above code is just an example. My real application is different and would give message over text box from different parts of application. So how to solve this case..if there is any easy way out like to refresh the form every time...etc...<br />
<br />
Any help would be greatly appreiated..<br />
Thanks<br />
<br />
<br />
|
|
|
|
|
Software_Specialist wrote: The above code again makes the form not responding after i press the start button.
Thats probably because the while loop at the beginning of the start button click event will be infinite (assuming Count and count are the same variable, you just typed differently)
But a couple of points in your code.. If you are using BackGroundWorker you should not do Application.DoEvents. That can cause problems. Also you are doing a Thread.Sleep in OnChange, which is being called from the GUI thread. So I guess this will hold up your GUI thread?
So basically, no DoEvents and no sleeping on the GUI thread should make it better.
|
|
|
|
|
Actually your app is probably not responding because you're calling Thread.Sleep(1000) for 1000 times from the main thread your application is running on. After a thousand seconds it'll be responsive again. You should run your code from a separate thread.
Again, you should use a background worker for your loop and update your controls using the report progress mechanism of the background worker or something similar. I showed you the code for this some days ago I think here
Standards are great! Everybody should have one!
|
|
|
|
|
Hi body...
I have a datagrid with a binding source. when I try to go out of that row there are a multitude of events that get raised (dataError, cellvalidating, rowvalidating, ...) does somebody know the exact sequence of those events?
for exemple, at the moment I know that when I change the selected row first I have a RowValidating, then a rowValidated and finally a SelectionChanged. Are all those sequences written somewhere?
Thank you very much
Life is not short... the problem is only how you organize yourself
|
|
|
|
|
JoZ CaVaLLo wrote: does somebody know the exact sequence of those events?
Well if you create handlers for each one then add Debug.WriteLine statements in them and launch a Debug instance and look at the results in the Output window, then you will know what the sequence is. I hope this is a school project.
|
|
|
|
|
Hi all,
I've implemented drag 'n drop for internal and external drag operations. However, I need to test for which mouse button is currently being pressed to enable different functionality.
The MSDN documentation for DragEventArgs suggest using the KeyState property and testing it's value to do this.
Now here comes the wierd part: I had this working a little while back. Nothing has changed in that section of code.
And the KeyState property has a value of 0 (which is impossible because that would mean no buttons at all are being pressed, so why is there a drag operation occurring?).
Anyone had any experience with this situation and/or know what the problem is and how to fix it?
|
|
|
|
|
Hi,
This is my first message to this forum. I beleive you guys are very helpful. Pls reply me on the following.
I have two forms one contain grid to list all the data and a add button. When I press the add button it opens a new form to insert record. Then I press save it saves to the database and close. But I need to reload the first form with the added record before closing the second. I have tried hard to find the answer. Pls help me on.
Thank you.
Nisha S.
|
|
|
|
|
Nisha S. wrote: This is my first message to this forum
Wellcome!
Nisha S. wrote: I need to reload the first form with the added record before closing the second
I would use the ShowDialog() method to show the second form modal.
This will allow you to reload the datas in the mainform, when the method finished (second Form closes)
If you have to use Show(), you would need to handle the Closeing or Closed event of the second form.
Hope it helps!
All the best,
Martin
|
|
|
|
|
Simple method. But I don't know how much efficient is this when considering about big applications. What you can do is pass first forms object to second form when second from loads. Make datagrid to public. Using first forms object you can access datagrid. Bind that as you do always
|
|
|
|
|
No - you don't do this. This violates so many OO principles that I don't know where to begin. Please don't recommend behaviour like this.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Nisha S. wrote: This is my first message to this forum.
Welcome to CP.
Nisha S. wrote: I beleive you guys are very helpful.
We are here to Help you.
Just try by using Crosspage Post back method.
SSK.
|
|
|
|
|
What? What on earth is the "Crosspage Post back method"? If you want to help, provide an explanation of what this is.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Satips please at least confine your rubbish to Lounge/Soap Box
|
|
|
|
|
use the Form2.ShowDialog() better than Form2.Show()
it should answer your question
Regard,
TCim
|
|
|
|
|
Please tell me that you don't use default names. Tell me that you are using Form2 as an example only.
It is never good practice to leave default names for something - what is the purpose of Form2? Can you tell without opening the form up and having a look?
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
i used form2 only for the example..i know that is bad naming if we use Form2 or Form1 for naming a form
|
|
|
|
|
That's OK then - now, lets make sure that the OP knows not to do this.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Pete O`Hanlon wrote: It is never good practice to leave default names for something - what is the purpose of Form2? Can you tell without opening the form up and having a look?
Pete O`Hanlon wrote: Tell me that you are using Form2 as an example only.
Which is actually a bad one, cause it would make the beginners think, that they would not need an instance of the form (which is per default named "Form2" as class name) to call ShowDialog.
And then will ask 10 min. later why they get a compile error.
All the best,
Martin
|
|
|
|
|
try use Form2.ShowDialog()
then you'll not have to set your first form to show after second form is closed
Regards,
TCim
|
|
|
|
|
|
i'm sorry .. Form2 just an example, not the name of the form
Regards,
TCim
|
|
|
|
|
OK, ok,
But why are you posting same answere twice?
All the best,
Martin
|
|
|
|
|
First i post a message..my post doesn't show in forum then i posted it again, then i get my new and old message show
Regard,
TCim
|
|
|
|
|
As Martin stated, you should use the ShowDialog method. Then, depending on the DialogResult from the form, you would reload the data (as it doesn't make sense to reload the data if you haven't actually done the save).
Suppose that you have a form called OptionsDialog which you are going to use to add some options into a program and that the details will be saved to the database when the user clicks OK. In this form, set the DialogResult of the OK button to OK.
In your parent form, you would call it with something like this:
private void AddOptions()
{
using (OptionsDialog dlg = new OptionsDialog())
{
if (dlg.ShowDialog(this) == DialogResult.OK)
{
ReloadOptions();
}
}
} I hope that this helps.
Deja View - the feeling that you've seen this post before.
|
|
|
|