473,783 Members | 2,376 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

.NET Threading Question

What is wrong with this code. All i am trying to test is 3 progressbar and
one button. On buttonclick i create 3 threads and each thread calls a method
which in turn updates the progressbar and it works. I would to know if this
can be used.
Thanks
private void button1_Click(o bject sender, System.EventArg s e)

{

ThreadStart job = new ThreadStart(one method);

Thread thread = new Thread(job);

thread.Start();

ThreadStart job1 = new ThreadStart(sec ondmethod);

Thread thread1 = new Thread(job1);

thread1.Start() ;

ThreadStart job2 = new ThreadStart(thi rdmethod);

Thread thread2 = new Thread(job2);

thread2.Start() ;

}

public void onemethod()

{

for (int i=0;i<100;i++)

{

progressBar1.Va lue = i;

Thread.Sleep(10 0);

}

}

public void secondmethod()

{

for (int i=0;i<100;i++)

{

progressBar2.Va lue = i;

Thread.Sleep(10 0);

}

}

public void thirdmethod()

{

for (int i=0;i<100;i++)

{

progressBar3.Va lue = i;

Thread.Sleep(10 0);

}

}
Nov 17 '05 #1
17 1502
Hi Arun,

The code should work fine, although it violates best practices by
interacting directly with the UI from threads other than the main
thread. Other than that though, it *looks* like it should run just fine.

~d

Arun Kumar wrote:
What is wrong with this code. All i am trying to test is 3 progressbar and
one button. On buttonclick i create 3 threads and each thread calls a method
which in turn updates the progressbar and it works. I would to know if this
can be used.
Thanks
private void button1_Click(o bject sender, System.EventArg s e)

{

ThreadStart job = new ThreadStart(one method);

Thread thread = new Thread(job);

thread.Start();

ThreadStart job1 = new ThreadStart(sec ondmethod);

Thread thread1 = new Thread(job1);

thread1.Start() ;

ThreadStart job2 = new ThreadStart(thi rdmethod);

Thread thread2 = new Thread(job2);

thread2.Start() ;

}

public void onemethod()

{

for (int i=0;i<100;i++)

{

progressBar1.Va lue = i;

Thread.Sleep(10 0);

}

}

public void secondmethod()

{

for (int i=0;i<100;i++)

{

progressBar2.Va lue = i;

Thread.Sleep(10 0);

}

}

public void thirdmethod()

{

for (int i=0;i<100;i++)

{

progressBar3.Va lue = i;

Thread.Sleep(10 0);

}

}

Nov 17 '05 #2
DotNet Coder,

I agree, its not the best practices but my point is, if you use threads and
use it carefully it shouldn't be a problem.
Check this link
http://www.yoda.arachsys.com/csharp/...winforms.shtml

let me know you comments.

Arun
"DotNet Coder" <d0*********@ya hoo.dot.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
Hi Arun,

The code should work fine, although it violates best practices by
interacting directly with the UI from threads other than the main thread.
Other than that though, it *looks* like it should run just fine.

~d

Arun Kumar wrote:
What is wrong with this code. All i am trying to test is 3 progressbar
and one button. On buttonclick i create 3 threads and each thread calls a
method which in turn updates the progressbar and it works. I would to
know if this can be used.
Thanks
private void button1_Click(o bject sender, System.EventArg s e)

{

ThreadStart job = new ThreadStart(one method);

Thread thread = new Thread(job);

thread.Start();

ThreadStart job1 = new ThreadStart(sec ondmethod);

Thread thread1 = new Thread(job1);

thread1.Start() ;

ThreadStart job2 = new ThreadStart(thi rdmethod);

Thread thread2 = new Thread(job2);

thread2.Start() ;

}

public void onemethod()

{

for (int i=0;i<100;i++)

{

progressBar1.Va lue = i;

Thread.Sleep(10 0);

}

}

public void secondmethod()

{

for (int i=0;i<100;i++)

{

progressBar2.Va lue = i;

Thread.Sleep(10 0);

}

}

public void thirdmethod()

{

for (int i=0;i<100;i++)

{

progressBar3.Va lue = i;

Thread.Sleep(10 0);

}

}


Nov 17 '05 #3
Completely wrong... if you use threads and use it carefully, it will
still be a problem. As that article states, "Never invoke any method or
property on a control created on another thread other than Invoke,
BeginInvoke, EndInvoke or CreateGraphics, and InvokeRequired. ".

