473,385 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Inheriting from array of System.Diagnostics.Process[]

I'm trying to create a ListBox that holds list of currently running
processes. So I inherited from Process a classes named _Process, which
it's objects are to be held in the ListBox (I just overrode the
"ToString"). Using the "GetProcesses" method, I get Process[], that I
want to cast to _Process[]. So why do I get
"System.InvalidCastException" all the time ? ("Specified cast is not
valid")
using System.Diagnostics;
using System.Windows.Forms;
..
..
..
namespace dbg
{
public class _Process: Process
{
public override String ToString()
{
return this.ProcessName;
}
};
..
..
..
private void InitializeComponent() // of the ListBox
{
Sep 30 '06 #1
7 4736


"Gamma" <Ta******@gmail.comwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...
I'm trying to create a ListBox that holds list of currently running
processes. So I inherited from Process a classes named _Process, which
it's objects are to be held in the ListBox (I just overrode the
"ToString"). Using the "GetProcesses" method, I get Process[], that I
want to cast to _Process[]. So why do I get
"System.InvalidCastException" all the time ? ("Specified cast is not
valid")

I think you're using the wrong OO techniques here. Why are you inheriting
from Process? Do you have a different kind of process? Is your type a
Process? No. Perhaps it's a wrapper for a Process, or has a list of
Process.

David

Sep 30 '06 #2

"Gamma" <Ta******@gmail.comwrote:
I'm trying to create a ListBox that holds list of currently running
processes. So I inherited from Process a classes named _Process, which
it's objects are to be held in the ListBox (I just overrode the
"ToString"). Using the "GetProcesses" method, I get Process[], that I
want to cast to _Process[].
You're pretty close - here's the code I got working:
(I'm assuming you have a Form with "ListBox1" on it...)
private void button1_Click(object sender, EventArgs e)
{
// Get the list of processes
Process[] p = Process.GetProcesses();

// Convert that array to our array. We overload ToString() so that the
// ListBox can actually do soemthing meaninfull.
ProcessWrapper []pw = Array.ConvertAll<Process, ProcessWrapper>
(p, new Converter<Process, ProcessWrapper>(ProcessToWrapper));

// Create a new collection of items for the list box.
ListBox.ObjectCollection o = new ListBox.ObjectCollection(listBox1);

// Add in all our processes.
o.AddRange(pw);
}

// This does the conversion
private ProcessWrapper ProcessToWrapper(Process p)
{
return new ProcessWrapper(p);
}

public class ProcessWrapper
{
private Process _process;
public ProcessWrapper(Process p)
{
_process = p;
}
public override string ToString()
{
return _process.ProcessName;
}
}

--
Chris Mullins, MCSD.NET, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins
Sep 30 '06 #3
Thank you very much for your quick responses. I would like to
understand what did I do wrong, technically. Why am I not able to
inherit a new class from Process ? (although it may not be correct in
terms of OO).

btw - The exception is raised also when I cast a single _Process
instance from Process (not an array!).

Oct 3 '06 #4
You really didn't do too much wrong from an OO perspective.

Your "MyProcess" class, which inherits from "Process" still passes the "IS
A" test that really is what defines inheritence.

Unfortuantly, there's no way in .NET (or any OO language, that I know of) to
take an existing collection of Processes and up-convert it to your more
specialized class.

For this to have worked, the GetAllProcesses() method would have needed to
take another parameter - one that told it the type it needs to create. This
way it could create "MyProcess" entries instead of "Process" entries.

The solution I posted used aggrigation - I wrote a wrapper (not an
extension, but a wrapper - my wrapper does NOT pass the "Is A" test) that
was able to be constructed from a process. I converted all the "Process"
classes to "ProcessWrappers" then bound that wrapper to the list box.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins

"Gamma" <Ta******@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
Thank you very much for your quick responses. I would like to
understand what did I do wrong, technically. Why am I not able to
inherit a new class from Process ? (although it may not be correct in
terms of OO).

btw - The exception is raised also when I cast a single _Process
instance from Process (not an array!).

Oct 3 '06 #5
Once again, thank you for your quick response :-)

I still don't understand - once I have received the Process[] array, I
had an array of Process(es). Theoretically, I should have been able to
cast even a single object from the array (let's say Process[0]) to a
single _Process object. From some reason, it raised an exception. To be
honest, I'm pretty sure it's a simple thing, like me forgetting to
override some method or something of that kind.

A simple cast, like:

Process p = new Process;
_Process _p;
_p = (_Process) p; // Exception !

raised an exception. That's what I don't understand - what should I do
in order for that example to succeed ?

(Once I manage to cast a single object, I pass it through "foreach" and
the rest is history...)

Oct 5 '06 #6
"Gamma" <Ta******@gmail.comwrote in message
A simple cast, like:

Process p = new Process;
_Process _p;
_p = (_Process) p; // Exception !
I'm assuming:
class _Process inherits Process

The problem is that you're backwards. The "Is a" test jumps up:
Because _Process inherits from Process, you can safely say that "_Process IS
A Process". This is polymorphism.

.... BUT (and this is a big but) you CANNOT say that "Process IS A _Process".

Let's extend the _Process class to do something interesting:
public class _Process : Process
{
public void CreateRemoteMainframeProcess(){...}
}

Now what? Your original Process has no definition for
"CreateRemoteMainframeProces", this method was added by the derived _Process
Class. So now, more clearly, "Process" IS NOT "_Process" because it has no
way to create a mainframe process.

I don't think my explination is very good. Hrm.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins
Oct 6 '06 #7
O.k, that makes sense (and your explanation was perfect), and a bit
embarrassing for not noticing it myself. So I corrected it and tried
again:

Process p = new Process;
Process _p;
_p = (_Process) p; // Exception !

Since I didn't add new propeties, but methods only, I can't see what's
wrong in the rectified segment above. In fact, I'm guessing that the
part that raises the exception is nothing but the cast:

(_Process) p

I miss C++ so much... :-s

Oct 9 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: marccruz | last post by:
Given an instance of System.Diagnostics.Process, how can I get the parent process o Given an instance of System.Diagnostics.Process, how can I get the child processes For example, I start a...
1
by: john conwell | last post by:
I've got a process engine written in .Net. the user writes a data file and saves it as file type ".xyz". I have a file assiciation set up in the database that when a .xyz file is invoked (double...
2
by: D | last post by:
I have an array of processes declared like so System.Diagnostics.Process _ProcessArray = new System.Diagnostics.Process; and the code below shows how I iterate thru the array and looking for a...
1
by: solex | last post by:
Hello All, Hopefully someone has run into this error. I have written a class(source below) that launches a thread to monitor the StandardOutput of a System.Diagnostics.Process, in particular I...
11
by: Nurit N | last post by:
This is the third newsgroup that I'm posting my problem. I'm sorry for the multiple posts but the matter becoming urgent. I hope this is the right place for it... I have created a very...
0
by: Daniel | last post by:
System.Diagnostics.Process.Start fails on windows server 2003 the process returns process.ExitCode == 0 but executing any process with System.Diagnostics.Process.Start on windows xp works fine....
0
by: Colin Williams | last post by:
I am using the code below to map network drive and then fire up an app in a sub dir of that drive. However when using the file open dialog from that app, drive K: appears just as Network drive K:...
3
by: garyusenet | last post by:
Hello everyone I hope you are having a good evening. This evening I made a method that allows me to find the process id's of running processes of a given 'friendly name' - using...
2
by: test3 | last post by:
Hello folks, I'm using System.Diagnostics.Process to start a thirdparty program (that works perfectly when started via command line). I'm using Process.StandardOutput to get the output of the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.