How To Multi Processing ?

A

Ayaz Tariq

Hellow Friends ... Here what i m working for ... i want to show
Multiprocessing by creating about three processes and then showing there
states that which process is now in running state and which one is in
wait state and which on has successfully completed its working ... i
hope i can get the desired help
 
P

Peter Duniho

Ayaz said:
Hellow Friends ... Here what i m working for ... i want to show
Multiprocessing by creating about three processes and then showing there
states that which process is now in running state and which one is in
wait state and which on has successfully completed its working ... i
hope i can get the desired help

See BackgroundWorker and Thread classes, to start with. The former is
probably a better choice if you are brand new to multi-threaded
programming, especially if you stick strictly to the events and methods
provided by that class.

One final suggestion: keep in mind the Control.Invoke() method, which is
required for accessing your user-interface (all calls involving the UI
need to happen on the original thread on which the UI was created, so
from your other threads you need to use Invoke() to get code to execute
on the required thread).

You will find a number of message threads in this newsgroup concerning
writing code that uses threads, so I also recommend browsing those with
Google Groups to familiarize yourself with discussions that have already
taken place.

Pete
 
A

Ayaz Tariq

using System;
using System.Diagnostics;
using System.Threading;

class MainClass
{
public static void Main()
{
//Process [] allProcs = Process.GetProcesses();
//for(int i=0;i<allProcs.Length;i++)
//Console.WriteLine("Process Name :{0} ",
allProcs.ProcessName.ToString());
process();
Console.ReadLine();
}

static void process()
{
Process newProc = Process.Start("wordpad.exe");
Process newProc1 = Process.Start("notepad.exe");

Console.WriteLine("New process started.");

Console.WriteLine("process: {0}, id: {1}", newProc.ProcessName,
newProc.Id);
ProcessThreadCollection myThreads;
ProcessThreadCollection myThreads2;
while (!newProc.HasExited)
{

myThreads = newProc.Threads;
myThreads2 = newProc1.Threads;

foreach (ProcessThread pt in myThreads)
{
//ClearMyConsole.Clear() s
Console.Write("thread state: {0}", pt.ThreadState);
foreach (ProcessThread pt1 in myThreads2)
{
Console.WriteLine("Notepad state: {0}",
pt.ThreadState);

}
}


}




newProc.WaitForExit();

newProc.Close(); // free resources

Console.WriteLine("New process ended.");
}
}


it is not changing the status of states continuously
 
A

Ayaz Tariq

LIke when i close the notepad it must show that it is in now stop state
... please correct my code .
 
P

Peter Duniho

Ayaz said:
LIke when i close the notepad it must show that it is in now stop state
... please correct my code .

My apologies. I'm so used to people writing "multiprocessing" when they
really mean "threading", I misunderstood your own question.

Anyway, as far as the code you posted goes, I only have a couple of
observations:

1) I don't think it makes sense to put the enumeration of the
second process's threads inside the enumeration of the first process's
threads.

More significantly:

2) I don't think it makes sense to get the ThreadCollection of a
process before checking to see if that process has exited.

I can't really explain why the ThreadCollection isn't simply empty after
the process exits, but it probably has something to do with how Windows
manages the threads. The obvious fix is to check the process state
first before trying to look at the state of the process's threads.

Pete
 
P

Peter Duniho

Ayaz said:
LIke when i close the notepad it must show that it is in now stop state
... please correct my code .

Ah, one more thing. Had a chance to look a little more closely at the
code you posted. In addition to what I wrote, another issue is that
you are using the wrong ProcessThread variable in your second loop when
you print out the thread state.

This doesn't change the underlying behavior (there really is a thread in
the process's thread collection even after the process exits), but you
should probably get the diagnostic code correct before worrying about
the results. :)

Pete
 
A

Ayaz Tariq

Ahan ... so how can i get the update process states of notepad and
wordpad process if it cant happen with threading ...
 
P

Peter Duniho

Ayaz said:
Ahan ... so how can i get the update process states of notepad and
wordpad process if it cant happen with threading ...

You already do it for the Wordpad process (Process.HasExited property).
Just put the same check in for the Notepad process before you bother
to get the Threads property from it.

Pete
 
A

Ayaz Tariq

Ya but it only indicate that when the process is exited ..
i want to know when is it in ready state , when it is in running and
waiting state .. hope u get my point
 
P

Peter Duniho

Ayaz said:
Ya but it only indicate that when the process is exited ..
i want to know when is it in ready state , when it is in running and
waiting state .. hope u get my point

No, I'm afraid I don't. If you reread my posts, you will see that I am
suggesting you check the HasExited property before relying on the
Threads property.

If that doesn't address your question, you should probably try to be
more specific about why that doesn't work for you.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top