This is not just a golden rule, it is a fact of winforms programming.
When you start messing with objects created in other threads, you start
running into all sorts of problems, like race conditions, deadlocks, etc.

Also, as the author stated, you may get away with it for awhile, but
eventually, your application will start displaying some pretty strange
behaviors and debugging could become a horrible process, especially if
it is threading errors that are killing the app.

HTH,
~d

Arun Kumar wrote:
DotNet Coder,

I agree, its not the best practices but my point is, if you use threads and
use it carefully it shouldn't be a problem.
Check this link
http://www.yoda.arachsys.com/csharp/...winforms.shtml

let me know you comments.

Arun
"DotNet Coder" <d0*********@ya hoo.dot.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
Hi Arun,

The code should work fine, although it violates best practices by
interacting directly with the UI from threads other than the main thread.
Other than that though, it *looks* like it should run just fine.

~d

Arun Kumar wrote:
What is wrong with this code. All i am trying to test is 3 progressbar
and one button. On buttonclick i create 3 threads and each thread calls a
method which in turn updates the progressbar and it works. I would to
know if this can be used.
Thanks
private void button1_Click(o bject sender, System.EventArg s e)

{

ThreadStar t job = new ThreadStart(one method);

Thread thread = new Thread(job);

thread.Start ();

ThreadStar t job1 = new ThreadStart(sec ondmethod);

Thread thread1 = new Thread(job1);

thread1.Star t();

ThreadStar t job2 = new ThreadStart(thi rdmethod);

Thread thread2 = new Thread(job2);

thread2.Star t();

}

public void onemethod()

{

for (int i=0;i<100;i++)

{

progressBar1 .Value = i;

Thread.Sleep (100);

}

}

public void secondmethod()

{

for (int i=0;i<100;i++)

{

progressBar2 .Value = i;

Thread.Sleep (100);

}

}

public void thirdmethod()

{

for (int i=0;i<100;i++)

{

progressBar3 .Value = i;

Thread.Sleep (100);

}

}


Nov 17 '05 #4

"Arun Kumar" <ar****@hotmail .com> wrote in message
news:OG******** ******@TK2MSFTN GP12.phx.gbl...
DotNet Coder,

I agree, its not the best practices but my point is, if you use threads
and use it carefully it shouldn't be a problem.
Check this link
http://www.yoda.arachsys.com/csharp/...winforms.shtml

let me know you comments.

Arun


There is nothing "carefully" with this, it may work in this simple sample
and it may work a million times but sooner or later it will break, it's
simply wrong to touch the UI element (a window handle) from a thread other
than the thread that owns the handle.

Willy.
Nov 17 '05 #5
Arun Kumar <ar****@hotmail .com> wrote:
I agree, its not the best practices but my point is, if you use threads and
use it carefully it shouldn't be a problem.
Not true.
Check this link
http://www.yoda.arachsys.com/csharp/...winforms.shtml


Which part of:

<quote>
Never invoke any method or property on a control created on another
thread other than Invoke, BeginInvoke, EndInvoke or CreateGraphics, and
InvokeRequired.
</quote>

gives you the impression that it shouldn't be a problem?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6
Alright,
Let me go back a step and tell you what i did in Win32 and I know it may not
be true in "WinForms"( As Microsoft keeps changing things)
I am a Delphi Developer, in Delphi, I got a Mainform with a statusbar and a
Thread which creates database.
I use Synchronize() to update the main form which i believe is correct.

I want to do the same in .Net and after playing around with things and
reading the article. (Correct me if i m wrong here)
1. Creating the Thread from Mainform
2. If the Thread calls a method which updates the mainform, is this wrong?

I dont want to write complex code just update the mainform.
Very simple
Mainform -> Creates Thread -> does some background database stuff, while
doing that it should update the mainform telling the user hey I am still
working on it wait till i complete it and also give them an indication where
its at.
This is what i am looking for and started playing with Threads. If its were
to be Delphi its a "piece of Cake" and I can do it with few lines.
But, I thought .Net is going to make coding lot easier I guess not.
<Article>" class itself, setting it in one thread, retrieving and processing
it in the other (updating the display in the UI thread, for example).
"<Article> Create a Thread to do stuff and create another Thread to update
UI what is going on here???

Anyway there is no point in hitting the wall

Arun


"DotNet Coder" <d0*********@ya hoo.dot.com> wrote in message
news:ey******** ******@TK2MSFTN GP14.phx.gbl...
Completely wrong... if you use threads and use it carefully, it will still
be a problem. As that article states, "Never invoke any method or property
on a control created on another thread other than Invoke, BeginInvoke,
EndInvoke or CreateGraphics, and InvokeRequired. ".

