473,397 Members | 2,099 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,397 software developers and data experts.

A More Effecient Threading?

I am working with an application that requires multithreading. I have found
a sample online that I am currently working with/learning from. However, I
am not sure if this is the most effecient way or most current way of
threading. Here is the sample I have found. I'd appreciate any
feedback/recommendations on better ways to use threading if this sample will
not suffice:

public partial class Form1 : Form
{
private void Form1_Load(object sender, EventArgs e)
{
int iFrom = 0;
int iTo = 5;

LA = new LH[iTo - iFrom + 1];

for (int i = iFrom; i <= iTo; i++)
{
//class defines thread start routine and data
LH Line = new LH();
LH[iTr - iFrom] = Line;

//pass parameters to the new thread
Line.iTr = i;
Line.MainForm = this;
ThreadStart ts = new ThreadStart(Line.ProcessingThread);
Thread wrkThread = new Thread(ts);
Line.CurrentThread = wrkThread;
wrkThread.SetApartmentState(ApartmentState.STA);
//default is MTA
wrkThread.Name = i.ToString(); //for easier tracing
wrkThread.Start();
Thread.Sleep(2000); //add some time for processing
}
}
public class LH
{
public Form1 MainForm = null;
public int iTr = -1;
public Thread CurrentThread = null;

public void ProcessingThread()
{
int iThreadId = CurrentThread.GetHashCode();
try
{
//events here
}
catch (COMException e)
{
return;
}
catch (Exception e)
{
return;
}

Application.Run();
}
}

Thanks for the assistance.

Dec 11 '06 #1
4 1197
Hang on...

Form_Load: you have a Sleep() in you UI code... (after starting the
worker thread) - plus you are creating a whole pile of worker threads
(6 in this case, due to <= iTo) - *each* of which thinks it is running
Application.Run() [at the same time]

Is this what you expected? You are also expecting COM execptions but
not really touching COM?

Personally, I think that Jon's pages are very good on threads:
http://www.yoda.arachsys.com/csharp/threads/

But the bigger question would be: what are you trying to do on each of
these threads, and why? And which are UI threads?

Marc
Dec 11 '06 #2
Also: it is not necessary to pass the "current" thread to the worker;
the worker can get this more reliably via Thread.CurrentThread; and it
would be better to use the ManagedThreadId than the hash-code
(although I seem to recall reading that the hash-code should be unique
for threads; not a guarantee [from me] though!).

Note also that Jon's article spans several pages, not just the one...

IMO, I think you have found a duff sample online... or maybe that
sample is trying to do something *very specific* that doesn't relate
to the general case.

As a final thought: the ThreadPool and BackgroundWorker classes offers
good alternatives to creating your own threads this way. I'm pretty
sure Jon covers both of these.

Marc
Dec 11 '06 #3
Thanks for the reply. Actually there is a COM component involved that needs
to be initialized 6 times in 6 different threads. The sleep methd seems to
allow the COM component to initialize properly without generating errors.
The COM component seems to need a few seconds per thread to initialize. I
will definately loo at Jon's article. Thanks a lot for the reply. I
appreciate it.

"Marc Gravell" <ma**********@gmail.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
Also: it is not necessary to pass the "current" thread to the worker; the
worker can get this more reliably via Thread.CurrentThread; and it would
be better to use the ManagedThreadId than the hash-code (although I seem
to recall reading that the hash-code should be unique for threads; not a
guarantee [from me] though!).

Note also that Jon's article spans several pages, not just the one...

IMO, I think you have found a duff sample online... or maybe that sample
is trying to do something *very specific* that doesn't relate to the
general case.

As a final thought: the ThreadPool and BackgroundWorker classes offers
good alternatives to creating your own threads this way. I'm pretty sure
Jon covers both of these.

Marc
Dec 11 '06 #4
Thanks for the reply. Actually there is a COM component involved that
needs to be initialized 6 times in 6 different threads. The sleep methd
seems to allow the COM component to initialize properly without generating
errors. The COM component seems to need a few seconds per thread to
initialize. I will definately loo at Jon's article. Thanks a lot for the
reply. I appreciate it.
Just an FYI that whenever I hear the term "thread" and "Sleep" in the same
sentence(s) I normally shudder. Thread synchronization should *not* be
carried out with "Sleep()" statements. The time you specify is not only
arbitrary and a waste of time (waiting, say, 2 seconds for a thread to
finish something that takes it a fraction of that time perhaps - this can
also add up if it occurs in a loop and/or many times in your program), but
it's potentially dangerous if the thread finishes sleeping but the thread it
was waiting for hasn't completed its work yet. Use proper synchronization
techniques such as an event semaphore ("ManualResetEvent" for instance).
Dec 11 '06 #5

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

Similar topics

65
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...
2
by: Egor Bolonev | last post by:
hi all my program terminates with error i dont know why it tells 'TypeError: run() takes exactly 1 argument (10 given)' =program==================== import os, os.path, threading, sys def...
77
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...
10
by: Amit | last post by:
Which is more efficient and why? p++ or ++p. Thanks.
6
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...
2
by: DKode | last post by:
Hello, I have created a website that uses a combonation of a content management system and static html pages. The CMS is built with asp.net, and now I am trying to determine the best way to...
2
by: Vjay77 | last post by:
In this code: Private Sub downloadBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) If Not (Me.downloadUrlTextBox.Text = "") Then Me.outputGroupBox.Enabled = True...
11
by: Paul Sijben | last post by:
I am stumped by the following problem. I have a large multi-threaded server accepting communications on one UDP port (chosen for its supposed speed). I have been profiling the code and found...
7
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
jinu1996
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...
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
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...

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.