VS 2003 C# - threads?

G

Guest

I have a win app. this a form with a button. after pressing the button a new
form is shown. when looading the form is being filled by data taken from a
database. the problem is that it takes a few seconds, so a user has to wait a
long time. i want to do something like that - when you have pressed the
button on the first form a new window should appear ("please wait when
loading"). in background the the main new form should be loading data from
the db. when it has finished, the form with the sentence "please wait..."
should close/hide.

i think i need threads. am i right? how to do that? i have tried something,
but it doesnt work. the new form doesnt respond to the user
 
G

Guest

I know this site. but can you tell me please how to implement threads in my
app?
should I create 1 or 2 threads in main program? what thread should do what
and when? how to handle synchronisation between them? I am a newbie in
threads so pls be patient :)
 
G

Guest

This is my code in C#. but it does NOT work in the way I want :(


System.Data.DataTable dt;
System.Threading.Thread info_t,fill_t;

private void button1_Click(object sender, System.EventArgs e)
{
info_t=new System.Threading.Thread(new
System.Threading.ThreadStart(waiting));
fill_t=new System.Threading.Thread(new
System.Threading.ThreadStart(filling));
info_t.Start();
fill_t.Start();
}

void filling()
{
System.Data.SqlClient.SqlDataAdapter da=new
System.Data.SqlClient.SqlDataAdapter();
//and so on...
this.dataGrid1.DataSource=dt;
info_t.Abort();
}

void waiting()
{
Form2 w=new Form2();//form that shows "please wait..."
w.Show();
}







//HELP!!
 
P

Peter Duniho

This is my code in C#. but it does NOT work in the way I want :(
[...]

Perhaps if you describe the results you are getting with that code, and
how that differs from what you want, that would lead to one or more useful
replies.

For what it's worth, you should only need one additional thread (to do the
processing), and you do not need to do anything except return from your
ThreadStart delegate method to stop the thread (ie don't call Abort()).

It seems to me that you don't really even need the "please wait" form.
You presumably already have a form for your UI...it seems like you could
just display the text "please wait" (or similar) in the area where the
results of the processing will be displayed.

Whether you do that or display a new form, the basic idea is this:

Main thread:
* Create the thread to do your processing
* Display whatever UI to indicate the user should wait

Processing thread:
* Do the processing
* Use Invoke or BeginInvoke to run a method on the main thread that
clears the message to wait (either closes the form you've displayed, or
simply replaces the "please wait" message with the results of the
processing)

The Invoke/BeginInvoke is required because you will be interacting with
the form window, and any code that uses the form window has to be run on
the same thread that created the form.

Pete
 
G

Guest

thx, it works! But when I have pressed the button again I am getting error:
thread is terminated or running. It cannot restart.
Please, can you check my code? I am a newbie in threads so I am not sure
about the style.

private System.Data.DataTable dt;
private System.Threading.Thread fill_t;
private Form2 w;
delegate void CloseForm();

public Form1() //main form with the button and the datagrid
{
InitializeComponent();
dt=new System.Data.DataTable();
fill_t=new System.Threading.Thread(new
System.Threading.ThreadStart(filling));
}

private void button1_Click(object sender, System.EventArgs e)
{
dt.Clear();
w=new Form2();
w.Show();
fill_t.Start();
}

void filling()
{
System.Data.SqlClient.SqlDataAdapter da=new
System.Data.SqlClient.SqlDataAdapter();
//filling the datatable dt...
da.Fill(dt);
Invoke(new CloseForm(CloseTheWaitForm));
}

void CloseTheWaitForm()
{
w.Close();
this.dataGrid1.DataSource=dt;
}



thank you in advance again
 
P

Peter Duniho

thx, it works! But when I have pressed the button again I am getting
error:
thread is terminated or running. It cannot restart.

When the thread exits the ThreadStart delegate, that's it. The thread is
done. You need to create a new thread. So, just move the code where you
create the thread into the button1_Click method and it should work fine.

And of course once you do that, there's not really any reason to save the
thread instance. Just instantiate it and let it go.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top