473,396 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

VS 2003 C# - threads?

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
Mar 29 '07 #1
7 1635
Yes, you would use Threading. Here's a good reference for you:

http://www.albahari.com/threading/

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
Networking Components, Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"Chris" <Ch***@discussions.microsoft.comwrote in message
news:B4**********************************@microsof t.com...
>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

Mar 29 '07 #2
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 :)
Mar 29 '07 #3
Can anyone help me?
Mar 30 '07 #4
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!!
Mar 30 '07 #5
On Fri, 30 Mar 2007 02:02:02 -0700, Chris
<Ch***@discussions.microsoft.comwrote:
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
Mar 30 '07 #6
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
Mar 30 '07 #7
On Fri, 30 Mar 2007 12:50:01 -0700, Chris
<Ch***@discussions.microsoft.comwrote:
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
Mar 30 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.