This is not just a golden rule, it is a fact of winforms programming. When
you start messing with objects created in other threads, you start running
into all sorts of problems, like race conditions, deadlocks, etc.

Also, as the author stated, you may get away with it for awhile, but
eventually, your application will start displaying some pretty strange
behaviors and debugging could become a horrible process, especially if it
is threading errors that are killing the app.

HTH,
~d

Arun Kumar wrote:
DotNet Coder,

I agree, its not the best practices but my point is, if you use threads
and use it carefully it shouldn't be a problem.
Check this link
http://www.yoda.arachsys.com/csharp/...winforms.shtml

let me know you comments.

Arun
"DotNet Coder" <d0*********@ya hoo.dot.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
Hi Arun,

The code should work fine, although it violates best practices by
interactin g directly with the UI from threads other than the main thread.
Other than that though, it *looks* like it should run just fine.

~d

Arun Kumar wrote:

What is wrong with this code. All i am trying to test is 3 progressbar
and one button. On buttonclick i create 3 threads and each thread calls
a method which in turn updates the progressbar and it works. I would to
know if this can be used.
Thanks
private void button1_Click(o bject sender, System.EventArg s e)

{

ThreadSta rt job = new ThreadStart(one method);

Thread thread = new Thread(job);

thread.Star t();

ThreadSta rt job1 = new ThreadStart(sec ondmethod);

Thread thread1 = new Thread(job1);

thread1.Sta rt();

ThreadSta rt job2 = new ThreadStart(thi rdmethod);

Thread thread2 = new Thread(job2);

thread2.Sta rt();

}

public void onemethod()

{

for (int i=0;i<100;i++)

{

progressBar 1.Value = i;

Thread.Slee p(100);

}

}

public void secondmethod()

{

for (int i=0;i<100;i++)

{

progressBar 2.Value = i;

Thread.Slee p(100);

}

}

public void thirdmethod()

{

for (int i=0;i<100;i++)

{

progressBar 3.Value = i;

Thread.Slee p(100);

}

}


Nov 17 '05 #7
Hey Jon? Didn't you write an article on this very subject? I seem to
remember you mentioning an article sometime last year.

~d

