|
You made an array "range" but didn't initialize its contents. It will only contain zero.
That array isn't necessary in the first place, you could just iterate over the range directly:
for (int number = firstNum; number <= lastNum; number++)
{
if (number % 2 == 0)
; print it or whatever
}
And you don't actually need that test to see if it's even, either, because you can make sure to only ever generate the even numbers:
for (int number = firstNum + (firstNum & 1); number <= lastNum; number += 2)
{
}
Of course this part: firstNum + (firstNum & 1) is something slightly special, it's rounding up to the next multiple of 2.
firstNum & 1 takes the lowest bit, which will be 1 if odd and 0 if even. Adding that back to the original number will not change it when it's even, but make it one higher if it's odd.
|
|
|
|
|
I have the following statement in a C# 2008 windows application that was converted to a console application:
finally
{
Console.ReadLine();
Environment.Exit(1);
}
<pre>
The error message that is displayed says there is not enough io memory.
Can tell show me in code and/or point me to a reference that will show me how to solve this problem?
|
|
|
|
|
Quote: The error message that is displayed says there is not enough io memory
The code you pasted here has no relation to the error message. It might be other part of your code creating this error. debug your application and run line by line and see on executing which method this error popsup.
also copy the complete exception stack here so that we can able to tell something from that.
|
|
|
|
|
Change the exit code: 0 indicates success, any other number is an error code. Exactly how the error code is interpreted depends on the environment the application is run under - I suspect that your app returning "1" is being interpreted as an I/O error.
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
|
|
|
|
|
The program that is executed is called by a C# 2010 windows application (called app1) that was converted from a console application.
The following is an example of some of the calls made from app1 to the C# 2008 application that is having the memory problem called app2.
string[] SubPkgIDs = rData.details.Where(c => c.Package_ID.StartsWith("SUB").Select(c => c.PackID).Distinct().ToArray();
foreach (string SubPkgID in SubPkgIDs)
{
{
Process eProcess = new Process();
String Process_Arguments = null;
eProcess.StartInfo.UseShellExecute = false;
e_Process.StartInfo.FileName = "app2.exe";
Process_Arguments = " 3 " + SubPkgID;
eProcess.StartInfo.Arguments = Process_Arguments;
eProcess.Start();
eProcess.WaitForExit(1800);
eProcess.Dispose();
Process_Arguments = null;
}
}
I think the line of code that is causing the memory problem possibly is the following:
eProcess.WaitForExit(1800);
App1 is not waiting for a response basically from app2.exe program that is being called. It waits the 1800 *.secs and makes
the next calls. There is no reason for app1 to wait or app2 to finish executing since it does not need a response from app2.
However if this is causing the memory problem I can wait for the app2 program to finish executing.
App1 and app2 were setup to be single threaded.
In the app1 program that calls the second program called app2, I am thinking of putting the following code at the end of the program:
foreach (Process proc in Process.GetProcessesByName("app2"))
{
proc.Kill();
}
This way the app2 processes that are still in memory can be released.
Thus based upon what I mentioned previously and what I just mentioned above, can you show me code and/or tell me what you would do to solve the memory problem I am having?
|
|
|
|
|
You have two processes. It isn't clear which one is running out of memory but because they are different processes they are different and neither would impact the other UNLESS there is something odd with the environment setup.
If there is something odd in the environment they you should at least track that down and understand it first. It would have to do with a system and\or user limit on the maximum memory possible. You can't figure out the environment problem by looking at the code you have.
Otherwise if the following are all true
- App1, the one that uses Process, is single threaded (not just assumed but actually is.)
- App1 is the one running out of memory
Then something in App1 before you make the Process call is using a lot of memory.
If it is App2 then either App2, not App1, has a bug or one of the options you are passing is wrong.
|
|
|
|
|
I have the following addtional questions to ask you since I am the only programmer at my small company, I have have 1 year of experience, and I have never come across memory issues in the past"
1. Based upon your statement, "It isn't clear which one is running out of memory but because they are different processes they are different and neither would impact the other UNLESS there is something odd with the environment setup.". Can you tell me what would be an 'odd' setup in my environment?
2.If there is something 'odd' setup in the environment, can you tell me how to tell what is the system and/or user limit on the maximum memory possible? Is there a tool that I can use, a process that I can look at? Is there a reference I can use to know how to set set the maximum limits?
3. If app1 is using alot of memory before it makes the call, can you tell me how to check on the memory that app1 is using?
|
|
|
|
|
dcof wrote: Can you tell me what would be an 'odd' setup in my environment?
Someone messed up the virtual memory setup (they would need to do this explicitly) or it is being run in a situation, such as a hosted environment, that explicitly limits the memory.
dcof wrote: If app1 is using alot of memory before it makes the call, can you tell me how to check on the memory that app1 is using?
Pretty sure there is a C# api that tells you that. Google for it.
|
|
|
|
|
I'm trying to use the child class method instead of the parent class for some child class cases. For some reason, I get to the parent class method, but it never gets to the override in the child. Any ideas why? I've been referring to http://stackoverflow.com/questions/9067885/calling-child-class-method-from-parent[^]
but it's not getting to the child's method.
In my PC.cs of the child class (Ko):
public partial class PC : GenericPC
{
public override void ValidateDynData(TextBox[] tb_dynData_Array, ref int result)
{
Console.WriteLine("Ko PC::ValidateDynData");
}
}
In my GenericPC.cs of the parent class:
private int processDynData()
{
ValidateDynData(itsEnteredDynData, ref returnCode);
}
public virtual void ValidateDynData(TextBox[] tb_dynData_Array, ref int result)
{
Console.WriteLine("Generic::ValidateDynData passing off to child to validate special data");
}
Any ideas why it's not going to the child class implementation of ValidateDynData when I call ValidateDynData in my parent class? This is the only area in my code where I am trying to have a child class override a parent implementation, so I'm not sure if I'm doing something wrong?
|
|
|
|
|
I tried it myself, by pasting your code into an app, and adding a few bits to prevent compilation errors:
private void button2_Click(object sender, EventArgs e)
{
PC pc = new PC();
int i = 4;
pc.ValidateDynData(null, ref i);
}
public partial class PC : GenericPC
{
public override void ValidateDynData(TextBox[] tb_dynData_Array, ref int result)
{
Console.WriteLine("Ko PC::ValidateDynData");
}
}
public class GenericPC
{
TextBox[] itsEnteredDynData;
int returnCode;
private int processDynData()
{
ValidateDynData(itsEnteredDynData, ref returnCode);
return returnCode;
}
public virtual void ValidateDynData(TextBox[] tb_dynData_Array, ref int result)
{
Console.WriteLine("Generic::ValidateDynData passing off to child to validate special data");
}
} Ignoring the "not assigned" warning message, when I press the button I get:
Ko PC::ValidateDynData Which is what I would expect - the overloaded version.
Could it be that you are using an instance declared as the base class, rather than the derived?
GenericPC gpc = new GenericPC();
gpc.ValidateDynData(null, ref i);
would give you
Generic::ValidateDynData passing off to child to validate special data
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
|
|
|
|
|
It's possible that's the problem. My object instance is created with Reflection:
_assembly = Assembly.LoadFrom(programDll);
_type = _assembly.GetType("CrWriter.PC");
_objectInstance = Activator.CreateInstance(_type);
_parameters = new Object[] { cb1, programDll, templateArr, itsDll, cert, i, cb1.cbInfos[i].boxID };
controls[i] = new Control();
controls[i] = (Control)_type.InvokeMember("GetPC", BindingFlags.Default | BindingFlags.InvokeMethod, null, _objectInstance, _parameters);
controls[i].Dock = DockStyle.None;
this.Controls.Add(controls[i]);
Since my Invocation of GetPC lands in the child class and not the parent, it must be created correctly. So I'm not sure why it's not landing in the correct ValidateDynData method. Maybe I need to cast my object to the programDll somehow. When I run the program and inspect the _objectInstance it could be a problem:
variable.............................................................value
base: {GenericCrWriter.GenericPC} ......CrWriter.PC
baseInst: .........................................................GenericCrWriter.GenericPC
But then, the _assembly is referring to Ko/PC and not Generic/GenericPC.
Also, my _assembly.GetType looks good. My Generic/parent doesn't have anything named CrWriter.PC
I wonder if there is a way to cast an object obtained through reflection?
modified 1-Mar-13 15:56pm.
|
|
|
|
|
MichCl wrote: Since my Invocation of GetPC lands in the child class and not the parent, it must be created correctly. So I'm not sure why it's not landing in the correct ValidateDynData method.
Presuming the former is really true then the possibilities are.
- The child signature of ValidateDynData is wrong
- You are not running the code that you think you are (binary does not match source.)
|
|
|
|
|
Do you see any problems with my method signatures?
This is the parent, GenericPC.cs:
public virtual void ValidateDynData(TextBox[] tb_dynData_Array, ref int result)
{
Console.WriteLine("Generic::ValidateDynData passing off to child to validate special dynamic data");
}
This is the child, PC.cs:
public override void ValidateDynData(TextBox[] tb_dynData_Array, ref int result)
{
Console.WriteLine("Ko PC::ValidateDynData");
}
The parent, GenericPC.cs calls the method this way:
ValidateDynData(itsEnteredDynamicData, ref returnCode);
I checked the correct version of Generic.dll is referenced in the child's project/class. I did a clean build in the child class. Any other binaries that should be checked?
|
|
|
|
|
I've been looking into this some more, and I think the reason flow in the parent is not seeing the overridden ValidateDynData in the child is because of the way I get flow into the parent from the child class:
Is there another way to get to the parent's method without creating an instance of the parent?
GenericPC baseInst = new GenericPC();
return baseInst.GetPC(cbInst, dllSel, templ, dll, cert0, slaveIndex, BoxID);
|
|
|
|
|
Is it possible to pass parameters from unmanaged code to managed code by reference?
I am programming agents for the Metta Trader 4 (MT4) platform using MetaQuotes Language 4 (MQL4); a C like language.
Normally one would use C++ to generate DLLs but I would like to use C# if I could because when you release something in C++ it is frozen in time as opposed to when you release something in C#. With C# if problems are detected in the underlieing classes they are corrected by normal updates of the .NET platform. This is an advantage I would like to take advantage of to simplify maintenance of products I hope to sell.
I will need to pass large arrays of market data to these DLLs for analysis and would like to do this by reference.
Here is an example of how to pass an array by value:
[DllExport(EntryPoint = "SumIntegerArray")]
public static int Sum([MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)]int[] values, int count)
{
int result = 0;
for (int i = 0; i < count; i++)
result += values[i];
return result;
}
How would I pass the array by reference to the DLL?
|
|
|
|
|
You could pass a pointer and use unsafe on the C# end instead of arrays. AFAIK you can't (easily) pretend that a native array is a managed array.
|
|
|
|
|
Thanks for the information, I will try it.
I am just unsure how to accomplish it in C#. I didn't know you could use a pointer in managed code.
Off to the articles to see if I can find out how.
|
|
|
|
|
It mostly works as you'd expect, same syntax and all, but you can't take the address of managed objects and you need "unsafe" keywords (and enable unsafe code in the build options).
Also, "int *x, y" makes both of them pointers, not just x.
|
|
|
|
|
Thanks for the information.
|
|
|
|
|
As an alternative, you could put it in a memory mapped file[^]
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I don't think it would be applicable in my case but I will look into them.
Thanks for the information.
|
|
|
|
|
I want to get the cursor coordinates when the mouse is clicked in inkcanvas.here i used mouse event handler and i am trying to display cordinates in a textbox but somehow its not working.i have used the following code snippet.can someone please help
private void inkCanvas1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
System.Windows.Point position = e.GetPosition(this);
this.textBox1.Text = position.X.ToString()+","+position.Y.ToString();
}
|
|
|
|
|
I am assuming its winforms
but have a look a
Cursor.Position.X
Further reading System.Windows.Forms.Cursort.Position[^]
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
|
|
|
|
|
|