|
Could you give me the simplest example how to do it? I can't cope with it, I'm new to C#
|
|
|
|
|
My outlook bar contains number of icons and names for those each icon. If Iright click my mouse button then I can rename the text in the icon name. I want to disable this option. Any ideas?
rajitha_kba
|
|
|
|
|
Is this siutable for C# ?
|
|
|
|
|
Ok i am re-asking the question ,probably i wasn`t clear enough to get the attention of people like Heath Stewart
i am working on a add-on Application which would be crawler like,means there would be an entry point URL(e.g: http://site.com/virtual/index.asp),the links from the page will be retrieved and then same process will be repeated with those links,during this exercise,i found relative link on a webpage,for instance "members/file.asp" etc
sine in next round,i have to access the above mentioned URL via HTTPWEbrequest,for that i would have to pass url something like http://site.com/virtual/members/file.asp,where bold part is the part has to be attached with the resultant Url,in html,web browser can understand relative paths, for instance,browser will convert the relative url "../my.asp" to http://site.com/my.asp when its rendered on browser,how could it be done if its done on a non web based application in C#
Thanks
MyBlogs
http://weblogs.com.pk/kadnan
|
|
|
|
|
|
I've got some long-running operation that could be cancelled by the user. The way I'm doing it is
1. launch a second thread to do the entire operation.
2. when the second thread comes to the long-running piece of the operation, it launches a 3rd thread
3. the second thread monitors the 3rd thread, waiting for it to complete, while also monitoring if the user has cancelled.
My code looks something similar to
void DoHeavy()
{
EatCpuForTwoMinutes();
}
void DoEntireOperation(ICancellable cancelFlag)
{
...
ThreadStart heavyMethod = new ThreadStart(DoHeavy);
IAsyncResult result = heavyMethod.BeginInvoke(null, null);
while(result.IsCompleted == false)
{
if(cancelFlag.IsCancelPending) break;
else Thread.Sleep(250);
}
...
}
I feel like Thread.Sleep is a total waste, since the operation could complete before 250 milliseconds is up. I also feel this is inducing too much overhead, constantly checking whether the delegate is completed. Is there a better way to do this?
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Horrific Minnesota Radio
Judah Himango
|
|
|
|
|
But do you actually kill the DoHeavy thread? Is there someplace that the DoHeavy thread can check to see whether it should cancel itself? That's usually the best practice.
As it is now, it looks like you are allowing the second thread to exit, but DoHeavy will continue running, eating up CPU cycles until it's done. This is probably not ideal.
Marc
MyXaml
Advanced Unit Testing
YAPO
|
|
|
|
|
I don't kill the DoHeavy thread because DoHeavy is calling a method in a library I don't have the source code for (so I can't 'cancel' the thread, so to speak). And I'm a firm believer in abstaining from Thread.Abort calls, especially from asynchronous code.
The slow method in question is actually a synchronous IO operation, one which the library providers did not provide an asynchronous alternative to, so in reality it's not so much eating the CPU cycles as it is just wasting disk space on an disk writing operation.
As far as the Thread.Sleep() & operation.IsCompleted calls inside the while loop, is there any better way to do that? If not, would it be wise to make shorter iterations in the sleep loop (causing more IsCompleted calls) or better to sleep more and query IsCompleted less often?
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Horrific Minnesota Radio
Judah Himango
|
|
|
|
|
This really brings up a question i have in mind, which I've tried but with no success. Assume the following:-
<br />
Thread _thread = new ThreadStart(SomeMethod)<br />
public void SomeMethod()<br />
{<br />
}<br />
The aboved code is ok.
But, if I wanted to do this,
<br />
Thread _thread = new ThreadStart(SomeMethod)<br />
public void SomeMethod(int a, int b)<br />
{<br />
}<br />
It will complain, because the delegate does NOT take any callback functions with parameters. Question, is there a way to achieve what I'm trying to do ?
Provide sample syntax if got possible solution(s).
Thanks
Stanley
|
|
|
|
|
Yes, but not with Thread.Start (although you can in .NET 2.0, because Thread.Start will have overloads to take a ParameterizedThreadStart, which allows you to pass a System.Object parameter).
For .NET 1.x code, you have 2 options.
You could use the built-in .NET threadpool:
void SomeMethod(object parameter)
{
int[] typedParam = (int[])parameter;
}
WaitCallback waitCallback = new WaitCallback(SomeMethod);
ThreadPool.QueueUserWorkItem(waitCallback, new int[]{1, 5});
Or you can define your own delegate (or use an existing one) that matches your desired method signature (i.e. has the correct number of parameters and the correct parameter types), then call delegate.BeginInvoke on it:
delegate void MethodWithTwoIntegers(int a, int b);
void SomeMethod(int a, int b)
{
}
MethodWithTwoIntegers myMethod = new MethodWithTwoIntegers(SomeMethod);
myMethod.BeginInvoke(5, 10, null, null);
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Horrific Minnesota Radio
Judah Himango
|
|
|
|
|
|
|
Anybody knows how to implement a Generic Hashtable?
Thanks
|
|
|
|
|
Anonymous wrote:
Anybody knows how to implement a Generic Hashtable?
Um, not to sound sarcastic, but can you be more specific? .NET's Hashtable implementation seems pretty generic to me--both keys and data are objects.
Marc
MyXaml
Advanced Unit Testing
YAPO
|
|
|
|
|
What I meant is right now Hashtable returns an object which we need to cast to the specific type. This is what I want to eliminate using Generic so I can have a List with Key Value pair but with the specific type value.
Thanks
|
|
|
|
|
Here's my 'simple' take. Some features are missing and some things are slighty different to a traditional hashtable. Bit more than a 100 lines
public class GrowingHashtable
{
struct bucket
{
internal object key;
internal object val;
}
int size;
bucket[] buckets;
static readonly uint[] primes = { 11,17,23,29,
37,47,59,71,89,107,131,163,197,239,293,353,431,521,631,761,919,
1103,1327,1597,1931,2333,2801,3371,4049,4861,5839,7013,8419,10103,12143,14591,
17519,21023,25229,30293,36353,43627,52361,62851,75431,90523, 108631, 130363,
156437, 187751, 225307, 270371, 324449, 389357, 467237, 560689, 672827, 807403,
968897, 1162687, 1395263, 1674319, 2009191, 2411033, 2893249, 3471899, 4166287,
4999559, 5999471, 7199369 };
public GrowingHashtable()
{
size = 0;
buckets = new bucket[0];
}
static uint Hash(object key)
{
return unchecked( (uint) key.GetHashCode());
}
uint GetCap()
{
uint i = 0, l = 0;
for (;i < size;i++)
{
l += primes[i];
}
return l;
}
int AddBucketSet()
{
bucket[] newbuckets = new bucket[GetCap() + primes[size]];
Array.Copy(buckets, newbuckets, buckets.Length);
buckets = newbuckets;
return ++size;
}
public void Add(object key, object value)
{
for (uint i = 0, pos = 0, hash = Hash(key); i < size; i++)
{
pos += hash % primes[i];
if (buckets[pos].key == null)
{
buckets[pos].key = key;
buckets[pos].val = value;
return;
}
}
AddBucketSet();
Add(key, value);
}
public bool Contains(object key)
{
for (uint i = 0, pos = 0, hash = Hash(key); i < size;i++)
{
pos += hash % primes[i];
if (key.Equals(buckets[pos].key))
{
return true;
}
}
return false;
}
public object this[object key]
{
get
{
for (uint i = 0, pos = 0, hash = Hash(key); i < size; i++)
{
pos += hash % primes[i];
if (key.Equals(buckets[pos].key))
{
return buckets[pos].val;
}
}
return null;
}
set
{
if (!Contains(key))
{
Add(key, value);
}
else
{
for (uint i = 0, pos = 0, hash = Hash(key); i < size; i++)
{
pos += hash % primes[i];
if (key.Equals(buckets[pos].key))
{
buckets[pos].val = value;
}
}
}
}
}
}
xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots
|
|
|
|
|
Does .NET support any way of implementing an OLE server?
I have to write an app that will be contained (displayed
and in-place edited) in AutoCad. I can easily do it in MFC
but would consider using .NET if it supported.
Thanks
|
|
|
|
|
Hi there, I'm developing graphic IDE and I would like to use some of the Visual .NET 2003 icons - for exmple I have a property pgrid and would like to have the same icon as in Visual (the hand), or toolbox (wrench and hammer). Do you know where I can find those?
|
|
|
|
|
They're probably available in some dll in Visual Studio. If you download beta 2 of VS 2005, you'll notice there's an icon library provided at the request of developers under the Visual Studio 8\Common 7\ directory, called VS2005ImageLibrary.zip, which contains all the icons used in VS.
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Horrific Minnesota Radio
Judah Himango
|
|
|
|
|
If you are using the .Net 2003 you will find it in the :
Common7\graphics . all are there.
|
|
|
|
|
In C++, I could read/write an object thus:
read ((void*) &myObject, 0, sizeof (Object)); //same for write
...rather than having to write a function that read/writes each individual field in the object. Can this be done in C#? My attempt gives me a problem with the &myObject: "can't take the address of a managed class."
If not, can this be done some other way?
If not . . . my alternate in C++ would be
istream& operator>> (istream& in, myClass& myObject)
{
return in >> field1 >> field2 >> ...;
}
The closest I can come in C# is
public void Read (Stream s)
{
s.ReadBool (field1);
field2.Read (s); //if field2 is of a class that I wrote
for (int i = 0; i < DimensionIHaveToRemember1; ++i)
for (int j = 0; j < DimensionIHaveToRemember2; ++j)
s.ReadInt (field3[i,j]);
...
}
(assuming
bool field1;
someClass field2;
int [,] field3; //OK, I'd have to do something special
//to make C++ do this easily, too...
...)
...and this seems needlessly messy. Is there a better way?
Thanks -- and if you want to refer me to a book, fine: C# and the .NET platform by Troelson is what I'm using, and it doesn't answer these questions, but maybe another does.
|
|
|
|
|
This is called serialization, and you can use a BinaryFormatter to serialize. Read Binary Serialization[^] in the .NET Framework SDK for more information and samples.
In C/C++ this works because of how memory is laid out, but in the managed world things are different. Reading data into a struct in native code can also be secure, while the Framework allows you to place constraints on your fields and properties (depending on what you serialize).
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Customer Product-lifecycle Experience
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Take a look at classes like MemoryStream and BinaryFormatter. The drawback to this is that you have to write out the class/fields with BinaryFormatter as well. If you need to put bytes directly into a field, you can use unsafe (you also have to set a compiler switch on the project to enable unsafe). Unsafe will allow you to get a pointer to the field.
Marc
MyXaml
Advanced Unit Testing
YAPO
|
|
|
|
|
Also, if you have to deal with binary formats coming from hardware and you want to put the data into a structure, look at StructLayout and the FieldOffset attribute.
Lastly, consider writing a C++ dll to handle that nastiness that returns happy managed data to the managed code.
Marc
MyXaml
Advanced Unit Testing
YAPO
|
|
|
|
|
Thanks, Heath, Marc! Serialization is exactly what I wanted.
I'm having this problem using it:
[Serializable]
public class Superclass ...
[Serializable]
public class Subclass: Superclass ...
{
[Nonserializable]
private Bitmap bigThing = bitmap ("bigfile.bmp");
}
...
//mySubclassObject already exists
IFormatter formatter = new BinaryFormatter();
mySubclassObject = (visualBoard) formatter.Deserialize(s);
So when I do this, I successfully load the parts I want . . . but the bitmap is of course not loaded, and mySubclassObject is not displayable. Before I start this chunk of code, it knows where the bitmap is.
I also tried moving this deserialization code into a method of Subclass, so I could then remind it where the bitmap is, but then I can't deserialize into "this" because "this" is read-only.
Thanks for all--
|
|
|
|
|