Jon Skeet [C# MVP] wrote:
Arun Kumar <ar****@hotmail .com> wrote:
I agree, its not the best practices but my point is, if you use threads and
use it carefully it shouldn't be a problem.

Not true.

Check this link
http://www.yoda.arachsys.com/csharp/...winforms.shtml

Which part of:

<quote>
Never invoke any method or property on a control created on another
thread other than Invoke, BeginInvoke, EndInvoke or CreateGraphics, and
InvokeRequired.
</quote>

gives you the impression that it shouldn't be a problem?

Nov 17 '05 #8

"Willy Denoyette [MVP]" <wi************ *@telenet.be> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..

"Arun Kumar" <ar****@hotmail .com> wrote in message
news:OG******** ******@TK2MSFTN GP12.phx.gbl...
DotNet Coder,

I agree, its not the best practices but my point is, if you use threads
and use it carefully it shouldn't be a problem.
Check this link
http://www.yoda.arachsys.com/csharp/...winforms.shtml

let me know you comments.

Arun

There is nothing "carefully" with this, it may work in this simple sample
and it may work a million times but sooner or later it will break, it's
simply wrong to touch the UI element (a window handle) from a thread other
than the thread that owns the handle.

Willy.

Thanks for your response.

Yes. I know that. there is nothing called "carefully" . I have worked on
Threads alot in Win32 and I know what will have happen." simply wrong to touch the UI element (a window handle) from a thread
other than the thread that owns the handle."

True. its a sample, yes I am trying out things what i can and what i cannot
do in .Net and what are the limitations and I strong believe there is
nothing wrong in doing that and putting my words out here and get the best
out it.

Arun

Nov 17 '05 #9
Arun Kumar <ar****@hotmail .com> wrote:
Let me go back a step and tell you what i did in Win32 and I know it may not
be true in "WinForms"( As Microsoft keeps changing things)
I am a Delphi Developer, in Delphi, I got a Mainform with a statusbar and a
Thread which creates database.
I use Synchronize() to update the main form which i believe is correct.
I'm afraid I don't know what Synchronize() does in Delphi. The "don't
change the UI from a non-UI thread" is a general Win32 rule though.
I want to do the same in .Net and after playing around with things and
reading the article. (Correct me if i m wrong here)
1. Creating the Thread from Mainform
2. If the Thread calls a method which updates the mainform, is this wrong?
Yes.
I dont want to write complex code just update the mainform.
Very simple
Mainform -> Creates Thread -> does some background database stuff, while
doing that it should update the mainform telling the user hey I am still
working on it wait till i complete it and also give them an indication where
its at.
Sure - so use Invoke or BeginInvoke, and you should be fine. There's a
pretty full example in the article.
This is what i am looking for and started playing with Threads. If its were
to be Delphi its a "piece of Cake" and I can do it with few lines.
But, I thought .Net is going to make coding lot easier I guess not.
<Article>" class itself, setting it in one thread, retrieving and processing
it in the other (updating the display in the UI thread, for example).
"<Article> Create a Thread to do stuff and create another Thread to update
UI what is going on here???
You don't create another thread to update the UI - you tell the
existing UI thread to update the UI.
Anyway there is no point in hitting the wall


There's every point in learning how to do it properly though. I suggest
you read the article again, preferably starting at the first page of it
(rather than just the windows forms page) and taking it a bit at a
time. Threading is fundamentally pretty tricky, but it's very useful
too.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #10

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

Similar topics

65
6761
by: Anthony_Barker | last post by:
I have been reading a book about the evolution of the Basic programming language. The author states that Basic - particularly Microsoft's version is full of compromises which crept in along the language's 30+ year evolution. What to you think python largest compromises are? The three that come to my mind are significant whitespace, dynamic typing, and that it is interpreted - not compiled. These three put python under fire and cause...
19
6487
by: Jane Austine | last post by:
As far as I know python's threading module models after Java's. However, I can't find something equivalent to Java's interrupt and isInterrupted methods, along with InterruptedException. "somethread.interrupt()" will wake somethread up when it's in sleeping/waiting state. Is there any way of doing this with python's thread? I suppose thread interrupt is a very primitive functionality for stopping a blocked thread.
3
1494
by: David Harrison | last post by:
I am working on an application on Mac OS X that calls out to python via PyImport_ImportModule(). I find that if the imported module creates and starts a python thread, the thread seems to be killed when the import of the module is complete. Is this expected? Does python have to be in control to allow threads to run? Would it be better to arrange things such that the file is processed using PyRun_SimpleFile? David S. Harrison
4
1592
by: Antal Rutz | last post by:
Hi, All! I'm new to threading. I have some design questions: Task: I collect data and store them in an RDBMS (mysql or pgsql) The question is how to do that with threading? The data-collecting piece of the code runs in a thread. 1. Open the db, and each thread writes the result immediately. (Sub-question: which is better: cursor object passed to the thread
6
555
by: CK | last post by:
I have the following code in a windows service, when I start the windows service process1 and process2 work fine , but final process (3) doesnt get called. i stop and restart the windows service and the final process(3) gets called. what am I doing wrong with the threading? by the way Directory.GetFiles(IncomingXMLPath1).Length is some global outcome from process 1. Thanks 1)
7
318
by: Anthony Nystrom | last post by:
What is the correct way to stop a thread? abort? sleep? Will it start up again... Just curious... If the thread is enabling a form, if the form is disposed is the thread as well? Thanks, Anthony Nystrom
4
1308
by: Bob | last post by:
- For cleanup, is it sufficient to set a Thread to Nothing after it's done? - It is OK to pass objects out of the thread? (dumb question maybe but I want to be sure) - What's the best way to process messages coming out of a thread? I want to queue them up, but MessageQueue doesn't look like what I need. Should I just make my own queue class? If so I'll have to worry about enumerator synchronization... a pointer to a 'best practice'...
4
321
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...
4
1605
by: Steven | last post by:
I am taking an "advanced" VB.Net course via web at a state university toward an information science degree. This is my second VB class and I am kind of disappointed in it. This week we covered threading. The instructor is of the opinion that threading is not very useful and glossed over the subject. At least he admitted that he was glossing it over. I've noticed that many of the postings on this board have to do with threading and I...
19
1807
by: frankiespark | last post by:
Hello all, I was perusing the internet for information on threading when I came across this group. Since there seems to be a lot of good ideas and useful info I thought I'd pose a question. Threading is a new concept for me to implement. Here is my problem. I have a system that receives xml files and records their file locations in a database. I can potentially receive thousands,
0
9643
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, 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...
1
10083
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
9946
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...
1
7494
isladogs
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...
0
6737
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
5379
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...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
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.