 |
|
 |
I recently started working for a new company and found quite a few situations like this:
Thread m_thread = new Thread(new ThreadStart(GenerateTimeLineImage));
m_thread.Start();
m_thread.Join();
I'm no expert on mulithreading but there are so many different (not just brainless copy-paste) of code like that, that it made me wondering is there some hidden magic behind that code that I'm not aware of.
So, what's the difference between that and
GenerateTimeLineImage();
besides obvious performance loss due to creating a pointless thread?
|
|
|
|
 |
|
 |
I dont think you missed the magic here. I think, you are right that the thread is pointless in this sample.
You should speak to the author of this code if hes still in your company.
|
|
|
|
 |
|
 |
He's not, I'm his replacement...
|
|
|
|
 |
|
 |
That's why you are replacing him - you can see it.
|
|
|
|
 |
|
 |
Yes you are right that this code works synchronously albeit using a separate thread. The coder needs to stand in the corner with a dunce cap on.
Cheers!
"With sufficient thrust, pigs fly just fine."
Ross Callon, The Twelve Networking Truths, RFC1925
|
|
|
|
 |
|
 |
I think that makes sense in a WinForms app -- if GenerateTimeLineImage takes a bit of time, which seems likely.
|
|
|
|
 |
|
 |
It's not a winforms app, but even it were, it still don't make sense. GenerateTimeLineImage is a heavy function but it would still block the UI thread (if that's why you mentioned WinApp) because of the m_thread.Join() there.
|
|
|
|
 |
|
 |
No, not the least. In that case the UI thread would be waiting until the other thread is finally done. Meanwhile the UI would not respond, just as if the task had been done on the UI thread itself.
At least artificial intelligence already is superior to natural stupidity
|
|
|
|
 |
|
 |
Thread.Join Method
Blocks the calling thread until a thread terminates, while continuing to perform standard COM and SendMessage pumping.
|
|
|
|
 |
|
 |
Does this actually work? (I.e. do your event handlers get called, does the window repaint properly, etc?)
|
|
|
|
 |
|
 |
No. Try the following code - The ui blocks for 10 seconds - i am sure.
private void button1_Click(object sender, EventArgs e)
{
Thread m_thread = new Thread(new ThreadStart(GenerateTimeLineImage));
m_thread.Start();
m_thread.Join();
}
void GenerateTimeLineImage()
{
Thread.Sleep(10000);
}
|
|
|
|
 |
|
 |
OK, I sit corrected. However, maybe the developer often needs to do that with several items:
Thread t1 = ... ; t1.Start() ;
Thread t2 = ... ; t2.Start() ;
...
Thread tn = ... ; tn.Start() ;
Join (t1 ) ;
Join (t2 ) ;
...
Join (tn ) ;
and uses this as a general pattern. :shrug:
In which case I might consider a method that takes a collection of delegates:
private static void
InvokeAll
(
params System.Threading.ThreadStart[] Items
)
{
/* Null checking :D */
System.Threading.Thread[] thread = new System.Threading.Thread [ Items.Length ] ;
for ( int i = 0 ; i < Items.Length ; i++ )
{
if ( Items [ i ] != null )
{
thread [ i ] = new System.Threading.Thread ( Items [ i ] ) ;
thread [ i ].Start() ;
}
}
for ( int i = 0 ; i < thread.Length ; i++ )
{
if ( thread [ i ] != null )
{
thread [ i ].Join() ;
}
}
return ;
}
(Untested)
|
|
|
|
 |
|
|
 |
|
 |
The only reason I can imagine doing something like that is if there is a requirement that GenerateTimeLineImage() execute on a non-UI thread, and yet be synchronized to the UI thread that requests the operation.
There are plenty of the opposite circumstance (requesting a UI operation from a worker thread), but this sounds strange.
Software Zen: delete this;
|
|
|
|
 |
|
 |
Or perhaps to protect the UI thread from Exceptions?
|
|
|
|
 |
|
 |
Or perhaps there is another thread that somehow manages these extra threads (e.g., abort them if they take too long).
|
|
|
|
 |
|
 |
Hi Friends...
I have AJAX calender control in my application.
But i am not able to select the day before from current date.
Only able to select after the current date.
What can i Do.
Collapse | Copy Code<asp:Label ID="lblstartdate" Text="Start Date" CssClass="lbl" runat="server"></asp:Label>
<AjaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</AjaxToolkit:ToolkitScriptManager>
<asp:TextBox ID="txtstartdate" CssClass="txtbox" runat="server"></asp:TextBox>
<ajaxtoolkit:calendarextender runat="server" ID="calExtender2"
PopupButtonID="btnDate3" TargetControlID="txtstartdate" Format="MMMM d, yy" />
<asp:ImageButton ID="btnDate3" ImageUrl="~/images/CalendarIcon.jpg" Width="20px"
runat="server" />
modified 37 mins ago.
|
|
|
|
 |
|
 |
Little confused. why is this in "Hall of shame"?
|
|
|
|
 |
|
 |
Pretty obvious, OP should be ashamed for posting this here
It's an OO world.
public class Naerling : Lazy<Person>{
public void DoWork(){ throw new NotImplementedException(); }
}
|
|
|
|
 |
|
|
 |
|
 |
I think he 'mis-responded'.
---------------------------------
I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave
CCC Link[ ^]
English League Tables - Live
|
|
|
|
 |
|
 |
Just found this gem in my own code Worst part is that this runs quite often in tight loops in production code, haha.
public class SomeClass()
{
private PropertyInfo _propertyInfo;
public SomeClass(PropertyInfo propertyInfo)
{
_propertyInfo = propertyInfo;
}
public void ProcessStuff(object obj, object value)
{
obj.GetType().GetProperty(_propertyInfo.Name).SetValue(obj, value, null);
}
}
Who can spot the blunder?
|
|
|
|
 |
|
 |
Wow!!!
obj.GetType().GetProperty(_propertyInfo.Name)
LOL
But it might be useful, if you want to set a value on a different type that has a property with same name!! otherwise
|
|
|
|
 |
|
 |
Haha, I guess that might be an unlikely scenario in some weird situation, but not the case here unfortunately lol, just pure sillyness
|
|
|
|
 |
|
 |
If he has several classes with common properties or methods, then why does he not simply use an interface and pass objects that inherit from that interface? Reflection can be very useful at times, but it should not be misused to work around flaws in the class design.
Edit: And yes, that code must fall on its nose with an exception when objects are passed which do not have the property he's looking for. It's also not assured that _propertyInfo has ever been set, so a null reference is also possible.
At least artificial intelligence already is superior to natural stupidity
|
|
|
|
 |