I am learning C sharp. I want to call a function (any simple function.
E.g. : Never ending while loop or large For loop inside the function).
I want to call this function from the main application window. I found
couple of tutorials but for some reason window freezes.
THis is what I did,
Thread FirstThread = new Thread(new ThreadStart(Thr eadFunc));
FirstThread.Sta rt();
FirstThread.Joi n();
protected static void ThreadFunc()
{
for (Int64 i = 0; i < 10000000; i++)
{
// Do my job . . . . .
}
}
Can some one help me to resolve this problem. I am still a bigginner.
Pubudu 7 1413
Pubs <pu******@gmail .comwrote:
I am learning C sharp. I want to call a function (any simple function.
E.g. : Never ending while loop or large For loop inside the function).
I want to call this function from the main application window. I found
couple of tutorials but for some reason window freezes.
THis is what I did,
Thread FirstThread = new Thread(new ThreadStart(Thr eadFunc));
FirstThread.Sta rt();
FirstThread.Joi n();
When you call Join, that basically says "wait until the thread has
finished" - which completely negates the point of doing threading.
Just don't call Join and it should be fine.
--
Jon Skeet - <sk***@pobox.co m>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
You are calling Join, which by definition waits for the other thread
to exit. In short, don't call Join here - just start the other thread
and let it run. By the way, if the worker thread is touching the form
or any controls (such as setting text etc), you will need to use
Control.Invoke to switch back to the UI thread, due to thread
affinity.
Marc
On May 31, 11:40*am, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:
Pubs <pubud...@gmail .comwrote:
I am learning C sharp. I want to call a function (any simple function.
E.g. : Never ending while loop or large For loop inside the function).
I want to call this function from the main application window. I found
couple of tutorials but for some reason window freezes.
THis is what I did,
Thread FirstThread = new Thread(new ThreadStart(Thr eadFunc));
* * * * * *FirstThread.St art();
* * * * * *FirstThread.Jo in();
When you call Join, that basically says "wait until the thread has
finished" - which completely negates the point of doing threading.
Just don't call Join and it should be fine.
--
Jon Skeet - <sk...@pobox.co m>
Web site:http://www.pobox.com/~skeet*
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com
Jon Skeet
Thank you very much for the help. It works now.
How do I know once the function is completed, thread is terminated ?
Pubudu
On May 31, 11:43*am, Marc Gravell <marc.grav...@g mail.comwrote:
You are calling Join, which by definition waits for the other thread
to exit. In short, don't call Join here - just start the other thread
and let it run. By the way, if the worker thread is touching the form
or any controls (such as setting text etc), you will need to use
Control.Invoke to switch back to the UI thread, due to thread
affinity.
Marc
Marc,
Thank you very much Marc. It is working fine.
Pubudu
On Sat, 31 May 2008 09:51:15 -0700, Pubs <pu******@gmail .comwrote:
Thank you very much for the help. It works now.
How do I know once the function is completed, thread is terminated ?
One approach would be to have your thread call a specific method when it's
done. If you want the method to do something with the UI, you should do
as Marc suggests with that method, calling it with Control.Invoke( )
instead of calling it directly.
Pete
To second Peter's answer: if the code in question is "close" to the
other code, then just call some method directly, usually via
Control.Invoke - i.e.
void WorkerMethod()
{ // runs on background thread
// ... some long code
this.Invoke((Me thodInvoker) LongOpFinished) ;
}
void LongOpFinished( )
{ // runs on UI thread when WorkerMethod is complete
}
If the code is more separated (such as library methods), then you
might use a "callback" - which at the simplest could be as simple as
passing a MethodInvoker (or Action, or ThreadStart, or whatever) into
the method as an argument, and then invoking it at the end. Callbacks
can also be implemented via events (and about 20 other ways) - or you
can just have the UI worry about it locally (i.e. the UI calls a local
method 9worker thread) that calls the library method (worker thread),
then calls another local method (UI thread).
Marc
On May 31, 4:26*pm, Marc Gravell <marc.grav...@g mail.comwrote:
To second Peter's answer: if the code in question is "close" to the
other code, then just call some method directly, usually via
Control.Invoke - i.e.
void WorkerMethod()
{ // runs on background thread
* // ... some long code
* this.Invoke((Me thodInvoker) LongOpFinished) ;}
void LongOpFinished( )
{ // runs on UI thread when WorkerMethod is complete
}
If the code is more separated (such as library methods), then you
might use a "callback" - which at the simplest could be as simple as
passing a MethodInvoker (or Action, or ThreadStart, or whatever) into
the method as an argument, and then invoking it at the end. Callbacks
can also be implemented via events (and about 20 other ways) - or you
can just have the UI worry about it locally (i.e. the UI calls a local
method 9worker thread) that calls the library method (worker thread),
then calls another local method (UI thread).
Marc
Marc,
Thank you very much. This helped me a lot.
Pubudu This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jon Skeet [C# MVP] |
last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the
article on all the groups this is posted to.
I've finally managed to finish my article on multi-threading - at least
for the moment. I'd be *very* grateful if people with any interest in
multi-threading would read it (even just bits of it - it's somewhat
long to go through the whole thing!) to check for accuracy,
effectiveness of examples, etc.
Feel free to mail...
|
by: bughunter |
last post by:
In version 7.x
connect to db
activate db db
SQL1494W Activate database is successful, however, there is already a
connection to the database.
works fine - db activated.
In 8.x - No! WHY??? :-(
|
by: John |
last post by:
I've got some reasonably complex business logic in my C# code, in a class
called by a ASP.NET page. This takes around 3-4 seconds to execute. It's not
dependent on SQL calls or anything like that.
I know of the problems with the worker process doing two things at once from
the main thread pool (The old "Sit in a tight loop for 20 seconds and watch
all your webapps die!" issue). So I've worked around the issue by spawning
the business...
|
by: Pawan Narula via DotNetMonster.com |
last post by:
hi all,
i'm using VB.NET and trying to code for contact management in a tree. all my contacts r saved in a text file and my C dll reads them one by one and sends to VB callback in a sync mode thread. so far so good. all contacts r added properly.
now when another login adds me in his contact, i recv a subscription, so i popup a form and ask for accept/reject. this all happens in a separate thread. popup form gets opened and choice is...
|
by: Christian Kaiser |
last post by:
We have a component that has no window. Well, no window in managed code - it
uses a DLL which itself uses a window, and this is our problem!
When the garbage collector runs and removes our component (created
dynamically by, say, a button click, and then not referenced any more), the
GC runs in a different thread, which prohibits the DLL to destroy its
window, resulting in a GPF when the WndProc of that window is called - the
code is gone...
| |
by: DBC User |
last post by:
I have a background process which reads a table to see if there are any
pending requests. If there are any, then it will start a worker thread
(only 10 allowed at a time) and executes a method. In this method, I
iniate a PROCESS and on completion, it reduces the available worker
thread and continue.
I have couple of questions;
1. Since I am launching multiple threads on a same method, do I have to
take care of locking so that each one...
|
by: Mike P |
last post by:
I am trying to write my first program using threading..basically I am
moving messages from an Outlook inbox and want to show the user where
the process is up to without having to wait until it has finished. I am
trying to follow this example :
http://www.codeproject.com/cs/miscctrl/progressdialog.asp
But although the messages still get moved, the progress window never
does anything. Here is my code in full, if anybody who knows...
|
by: Pubs |
last post by:
I am learning C sharp. I want to call a function (any simple function.
E.g. : Never ending while loop or large For loop inside the function).
I want to call this function from the main application window. I found
couple of tutorials but for some reason window freezes.
THis is what I did,
Thread FirstThread = new Thread(new ThreadStart(ThreadFunc));
FirstThread.Start();
FirstThread.Join();
|
by: Eng Teng |
last post by:
How do I write a code to activate a popup menu when I right click in a label
control?
Regards,
Tee
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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,...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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...
|
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
| |
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...
| |