|
I have a form with device information for for each type of device. Most of the information is similar, so I'm sharing a single form, using SetDevice() to setup fields and controls specific to each derived device type. (right now, all SetDevice methods are inside the form, not the Device classes).
SelectedDevice is instantiated as a Device , then cast to whichever derived class is appropriate. The cast is necessary because at compile-time, SelectedDevice is just a Device , but at run-time will have a specific derived type.
|
|
|
|
|
I wonder whether or not dynamic s in C# 4 will do it.
|
|
|
|
|
PIEBALDconsult wrote: Or perhaps SetDevice should handle determining which type of device it was passed.
Wouldn't that mean creating a big SetDevice to handle all types, and just move the same code into SetDevice ? Even if the code is a little less ugly thanks to is .
Molly
|
|
|
|
|
Afaik, the 'is' keyword is not fully equal to x.GetType() == typeof(y) .
Let's say I have:
class A
class B:A (B inheriting from A)
Then following (pseudo-)code:
b = new B();
b is A returns true and
b = new B();
b.GetType() == typeof(A) returns false.
The latter option checks for an exact type match.
|
|
|
|
|
Paul89 wrote:
b = new B();
b.GetType() == typeof(A);
The above test the immediate types A.GetType() != B.GetType() && B.GetType() != A.GetType()
for a better understanding try this:
b = new B();
a = new A();
typeof(A).ToString();
typeof(B).toString();
a.GetType().toString();
b.GetType().toString();
However this works like the is keyword:
B.IsSubclassOf(A) returns true
-or-
A.IsAssignableFrom(B) returns true
On the is key word:
'An is expression evaluates to true if the provided expression is non-null, and the provided object can be cast to the provided type without causing an exception to be thrown.'
modified on Wednesday, December 30, 2009 2:42 PM
|
|
|
|
|
Thanks! I think is works for the code I've got today, but you probably saved me from myself on some future project.
Molly
|
|
|
|
|
Or maybe not!
|
|
|
|
|
Who goes 400 pages deep (as of today) into a forum just to rub it in?
|
|
|
|
|
That'd be me.
But I took a shortcut -- I saw on your profile that you had only posted a few questions, I checked those few.
I didn't actually know I'd find this.
|
|
|
|
|
|
Today, I tried to move the logic of my code from the SQLServer to my C# code.
The task is simple: Create a new HierarchyID element which is the first child of the current.
At the server side, the code is
DECLARE @User hierarchyid
SELECT @User = hierarchyid::Parse('/1/')
INSERT USERS (LOGIN,HID)
VALUES ('UserName', @User.GetDescendant(NULL, NULL)
O.K. Now I try to do the same logic in code like this:
SqlHierarchyId NewHID = new SqlHierarchyId();
NewHID = SqlHierarchyId.Parse("/1/");
NewHID = NewHID.GetDescendant(null,null);
but I receive an error
"Error 6 Argument '1': cannot convert from null to 'Microsoft.SqlServer.Types.SqlHierarchyId"
What I find in the Microsoft's documentation at http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.types.sqlhierarchyid.getdescendant(SQL.105).aspx[^]
sais that NULL is allowed.
Now what can I do in the case? Any Ideas?
p.s. I've designed a workaround meanwhile:
DECLARE @User hierarchyid
SELECT @User = hierarchyid::Parse('/1/')
SELECT @User.GetDescendant(NULL,NULL).ToString() as Child
...and I get a dataset filled with the value of the child. But this is stupid, isn't it?
modified on Wednesday, December 30, 2009 8:42 AM
|
|
|
|
|
I want to call to a function every time that a function is add to my Event.
How can i do it?
Example for this kind of code:
public delegate void DoItDelegate(int i);
public partial class Form1 : Form
{
public DoItDelegate MyFunction;
public int Count=0;
public Form1()
{
InitializeComponent();
RegisterTheNewMethod();
MyFunction += new DoItDelegate(DoItFunction);
MyFunction.Invoke(32);
}
public void DoItFunction(int i)
{
Count++;
int j = i;
}
public void RegisterTheNewMethod()
{
Count = 0;
}
}
Every time that this kind of command (MyFunction += new DoItDelegate(DoItFunction);)is been called and the function is been added to the Event MyFunction i want to invoke some function (In this example RegisterTheNewMethod()).
How can i do it?
|
|
|
|
|
Did you not read the answers to your earlier post? So why post the same again?
All those who believe in psycho kinesis, raise my hand.
|
|
|
|
|
hi, I am not sure how to call method every time you add to a delegate. Why don't you call the method after the delegate is added .
e.g
MyFunction += new DoItDelegate(DoItFunction);
RegisterTheNewMethod();
|
|
|
|
|
Hi,
i will be appreciated if someone could help me in this situation:
my PC: Pentium 2Core 2.1GHz 4Gb(Ram) windows 7 x64
The problem that I can not solve by myself is how to speed up calculations.
My method runs 850-970ms. method created for image processing (crops image ,recognize coped image and put calculated value into matrix)
When i am using threads (in this example 2 threads)
ThreadStart ts = new ThreadStart(ThreadOneP);
ThreadStart ts2 = new ThreadStart(ThreadTwoP);
Thread thread1 = new Thread(ts);
Thread thread2 = new Thread(ts2);
thread1.Start();
thread2.Start();
thread1.Join();
thread2.Join();
method takes 900-1070ms.
How to speed up that method will take about 400ms? it is possible?
P.S sorry for my English
|
|
|
|
|
Well, that's a bit vague without knowing what the threads are actually doing.
The bit of code you posted isn't really incorrect, although the calling thread is blocked and you could do some work on that rather than create a new one then just block on its completion.
To improve performance, you need to take a look at your algorithm and make sure it can't be improved. Splitting the task onto more threads might be an idea as well so that machines with more processors can make use of them.
Perhaps try increases the thread priority of the workers?
Regards,
Rob Philpott.
|
|
|
|
|
Also I just now try to set 7 thread 1 thread for each image row. but time consumption is the same as Using 2 threads 900-1000ms.
can you tell a little bit mote how to use thread priority, because i thing i i will set high priority for all threads there will be no effect.
|
|
|
|
|
Hi,
adding threads can improve latency time by doing some things in parallel; it only is effective if:
1. (some of the) CPUs aren't fully loaded (watch Task Manager, performance) to start with.
2. synchronization cost is low
with only two active threads (the launching thread is idle as it waits for the others to join) a dual-core should work well, independent of thread priorities.
you are not allowed to directly touch WinForm Controls from inside your threads!
I suggest you show us the actual code for more feedback.
|
|
|
|
|
(how code looks after trying with 7 threads (because captured image is divided in 7 rows)) I have to warn you CODE is not optimized. as example I show you only 2 thread methods: ThreadOneP and ThreadTwoP
public int TransferToMatrixThread()
{
temptest = 0;
CabX = CabX - 18;
CabY = CabY + 53;
CabXX = CabX - 484; //POSSITION
CabYY = CabY + 20;
for (int i = 0; i < 11; i++) //FILLS MATRIX WITH 0
{
for (int j = 0; j < 11; j++)
{
transfered[i, j] = 0;
}
}
ThreadStart ts = new ThreadStart(ThreadOneP);
ThreadStart ts2 = new ThreadStart(ThreadTwoP);
ThreadStart ts3 = new ThreadStart(ThreadThreeP);
ThreadStart ts4 = new ThreadStart(ThreadFourP);
ThreadStart ts5 = new ThreadStart(ThreadFiveP);
ThreadStart ts6 = new ThreadStart(ThreadSixP);
ThreadStart ts7 = new ThreadStart(ThreadSevenP);
Thread thread1 = new Thread(ts);
Thread thread2 = new Thread(ts2);
Thread thread3 = new Thread(ts3);
Thread thread4 = new Thread(ts4);
Thread thread5 = new Thread(ts5);
Thread thread6 = new Thread(ts6);
Thread thread7 = new Thread(ts7);
DateTime startTime = DateTime.Now; //TIME
thread1.Start();
thread2.Start();
thread3.Start();
thread4.Start();
thread5.Start();
thread6.Start();
thread7.Start();
thread1.Join();
thread2.Join();
thread3.Join();
thread4.Join();
thread5.Join();
thread6.Join();
thread7.Join();
DateTime stopTime = DateTime.Now;
TimeSpan duration = stopTime - startTime;
listBox1.Items.Add("7 THREAD = " + duration);
TransfX = currX + CabXX + 9; // norima nustatyti i pav viduri todel pridedamos konstantos
TransfY = currY + CabYY + 11; // norima nustatyti i pav viduri todel pridedamos konstantos
return temptest;
}
public void ThreadOneP()
{
int o = CabYY;
int zy = 8;
int zx = 2;
for (int ok = CabXX; ok < CabXX + 490; ok = ok + 72)
{
var color22 = RGBreturnLVL(ok, o, 30, 30, bmpScreenshot51);
if (maze[0, 1] < color22.R && color22.R < maze[0, 2] && maze[0, 3] < color22.G && color22.G < maze[0, 4] && maze[0, 5] < color22.B && color22.B < maze[0, 6])
{ transfered[zy, zx] = maze[0, 0]; }
else if (maze[3, 1] < color22.R && color22.R < maze[3, 2] && maze[3, 3] < color22.G && color22.G < maze[3, 4] && maze[3, 5] < color22.B && color22.B < maze[3, 6])
{ transfered[zy, zx] = maze[3, 0]; }
else { transfered[zy, zx] = 0; temptest++; }
zx++;
}
}
public void ThreadTwoP()
{
int o = CabYY - 72;
int zy = 7;
int zx = 2;
for (int ok = CabXX; ok < CabXX + 490; ok = ok + 72)
{
var color22 = RGBreturnLVL(ok, o, 30, 30, bmpScreenshot61);
if (maze[0, 1] < color22.R && color22.R < maze[0, 2] && maze[0, 3] < color22.G && color22.G < maze[0, 4] && maze[0, 5] < color22.B && color22.B < maze[0, 6])
{ transfered[zy, zx] = maze[0, 0]; }
else if (maze[3, 1] < color22.R && color22.R < maze[3, 2] && maze[3, 3] < color22.G && color22.G < maze[3, 4] && maze[3, 5] < color22.B && color22.B < maze[3, 6])
{ transfered[zy, zx] = maze[3, 0]; }
else { transfered[zy, zx] = 0; temptest++; }
zx++;
}
}
waiting your response
|
|
|
|
|
Some comments:
1.
please show code in PRE tags (e.g. use "code block" widget) for better readability
2.
your threads share the transfered[] array, i.e. they each fill one row. For performance, I would avoid 2-D arrays, give them each a 1-D array.
3.
you are abusing the var keyword; if color22 is a Color, then declare it a Color (for readability).
4.
as there are only a few iterations in the for loop, most time is spent inside RGBreturnLVL(). So one may want to see that code too.
5.
if threads do almost identical things, they don't need separate code; make a simple class that describes one threaded job, holding its code and data.
6.
if is not running on the main thread (the name seems to indicate so), then listBox1.Items.Add("7 THREAD = " + duration); is NOT allowed. See this article.[^]
7.
assuming there is no blocking system call (e.g. a file/network read) inside your threaded code, it does not make sense to have more calculation threads than there are CPUs. Threading overhead can make it slower.
8.
seems like maze[] is pretty constant, and only a few elements are needed here; I would copy them in local variables once, and use those all the time.
|
|
|
|
|
Thanks I will look into your comments, change code and then paste
|
|
|
|
|
AND, YES most of time is spend in RGBreturnLVL(...) method.
//Calculate average 30x30 image color
private Color RGBreturnLVL(int PositionX, int PositionY, int width, int height, Bitmap Image)
{
int ra = 0;
int ga = 0;
int ba = 0;
int total = 0;
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Color clr = CropBitmap(Image, PositionX, PositionY, width, height).GetPixel(x, y);
ra += clr.R;
ga += clr.G;
ba += clr.B;
total++;
}
}
ra /= total;
ga /= total;
ba /= total;
return Color.FromArgb(ra, ga, ba);
}
|
|
|
|
|
I asked you to use PRE tags.
and that code refers to yet another method.
and doesn't look very efficient.
|
|
|
|
|
GetPixel is a performance disaster, you should really use LockBits and unsafe code (that's what GetPixel does, too, but it does it every time you call it)
Using LockBits once and then using unsafe code to read the pixels is several orders of magnitude faster than using GetPixel
|
|
|
|
|
CropBitmap creates a copy of some sub-range of the bitmap?
If so, you are creating one copy for each pixel you read! That looks like an incredible waste of time. Move that CropBitmap call out of your loop.
Or try to get rid of it entirely; use Image.GetPixel(x + PositionX, y + PositionY) instead.
|
|
|
|
|