|
Thank you your reply.
Is there any example to use multi threading (Asynchornous and thread pool) in C# window service application?
|
|
|
|
|
Does this[^] help?
Now...bring me that horizon. And really bad eggs...Drink up me hearties, YO HO!
|
|
|
|
|
|
Luc Pattyn wrote: have similar code in more than 1*N to 2*N threads
My rule of thumb is 1.5*N
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
|
|
|
|
|
Harvey Saayman wrote: 1.5*N
you may run into trouble on an AMD triple core...
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. [The QA section does it automatically now, I hope we soon get it on regular forums as well]
|
|
|
|
|
Even on single core dinosaurs...
I was thinking that same thing just after clicking post on my OP
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
|
|
|
|
|
Having rules of thumb like 2* number of processors is a totally pointless affair.
Consider the following scenario
--I want to wait on 70 blocking operations
--When one of these operations complete. I would like a response/somethingto happen, quickly (Low latency)
I could
*Have 2*2 processors threads constantly polling all 70 blocking operations every millisecond to see when they are done. This would waste lots of CPU
*Use 70 threads which are all in Wait mode which would consume very little CPU
Using 70 threads would be far better in this case
The number of threads you need has nothing to do with the number of CPUs. And everything to do with latency and how much blocking operations you need to wait on
|
|
|
|
|
Theoretically, yes... you can have unlimited threads. But in practice, not so much
As Luc pointed out 500 threads are a bit heavy. If their running similar code I'd use Smart Thread Pool[^]
Its easy to use, quite flexible and to quote JSOP, "It simply works"
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
|
|
|
|
|
How can I copy a multidimensional List ?
public class DSensingDataCollection
{
private string _DAreaID;
private string _DModel1;
private string _DModel2;
private string _DModel3;
private string _DDeviceID;
private string _DDeviceType;
private bool _DIsNow;
private int _DResult;
private int _DSeq;
private int _DSensorCount;
private List<DSensingData> _DSensingData;
public string DAreaID
{
get { return _DAreaID; }
set { _DAreaID = value; }
}
public string DModel1
{
get { return _DModel1; }
set { _DModel1 = value; }
}
public string DModel2
{
get { return _DModel2; }
set { _DModel2 = value; }
}
public string DModel3
{
get { return _DModel3; }
set { _DModel3 = value; }
}
public string DDeviceID
{
get { return _DDeviceID; }
set { _DDeviceID = value; }
}
public string DDeviceType
{
get { return _DDeviceType; }
set { _DDeviceType = value; }
}
public bool DIsNow
{
get { return _DIsNow; }
set { _DIsNow = value; }
}
public int DResult
{
get { return _DResult; }
set { _DResult = value; }
}
public int DSeq
{
get { return _DSeq; }
set { _DSeq = value; }
}
public int DSensorCount
{
get { return _DSensorCount; }
set { _DSensorCount = value; }
}
public List<DSensingData> DSensingData
{
get { return _DSensingData; }
set { _DSensingData = value; }
}
public DSensingDataCollection()
{
this.DAreaID = "";
this.DModel1 = "";
this.DModel2 = "";
this.DModel3 = "";
this.DDeviceID = "";
this.DDeviceType = "";
this.DIsNow = true;
this.DResult = 0;
this.DSeq = 0;
this.DSensorCount = 0;
this.DSensingData = new List<dsensingdata>();
}
}
public class DSensingData
{
private string _DSensorTypeID;
private string _DMinValue;
private string _DMaxValue;
private int _DSlot;
private float _DValue;
private float _DDelta;
public string DSensorTypeID
{
get { return _DSensorTypeID; }
set { _DSensorTypeID = value; }
}
public string DMinValue
{
get { return _DMinValue; }
set { _DMinValue = value; }
}
public string DMaxValue
{
get { return _DMaxValue; }
set { _DMaxValue = value; }
}
public int DSlot
{
get { return _DSlot; }
set { _DSlot = value; }
}
public float DValue
{
get { return _DValue; }
set { this._DValue = value; }
}
public float DDelta
{
get { return _DDelta; }
set { this._DDelta = value; }
}
public DSensingData()
{
this._DSensorTypeID = "";
this._DMinValue = "";
this._DMaxValue = "";
this._DSlot = 0;
this._DValue = 0;
this._DDelta = 0;
}
public DSensingData(string _DSensorTypeID, string _DMinValue, string _DMaxValue, int _DSlot, float _DValue, float _DDelta)
{
this._DSensorTypeID = _DSensorTypeID;
this._DMinValue = _DMinValue;
this._DMaxValue = _DMaxValue;
this._DSlot = _DSlot;
this._DValue = _DValue;
this._DDelta = _DDelta;
}
}
public class DDeviceData
{
private string _DeviceID;
private DBufferData _DBufferData;
public string DeviceID
{
get { return _DeviceID; }
set { _DeviceID = value; }
}
public DBufferData DBufferData
{
get { return _DBufferData; }
set { _DBufferData = value; }
}
public DDeviceData()
{
this._DeviceID = "";
this._DBufferData = new DBufferData();
}
public DDeviceData(string param)
{
this._DBufferData = new DBufferData();
if (uHeader.gDevice.ContainsKey(param) == false)
{
this._DBufferData = new DBufferData();
uHeader.gDevice.Add(param, this._DBufferData);
}
else
{
IDictionaryEnumerator items = uHeader.gDevice.GetEnumerator();
while (items.MoveNext())
{
if (items.Key.ToString().Equals(param))
{
this._DBufferData = (DBufferData)items.Value;
}
}
this.DBufferData.DSensingDataCol3.Clear();
-- First and second array to array
this.DBufferData.DSensingDataCol2.Clear();
-- 0 the first array to array
this._DBufferData.DSensingDataCol1.Clear();
}
DSensingDataCollection input1 = new DSensingDataCollection();
input1.DAreaID = "1234";
input1.DDeviceID = "1234";
input1.DDeviceType = "edv";
input1.DIsNow = true;
input1.DModel1 = "fm001";
input1.DModel2 = "fm002";
input1.DModel3 = "fm003";
input1.DResult = 0;
input1.DSensorCount = 8;
input1.DSeq = 3;
input1.DSensingData.AddRange(new DSensingData[]{
new DSensingData("edv","34.1", "45.1", 0, 12.9f, 10.8f)});
this.DBufferData.DSensingDataCol1.Add(input1);
DSensingDataCollection input2 = new DSensingDataCollection();
input2.DAreaID = "2234";
input2.DDeviceID = "2234";
input2.DDeviceType = "edv";
input2.DIsNow = true;
input2.DModel1 = "fm001";
input2.DModel2 = "fm002";
input2.DModel3 = "fm003";
input2.DResult = 0;
input2.DSensorCount = 8;
input2.DSeq = 3;
input2.DSensingData.AddRange(new DSensingData[]{
new DSensingData("edv","11.1", "12.1", 0, 3.5f, 4.2f)});
this.DBufferData.DSensingDataCol1.Add(input2);
}
}
public class DBufferData
{
private List<DSensingDataCollection> _DSensingDataCol1;
private List<DSensingDataCollection> _DSensingDataCol2;
private List<DSensingDataCollection> _DSensingDataCol3;
public List<DSensingDataCollection> DSensingDataCol1
{
get { return _DSensingDataCol1; }
set { _DSensingDataCol1 = value; }
}
public List<DSensingDataCollection> DSensingDataCol2
{
get { return _DSensingDataCol2; }
set { _DSensingDataCol2 = value; }
}
public List<DSensingDataCollection> DSensingDataCol3
{
get { return _DSensingDataCol3; }
set { _DSensingDataCol3 = value; }
}
public DBufferData()
{
this._DSensingDataCol1 = new List<DSensingDataCollection>();
this._DSensingDataCol2 = new List<DSensingDataCollection>();
this._DSensingDataCol3 = new List<DSensingDataCollection>();
}
}
class Program
{
static void Main(string[] args)
{
string deviceid = "53410101";
DDeviceData ar = new DDeviceData(deviceid);
DDeviceData br = new DDeviceData(deviceid);
}
}
|
|
|
|
|
Have the courtesy to read the posting guidelines and format your code if you are going to post any. You don't need any of this code to ask the simple question.
This is a useless waste of everyones time
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Sorry.
Could not be too urgent to clean up the rest.
Once again I apologize.
|
|
|
|
|
Here you go:
public class SlimList<T> : IList<T>
{
#region Constants
private const int OVERHEAD = 31;
private const int MAX_CAPACITY = 1 << 30;
private const string NEGATIVE_CAPACITY = "Capacity cannot be less than 0.";
#endregion
#region Variables
private T[][] items;
private int count;
private int capacity;
private int capacityIndex;
private int lastSize;
#endregion
#region Properties
public T this[int index]
{
get
{
ValidateIndex(index);
int firstIndex = (index == 0 ? 0 : (int)Math.Truncate(Math.Log(index, 2)));
int baseIndex = (firstIndex == 0 ? 0 : (1 << firstIndex));
int offset = index - baseIndex;
return items[firstIndex][offset];
}
set
{
ValidateIndex(index);
int firstIndex = (index == 0 ? 0 : (int)Math.Truncate(Math.Log(index, 2)));
int baseIndex = (firstIndex == 0 ? 0 : (1 << firstIndex));
int offset = index - baseIndex;
items[firstIndex][offset] = value;
}
}
public int Count
{
get
{
return this.count;
}
}
public int Capacity
{
get
{
return this.capacity;
}
}
public bool IsReadOnly
{
get
{
return false;
}
}
#endregion
#region Constructors
public SlimList()
{
this.Clear();
}
public SlimList(int initialCapacity)
{
this.Clear();
if (initialCapacity == 0)
{
return;
}
else if (initialCapacity < 0)
{
throw new InvalidOperationException(NEGATIVE_CAPACITY);
}
this.items[0] = new T[2];
this.capacity = 2;
this.capacityIndex = 0;
this.lastSize = 1;
while (this.capacity < initialCapacity)
{
this.capacityIndex++;
this.lastSize <<= 1;
this.capacity <<= 1;
this.items[this.capacityIndex] = new T[this.lastSize];
}
}
public SlimList(IEnumerable<T> collection)
{
this.Clear();
foreach (T item in collection)
{
this.Add(item);
}
}
#endregion
#region Methods
public int IndexOf(T item)
{
EqualityComparer<T> comparer = EqualityComparer<T>.Default;
for (int i = 0; i < count; i++)
{
if(comparer.Equals(item, this[i]))
{
return i;
}
}
return -1;
}
private void ValidateIndex(int index)
{
if (index < 0 || index >= count)
{
throw new IndexOutOfRangeException();
}
}
public void Insert(int index, T item)
{
this.ValidateIndex(index);
this.Add(this[this.count - 1]);
for (int i = this.count - 1; i > index; i--)
{
this[i] = this[i - 1];
}
this[index] = item;
}
public void RemoveAt(int index)
{
this.ValidateIndex(index);
for (int i = index; i + 1 < this.count; i++)
{
this[i] = this[i + 1];
}
this.count--;
}
public void Add(T item)
{
count++;
if (count > capacity)
{
if (capacity == 0)
{
capacity = 1;
this.lastSize = 1;
}
else if (capacity == 2)
{
this.lastSize = 1;
}
else if (capacity == MAX_CAPACITY)
{
throw new InvalidOperationException("The list is full.");
}
capacity <<= 1;
this.capacityIndex++;
this.lastSize <<= 1;
this.items[this.capacityIndex] = new T[this.lastSize];
}
this[count - 1] = item;
}
public void Clear()
{
items = new T[OVERHEAD][];
this.count = 0;
this.capacity = 0;
this.capacityIndex = -1;
this.lastSize = 0;
}
public bool Contains(T item)
{
return this.IndexOf(item) >= 0;
}
public void CopyTo(T[] array, int arrayIndex)
{
for (int i = 0; i < this.count; i++)
{
array[i + arrayIndex] = this[i];
}
}
public bool Remove(T item)
{
int index = this.IndexOf(item);
bool removed;
if (index >= 0)
{
this.RemoveAt(index);
removed = true;
}
else
{
removed = false;
}
return removed;
}
public IEnumerator<T> GetEnumerator()
{
for (int i = 0; i < this.count; i++)
{
yield return this[i];
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
#endregion
}
Actually, none of that has anything to do with multidimensional lists. See, isn't it annoying when somebody posts a bunch of code that is completely unrelated to the topic at hand? Also, see how nice my code looks (with all the colors and tabs)? You can do that by surrounding your code with a PRE tag and specifying the lang attribute (in your case, it would be C#). For example, you could type this:
<pre lang="C#">int x = 5;</pre>
|
|
|
|
|
I haven't read all of that, but I was shocked by this bit inside your constructor:
aspdotnetdev wrote: while (this.capacity < initialCapacity)
{
this.capacityIndex++;
this.lastSize <<= 1;
this.capacity <<= 1;
this.items[this.capacityIndex] = new T[this.lastSize];
}
Why on earth would you create several arrays when you only need one?
If you don't know how to get the next power of 2 directly, then loop over the capacity WITHOUT creating the beast, and create one array once you decided on the right size.
Finding the next power of 2 is similar to locating the most significant bit set (x86 has an instruction that does that, so a little P/Invoke to assembly code could be the fastest), or determining the integer log2 (I am NOT suggesting you use the Math class for this!).
Without special tricks, the fastest way AFAIK is a binary search with some nested IFs that mask certain bit fields.(Is any bit set in the top half? is any bit set in the top quarter? etc)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. [The QA section does it automatically now, I hope we soon get it on regular forums as well]
|
|
|
|
|
Haha, very observant. I discuss that at the bottom of my article, SlimList. The instruction you are talking about is BSR (bit scan reverse). It checks for the number of consecutive zeros in the most significant bits, or something like that. Point is, it gets you a fast way to calculate the base-2 integer log. I didn't want SlimList to be platform-dependent, so I didn't go with BSR. Also, I didn't want the code to be confusing, so I didn't go with a binary search of the bits... rather, I went with Math.Log, because it was the most obvious.
Luc Pattyn wrote: Why on earth would you create several arrays when you only need one?
To avoid having to reallocate an array twice the size of the original array and copy values over when the number of elements has exceeded the capacity of the original array (the common technique for creating a List structure). Doing so has the potential to slow things down due to excessive memory allocations and also has the down-side of having 3x the required memory (1x by the old array and 2x by the new array). With SlimList, the old arrays take up 1x memory and the new array takes up 1x memory, for a total of 2x memory during list expansion. And thanks to my use of Log to directly calculate the proper index, the indexing performance is still O(1) (though it could be argued that Log is not truly an O(1) operation, it could be implemented as such with an array to perform the lookup rather than perform the calculation, or in hardware, as the BSR instruction demonstrates, though both are moot points when you consider that you would have to use an n-bit number to perform the lookup anyway with either List or SlimList). Keep in mind that my article is mostly academic in nature. I created it to present a new data structure, not to provide a class that should be used out of the box (as it is implemented, it has some performance issues, which are discussed at the bottom of the article). If your code will only be used on x86 computers, perhaps you go with BSR. Or maybe you go with a different instruction on a different platform. But most of the time, the best choice is to just use .Net's List.
|
|
|
|
|
aspdotnetdev wrote: (though it could be argued that Log is not truly an O(1) operation, it could be implemented as such with an array to perform the lookup rather than perform the calculation, or in hardware, as the BSR instruction demonstrates, though both are moot points when you consider that you would have to use an n-bit number to perform the lookup anyway with either List or SlimList)
Sorry in advance for that "little" digression
|
|
|
|
|
aspdotnetdev wrote: as it is implemented, it has some performance issues, which are discussed at the bottom of the article in the forum
FTFM.
|
|
|
|
|
great little monologue.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. [The QA section does it automatically now, I hope we soon get it on regular forums as well]
|
|
|
|
|
I have a requirement to be able to send keypresses to another application(yes this is the only option). I also cannot bring that application to the foreground(voiding the use of sendkeys). Ive found "postmessage" which does work, except that it seems to send the key directly to the program instead of simulating a keyboard press. So I need something that makes the application think it has come directly from the keyboard(which will also mean that another logging application I have will be able to pick the press up - as it doesnt with postmessage).
Is there maybe a function that I can call with p/invoke?
Thanks
Strive to be humble enough to take advice, and confident enough to do something about it.
|
|
|
|
|
roguemat wrote: I also cannot bring that application to the foreground(voiding the use of sendkeys).
Not sure if this is any help, but: Have you thought about bringing it to the foreground outside of the visible desktop area?
You could bring the window up at -1000, -1000 (and make sure it's window size doesn't exceed that of course), use SendKey and put it back to its original position. May not be very elegant, but it should work.
Standards are great! Everybody should have one!
|
|
|
|
|
I am tryng to split a process in some methods stopping the methods with objects ManualResetEvent and AutoResetEvent but I am having problems with the WaitHandle.WaitAll() method with this method I want stop the proccess until everyone event is pointed, but I receive the error message of "Not valid WaitAll for various events in a STA proccess"
Please ho can I solve the problem
|
|
|
|
|
Use Google. The first hit that comes up already provides a solution:
private void WaitAll(WaitHandle[] waitHandles) {
if (Thread.CurrentThread.ApartmentState == ApartmentState.STA) {
foreach(WaitHandle myWaitHandle in waitHandles) {
WaitHandle.WaitAny(new WaitHandle[]{myWaitHandle});
}
}
else {
WaitHandle.WaitAll(waitHandles);
}
}
Standards are great! Everybody should have one!
|
|
|
|
|
You can start a new thread, set its apartment state to MTA and call the WaitAll method there. Then just call Join on the thread. It is much better then using a foreach loop.
|
|
|
|
|
Hi anyone, i asking about managed and unmanaged code and why we need that???
I need a good reference to know in depth about them...........
Regards
|
|
|
|
|
This looks ok. Or you can just try Wikipedia and the references in the bottom of the page.
Standards are great! Everybody should have one!
|
|
|
|
|
I would also recommend you pick up a book like CLR via C# and read.
Now...bring me that horizon. And really bad eggs...Drink up me hearties, YO HO!
|
|
|
|
|
|
I have an address book program completely done, except for one thing...
When a name is highlighted in the listbox, I need all of their information displayed in the related textboxes. All the contact information is stored in an array. I was playing with the code, and I know how to display just the information in the listbox, which is the first and last name, but I need ALL of the contact info displayed in the textboxes. I have included my code, and a screenshot of what should happen when a name is selected from the listbox.
Screen Shot[]
public partial class frmAddressBook : Form
{
string[] Contacts = new string[20];
int i = 0;
public frmAddressBook()
{
InitializeComponent();
}
private void AddressBook_Load(object sender, EventArgs e)
{
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnAdd_Click(object sender, EventArgs e)
{
string lastName = tbxLastName.Text;
string firstName = tbxFirstName.Text;
string street = tbxStreet.Text;
string city = tbxCity.Text;
string state = tbxState.Text;
string zip = tbxZip.Text;
if (i < 20)
{
Contacts[i] = lastName + ", " + firstName;
i++;
}
else
{
MessageBox.Show("The Address Book Can Hold a Maximum of 20 Contacts.", "Address Book Full");
}
tbxFirstName.Text = "";
tbxLastName.Text = "";
tbxStreet.Text = "";
tbxCity.Text = "";
tbxState.Text = "";
tbxZip.Text = "";
lstContacts.Items.Clear();
for (int j = 0; j < 20; j++)
{
if (Contacts[j] != null)
{
lstContacts.Items.Add(Contacts[j].ToString().Trim());
}
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
for (int k = 0; k < Contacts.Length; k++)
{
try
{
if (Contacts[k].Contains(lstContacts.SelectedItem.ToString()))
{
Contacts[k] = null;
}
}
catch
{
}
}
lstContacts.Items.Clear();
for (int j = 0; j < 10; j++)
{
if (Contacts[j] != null)
{
lstContacts.Items.Add(Contacts[j].ToString().Trim());
}
}
}
private void lstContacts_SelectedIndexChanged(object sender, EventArgs e)
{
tbxFirstName.Text = lstContacts.SelectedItem.ToString();
}
}
}
|
|
|
|