473,786 Members | 2,407 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1648
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***@discussi ons.microsoft.c omwrote in message
news:B4******** *************** ***********@mic rosoft.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.Dat aTable dt;
System.Threadin g.Thread info_t,fill_t;

private void button1_Click(o bject sender, System.EventArg s e)
{
info_t=new System.Threadin g.Thread(new
System.Threadin g.ThreadStart(w aiting));
fill_t=new System.Threadin g.Thread(new
System.Threadin g.ThreadStart(f illing));
info_t.Start();
fill_t.Start();
}

void filling()
{
System.Data.Sql Client.SqlDataA dapter da=new
System.Data.Sql Client.SqlDataA dapter();
//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***@discussi ons.microsoft.c omwrote:
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.Dat aTable dt;
private System.Threadin g.Thread fill_t;
private Form2 w;
delegate void CloseForm();

public Form1() //main form with the button and the datagrid
{
InitializeCompo nent();
dt=new System.Data.Dat aTable();
fill_t=new System.Threadin g.Thread(new
System.Threadin g.ThreadStart(f illing));
}

private void button1_Click(o bject sender, System.EventArg s e)
{
dt.Clear();
w=new Form2();
w.Show();
fill_t.Start();
}

void filling()
{
System.Data.Sql Client.SqlDataA dapter da=new
System.Data.Sql Client.SqlDataA dapter();
//filling the datatable dt...
da.Fill(dt);
Invoke(new CloseForm(Close TheWaitForm));
}

void CloseTheWaitFor m()
{
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***@discussi ons.microsoft.c omwrote:
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
10360
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10108
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9960
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4064
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.