|
I did google on this. The only thing I got is that you can get the type and member info of a class. I couldn't find any way to declar the object of a class at run time.
If you know the code, please write it.
|
|
|
|
|
You can use either Activator.CreateInstance() or Assembly.CreateInstance()
|
|
|
|
|
You can do this using reflection.
Here's an example using .NET 1.1.
Assembly assembly = Assembly.GetExecutingAssembly();
string continentFactory = ConfigurationSettings.AppSettings["ContinentFactory"];
Binder binder = null;
Type continentType = assembly.GetType(continentFactory);
object[] args = null;
ContinentFactory continentInstance = (ContinentFactory) continentType.InvokeMember(
null,
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.CreateInstance, binder, null, args
);
string animalWorld = "PatternsTest.AnimalWorld";
Type worldType = assembly.GetType(animalWorld);
args = new object[] { continentInstance };
AnimalWorld worldInstance = (AnimalWorld) worldType.InvokeMember(
null,
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.CreateInstance, binder, null, args
);
worldInstance.RunFoodChain();
);
Kevin
|
|
|
|
|
Hello all,
I have a background thread that updates some userControls. To do this updates, I call invoke on the control, from the background thread, after testing for invokeRequired. No problem so far...
The problem begun when I tried doing this updates before the userControl has been displayed (like when it is on a hidden tagpage). In this case, the control does not have a handle yet, and invokeRequired returns false, causing my logic to make the backgroud thread to update the control directly. There´s no need to say what happened to my application next...
Then, to avoid this, I have begun to test is the userControl has a Handle, and if it hasn´t, I do control.CreateControl(). After this, it seemed like I could update the control using invoke properly. But now my application is freezing sometimes, like what happens when another thread tries to update a control without calling invoke.
How can I solve this? I realy need that all controls are created when this backgroud thread begins its job...
Thanks
Peterson
|
|
|
|
|
When do you start the background thread?
You shouldn't be seeing controls without handles. The only time that occurs is in the constructor. Once the constructor returns all the controls should be completely created.
You could always go through all your controls and call CreateControl() in your constructor, just to make sure the controls are all created. That way you're not making that call from another thread.
CreateControl() is a thread-bound operation and should only ever be called on the GUI thread.
|
|
|
|
|
Peterson Luiz wrote: The problem begun when I tried doing this updates before the userControl has been displayed (like when it is on a hidden tagpage).
There is a hack to get this to work. Just make sure you refer to the control's Handle property somewhere (in the correct thread, of course). The getter for Handle creates a handle if it already hasn't been created and it all works fine thereafter. I usually put it right after InitializeComponent.
class MyForm : Form
{
MyControl control;
public MyForm()
{
InitializeComponent();
IntPtr val = control.Handle;
Console.WriteLine(val.ToString());
}
}
Note that it's a hack and it relies on an implementation detail which might change in the future.
|
|
|
|
|
>I have a List<string> of IP Addresses in _check.GroupData. I iterate through the list, pinging, asynchronously, each machine. I've stripped out the vast majority of the code, which is why the stuff below looks functionally useless, but it shows the code I am running which leaks like a seive.
Monitoring with PerfMon you can see the private bytes for the process go sky high, and never come back.
Any ideas?
#using System.Net.NetworkInformation;
override public void Check()
{
foreach (string ipAddress in _check.GroupData)
{
Ping tmpPing = new Ping();
try
{
System.Threading.Interlocked.Increment(ref _pingsOutstanding);
tmpPing.PingCompleted += new PingCompletedEventHandler(ping_PingCompleted);
tmpPing.SendAsync(ipAddress , 10000 , ipAddress);
}
catch (Exception e)
{
}
}
while (_pingsOutstanding > 0)
{
System.Threading.Thread.Sleep(1000);
}
}
void ping_PingCompleted(object sender, PingCompletedEventArgs e)
{
System.Threading.Interlocked.Decrement(ref _pingsOutstanding);
(sender as Ping).Dispose();
return;
}
sorry about the layout - pushed for time fixing other stuff!
Cheers
Malc
"More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF
|
|
|
|
|
It is possible that garbage collection just hasn't decided to reclaim the memory. Just for fun you could try GC.Collect at the end of your processing to force garbage collection. That might free up all the memory. If it does then you probably don't have a memory link.
My only other thought is the way you are using the Ping variable inside the loop. If you had a variable out side of your method, perhaps a generic list. Add each of the ping objects to it. Then when all your threads are done loop through your generic list and free the ping objects.
Hope that helps.
Ben
|
|
|
|
|
You won't believe me, but the container of 'pings' was going to be my next step. I'll try the GC.Collect though - stupidly, I didn't realise you could call the GC explicitly. Thanks for that.
kubben wrote: Just for fun you
- is it not good practice then to call GC.Collect?
"More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF
|
|
|
|
|
Malcolm Smart wrote: - is it not good practice then to call GC.Collect?
It's generally not, but for testing purposes doing it will let you see if the GC simply hasn't gotten around to releasing the memory, or if there's an actual leak.
--
You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer
|
|
|
|
|
Well, Microsoft says that Garbage collection does take up resources time etc. So in general the discourage its use mainly for performance reasons. If they really didn't want us to use it they wouldn't have exposed it.
My experience with it is, it can seem that you might have a memory link, but for some reason garbage collection has just not decided to run. Anyway, if you run it manually you will know if you have a memory link or not.
Ben
|
|
|
|
|
Garbage collecting made no difference. I'm going to try and run the pings synchronously now - will take an age as we have so many machines in the farm, but it will eliminate the asynch aspect of the call.
Watch this space....
"More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF
|
|
|
|
|
I think if you had a generic list out side of the method you could save and then dispose of the ping objects. I think because you define the ping object in the method it is defined as a local variable. So then when you try to free it in the callback it doesn't work. Just my guess looking at the code.
Ben
|
|
|
|
|
You are not disposing of the Ping object if it throws an exception
|
|
|
|
|
I am, but not in the version of the code posted - honest!!!! I cut that down to its bare bones....but....the farm I am pingnig has about 1700 machines, and only 2 causeP Ping to through. The apparent memory leak is proportional to the number of machines pinged.
I'll post back my 'GC.Collect' resulst from the above post shortly.
Thanks.
"More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF
|
|
|
|
|
O ok
|
|
|
|
|
It seems the ASync call of Ping doesn't like havnig hundreds of them kicking about. I changed the code to call the ping synchronously, but placed the call in a worker thread, effectively giving me an asynchronous call. Works a treat..
class pinger
{
public static int PingsOutstanding = 0;
public string IP;
public void PingIt()
{
try
{
System.Threading.Interlocked.Increment(ref PingsOutstanding);
Ping ping = new Ping();
ping.Send(IP, 100);
}
catch (Exception e)
{
}
finally
{
System.Threading.Interlocked.Decrement(ref PingsOutstanding);
}
}
}
foreach (string ipAddress in _check.GroupData)
{
pinger p = new pinger();
p.IP = str;
new System.Threading.Thread(new System.Threading.ThreadStart(p.PingIt)).Start();
}
while (pinger.PingsOutstanding > 0)
{
System.Threading.Thread.Sleep(500);
}
This works, perfmon doesn't even blink about the memory being used, and it seems just as quick, if not quicker...happy for destructive criticism on this method!
Thanks all for the help.
"More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF
|
|
|
|
|
Malcolm Smart wrote: It seems the ASync call of Ping doesn't like havnig hundreds of them kicking about.
No it doesn't. You'd have to convert the code to limit the number of threads you have running pings. Perhaps queuing up the list of IP's to hit and having 10-20 or so ping threads feeding off that queue...
|
|
|
|
|
Hi, I'm a Java Developer wishing to Migrate to C#. Please provide me some ebook meant for Java Developers whiching to migrate to C#.
Thanks...
P.S: I'm new around here so please move my post to the appropriate section if it does not belong here.
|
|
|
|
|
Java and C# are very similar. I don't know of any ebooks, but I know there are a couple of books available for purchase. Overall, I'd say if you found some articles online ( just google it ), and then you started to play with C#, you wouldn't have much in the way of problems.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Like Christian said, Java and C# are quite similar. I learned Java at the university and C# on my own (and with help of Code Project articles ). There isn't a real sharp learning curve. Only difference is the most of the classes you find in .NET versus Java. There are many that are similar. Have fun learning C# and you did pick the right forum for asking C# questions.
There are many very knowledgeable members on this site, so feel free to drop a question anytime
Paul
"Any sort of work in VB6 is bound to provide several WTF moments." - Christian Graus
|
|
|
|
|
http://msdn2.microsoft.com/en-us/library/ms228358(VS.80).aspx[^]
This should get you started!
P.S. -- My first experience with both C# and Java was to port a project from Java to C#! I found most of my job was to run find/replace to replace methods like String.length to String.Length etc... The biggest bug that I created while doing this was that String.substring in Java has the same parameters as String.Substring in C#, but they do different things (index to index vs index to length).
Good luck!
|
|
|
|
|
Thanks Phil. Thats Quite helpfull...
|
|
|
|
|
Look at the Java Language Conversion Assistant (google for JLCA 3)
|
|
|
|
|
Hi frinds,
First of all thanks, I got the code to Convert CSV file to XLS file using Excel Application object.
My code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using Microsoft.Office.Interop.Excel;
namespace File_Conversion
{
class Program
{
static void Main(string[] args)
{
ApplicationClass app = new ApplicationClass();
Workbook doc = app.Workbooks._Open(
@"C:\GCProject\Input Example.csv",
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value,
false,
Missing.Value,
Missing.Value,
false);
Console.WriteLine("Reading CSV File........");
doc.SaveAs(
@"C:\GCProject\Output.xls",
XlFileFormat.xlWorkbookNormal,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value,
XlSaveAsAccessMode.xlExclusive,
Missing.Value,
false,
Missing.Value,
Missing.Value,
Missing.Value);
doc.Saved = true;
Console.WriteLine("Converted CSV to XLS file");
app.Quit();
Console.ReadLine();
}
}
}
But Now, I have to get specified format, Like follows.
My CSV file Contains Data like.
-----------------------------------
Ex: DriverID | CustomerID | Ship to ID | Load # | BOL Date/TimeStamp |PrdNam
520 | 2355 - | 1 | 61953 | ############ | 1 Eth 87
My XLS file Contains Data Like.
-----------------------------------------------------------------------------
Ex: Field | Definition | Length | Starting Column | Ending Column |
-----------------------------------------------------------------------------
delimiter"
-----------------------------------------------------------------------------
TransType|Some Definition |1 | 1 | 1
|
-----------------------------------------------------------------------------
delimiter ","
-----------------------------------------------------------------------------
Termin ID | Some Difinition| 3 | 3 | 5
|
-----------------------------------------------------------------------------
delimiter ","
-----------------------------------------------------------------------------
Term SPLC | Some Difi | 7 | 6 | 12
|
-----------------------------------------------------------------------------
I got the code converting CSV file to XLS file. But What I need is Converted
XLS file should look like above format in Converted Excel sheet.
Any body have Idea !!!!
Hope I explained well.!
Thanks in advance.
Regards,
Liyakhat.
|
|
|
|