|
Smart Thread Pool[^] supports cancelation of queued items.
Harvey Saayman - South Africa
Software Developer
.Net, C#, SQL
you.suck = (you.Occupation == jobTitles.Programmer && you.Passion != Programming)
1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111
|
|
|
|
|
|
Glad to be of service
|
|
|
|
|
The standard ThreadPool holds threads for you to use; you never get full control over those threads, so there are a lot of managerial operations you can't perform on them, such as changing their priority, or aborting them. What you can do is implement a co-operational abort, say a while loop where the outside changes the expression or variable that is going to be tested.
public void myThreadAction(...) {
while(!threadStop) {
... go on
}
}
The net result most often is the "abort" is not instantaneous, the while loop may (and should) call some blocking method (some input/output operation, or a Thread.Sleep).
BTW: that is also the way you should do it on normal threads, a hard abort is to be avoided as one never knows in what state objects (such as locks, unmanaged resources) will be at the moment of the abort.
|
|
|
|
|
Luc Pattyn wrote: a hard abort is to be avoided as one never knows in what state objects (such as locks, unmanaged resources) will be at the moment of the abort.
Sorry any elaboration on this...? I can't see my calculation threads has any "State" to it... and my guess is for all practical reason I can throw an exception in the thread just to kill it. Am I missing something, Abort vs Interrupt?
dev
|
|
|
|
|
you'll have state whenever some I/O is included, or something gets locked (e.g. a shared results queue), etc. Here is an excellent article on threading: http://www.albahari.com/threading/[^]
My conclusion is: choose the co-operative way whenever you can.
|
|
|
|
|
Hello everyone,
I am designing a simple convertor, that will take an ammount and convert it to a different unit.
the uits that one can convert between are:
moles
millimoles
grams
milligrams
mirolitres
millilitres
The user enters an amout, selects its unit, and then selects a unit to convert to, and the new value is output onto the winform.
Now, should I write individual conversion functions, and maybe use a case to decide what unit we have, and then call the required converter.
Or, should I have an overloaded function and pass something in to let it decide how to convert?
Any other advice would be greatly appreciated.
Thank you
Steve
|
|
|
|
|
Hmmm...
You should probably have an enumeration that contains the supported units and have one method that takes the magnitude and two units.
But you could also provide separate methods and the above method could just be a switching station -- but with Dictionaries, not switch es.
private static Dictionary<FromUnit,Dictionary<ToUnit,delegate>> dic ... ;
dic [ Unit.Grams ] = ... ;
dic [ Unit.Grams ] [ MilliGrams ] = ConvertGramsToMilliGrams ;
...
return ( dic [ FromUnit ] [ ToUnit ] ( Magnitude ) ) ;
Actually, the populating of the Dictionary can be automated -- perhaps create an Attribute to decorate your methods:
[UnitConvertorAttribute ( From , To )]
public double ConvertGramsToMilliGrams ( double Magnitude , Unit From Unit To ) ...
|
|
|
|
|
stephen.darling wrote: Or, should I have an overloaded function and pass something
IMO, Function overload is fine.
Maintaining switch cases has its own overhead, especially for the developer who is going to maintain this code in the future.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
My latest tip/trick
Visit the Hindi forum here.
|
|
|
|
|
Abhinav S wrote: IMO, Function overload is fine.
So what would I pass in to diffenciate the overloaded function?
For example, please consider the following punction
convert(float startValue, float outValue)
And I wanted to convert milliGrams to grams, how would this function know that?
would I need something like...
convert (float from, float to, UNIT_TYPE)
Where unit_type would be an enum, perhaps, to allow the correct units to be used. Or can you think of a better idea?
Thank you
Steve
|
|
|
|
|
Have you looked at applying the Strategy Pattern[^] or here[^].
[corrected links]
I know the language. I've read a book. - _Madmatt
modified on Sunday, October 10, 2010 5:20 PM
|
|
|
|
|
stephen.darling wrote: Now, should I write individual conversion functions, and maybe use a case to decide what unit we have, and then call the required converter.
The function is "always" the same; you take the base, and multiply it with a conversion-factor. The only thing you'd need to do is to match a factor to a unit, relative to it's base.
The only exception to this rule that I could find are temperatures
I are Troll
|
|
|
|
|
What about shoe sizes?
|
|
|
|
|
PIEBALDconsult wrote: What about shoe sizes?
Never considered those, but you're right
I are Troll
|
|
|
|
|
Is there a direct way to access a datatable in a collection directly by its name?
Currently, the only way I know how access a datatable is by using a foreach statement to find the table I wish to update/modify:
DataTableCollection tables = collection.Tables;<br />
<br />
foreach (DataTable tbl in tables)<br />
{<br />
string name = tbl.TableName;<br />
<br />
if (name == "name")<br />
{<br />
DataRow myNewRow;<br />
myNewRow = tbl.NewRow();<br />
<br />
code.....<br />
<br />
tbl.Rows.Add(myNewRow); <br />
<br />
}<br />
}
Thanks for any help
|
|
|
|
|
Umm... perhaps DataTableCollection.Item[^]
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
Thanks for the answer. I've not used datatable collections before and was spinning my wheels. So I can replace the foreach loop with:
DataTable table = tables["name"];
|
|
|
|
|
Yep. Should work for all collections!
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
ds = DB.FillDataSet(cmd, "StRecord");
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
foreach (DataColumn col in ds.Tables[0].Columns )
{
}
From: <ahref="http: www.programlive.tk"="">http://www.programlive.tk
|
|
|
|
|
I have written a program in C# using inpout32.dll which, when you press a button, it sets all the data pins on the paralell port to high by setting the address as 888 and the decimal value 255 to that address. I have heard that when a pin on the port is a high, it is outputting a voltage of 5v, but it shows about 1.5v constantly, not changing, while none of the other data ports do anything. I have a feeling this is not working because I either have the wrong address, or I am setting the wrong value.
In my BIOS, it says that the address is 378h, which I know if 888 in decimal, which is why I am setting it as the address. I also have the mode set to EPP. Can somebody tell me what I am doing wrong?
|
|
|
|
|
I don't know. I've used that in one small app. My code is:
[
System.Runtime.InteropServices.DllImportAttribute
(
"INPOUT32"
,
SetLastError=true
,
EntryPoint="Out32"
)
]
private static extern void
API_Out32
(
System.Int32 Port
,
System.Int32 Data
) ;
public static void
WriteToPort
(
System.UInt16 Port
,
System.Byte Data
)
{
API_Out32
(
Port
,
Data
) ;
return ;
}
PIEBALD.Lib.LibApi.WriteToPort ( 0x378 , (byte) this.tvStati.Tag ) ;
|
|
|
|
|
hi, i want to store 2 strings which are named "player" and "score" ,
how do i create a table for this and i want to know how to display this information ...
thanks for all your help
|
|
|
|
|
XML
<Game>
<Players>
<Player Name="Joshua" Score="42" />
</Players>
</Game>
How you display it is up to you.
|
|
|
|
|
Won't that give InsaneDataException at name?
|
|
|
|
|
What? I said Joshua, not HAL.
|
|
|
|