By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,496 Members | 1,527 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,496 IT Pros & Developers. It's quick & easy.

URGEN HELP: Starting a process from my application ???

P: n/a
Dear all,

I have an application which is suppose to start another executable process.
As soon as that process is running, I need to retrive its handle. The
problem of the particular process I am starting is that it has a welcome
window first which gets displayed and then the real windows after a while,in
other words it means that the process name is the same, but the handle I need
to retrive is the one from the final window and not the welome window.

So in real that means that from my main application I need to wait for a
while that the whol process started is really finishe to load befor getting
the real handle I am interresting on. The problem I get if I set a kind of
small loop in my main application is that the process I try to start is
freezed as long as I remain in my main application loop.

In other words how can I start my process, asynchronously from my main
application and then after a while try to retrive its handle ??

For you infor I use the follwoing code to starting it:
=================================================

.......
Dim myProcessStartInfo As New ProcessStartInfo(m_sIntouchInstallPath &
"\view.exe")
myProcessStartInfo.UseShellExecute = False
myProcess.StartInfo = myProcessStartInfo
myProcess.StartInfo.WindowStyle =
ProcessWindowStyle.Normal

myProcess.Start()
AddHandler myProcess.Exited, AddressOf MyProcessExited
' Set 'EnableRaisingEvents' to true, to raise
'Exited' event when process is terminated.
myProcess.EnableRaisingEvents = True

Dim i As Integer

While m_iViewerHandle = 0 Or i < 10
m_iViewerHandle = FindWindow(vbNullString, "VIEW")
i = i + 1
End While

m_iHandle = m_iViewerHandle
Return m_iHandle
.......

With this code, as long as I am in my While.... loop, the process I start is
freeze and my all application gets "No responding status".

To sumurize the situation I need to:
- strart my exe process without locking my my application
- retrive the correct window handle
Thanks to help me to solve my issue.
regards
CALDERARA Serge
Jul 21 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
I suggest calling myProcess.WaitForExit(1000) in your while loop. This will
give control to other apps (allowing them to process events) and come back to
your loop after a second (1000 mS).

Regards
John

"serge calderara" wrote:
Dear all,

I have an application which is suppose to start another executable process.
As soon as that process is running, I need to retrive its handle. The
problem of the particular process I am starting is that it has a welcome
window first which gets displayed and then the real windows after a while,in
other words it means that the process name is the same, but the handle I need
to retrive is the one from the final window and not the welome window.

So in real that means that from my main application I need to wait for a
while that the whol process started is really finishe to load befor getting
the real handle I am interresting on. The problem I get if I set a kind of
small loop in my main application is that the process I try to start is
freezed as long as I remain in my main application loop.

In other words how can I start my process, asynchronously from my main
application and then after a while try to retrive its handle ??

For you infor I use the follwoing code to starting it:
=================================================

......
Dim myProcessStartInfo As New ProcessStartInfo(m_sIntouchInstallPath &
"\view.exe")
myProcessStartInfo.UseShellExecute = False
myProcess.StartInfo = myProcessStartInfo
myProcess.StartInfo.WindowStyle =
ProcessWindowStyle.Normal

myProcess.Start()
AddHandler myProcess.Exited, AddressOf MyProcessExited
' Set 'EnableRaisingEvents' to true, to raise
'Exited' event when process is terminated.
myProcess.EnableRaisingEvents = True

Dim i As Integer

While m_iViewerHandle = 0 Or i < 10
m_iViewerHandle = FindWindow(vbNullString, "VIEW")
i = i + 1
End While

m_iHandle = m_iViewerHandle
Return m_iHandle
......

With this code, as long as I am in my While.... loop, the process I start is
freeze and my all application gets "No responding status".

To sumurize the situation I need to:
- strart my exe process without locking my my application
- retrive the correct window handle
Thanks to help me to solve my issue.
regards
CALDERARA Serge

Jul 21 '05 #2

P: n/a
HI john,

Thnaks for your answer. I give a try by changing my code according to your
advise but it is still the same. Having this while loop place my application
in a Non Responding satus and my process is partialy started but lock at the
welcome screen.

Now if I close my main application with task manager, my started process
continue to execute and open fully .....

Any other idea how to start that process and retrive its handle without
locking the whole application ? it seems it all runs in the same context and
threads that is may be the reason.. if so shouzdl I create a thread that
launch my process ?

The modify loop isas follow :
While m_iViewerHandle = 0
------> myProcess.WaitForExit(1000)
System.Threading.Thread.Sleep(500)
m_iViewerHandle = FindWindow(vbNullString, "VIEW
- " & m_sApplicationPath)
End While

Thnaks for your help
serge

"John Roper" wrote:
I suggest calling myProcess.WaitForExit(1000) in your while loop. This will
give control to other apps (allowing them to process events) and come back to
your loop after a second (1000 mS).

Regards
John

"serge calderara" wrote:
Dear all,

I have an application which is suppose to start another executable process.
As soon as that process is running, I need to retrive its handle. The
problem of the particular process I am starting is that it has a welcome
window first which gets displayed and then the real windows after a while,in
other words it means that the process name is the same, but the handle I need
to retrive is the one from the final window and not the welome window.

So in real that means that from my main application I need to wait for a
while that the whol process started is really finishe to load befor getting
the real handle I am interresting on. The problem I get if I set a kind of
small loop in my main application is that the process I try to start is
freezed as long as I remain in my main application loop.

In other words how can I start my process, asynchronously from my main
application and then after a while try to retrive its handle ??

For you infor I use the follwoing code to starting it:
=================================================

......
Dim myProcessStartInfo As New ProcessStartInfo(m_sIntouchInstallPath &
"\view.exe")
myProcessStartInfo.UseShellExecute = False
myProcess.StartInfo = myProcessStartInfo
myProcess.StartInfo.WindowStyle =
ProcessWindowStyle.Normal

myProcess.Start()
AddHandler myProcess.Exited, AddressOf MyProcessExited
' Set 'EnableRaisingEvents' to true, to raise
'Exited' event when process is terminated.
myProcess.EnableRaisingEvents = True

Dim i As Integer

While m_iViewerHandle = 0 Or i < 10
m_iViewerHandle = FindWindow(vbNullString, "VIEW")
i = i + 1
End While

m_iHandle = m_iViewerHandle
Return m_iHandle
......

With this code, as long as I am in my While.... loop, the process I start is
freeze and my all application gets "No responding status".

To sumurize the situation I need to:
- strart my exe process without locking my my application
- retrive the correct window handle
Thanks to help me to solve my issue.
regards
CALDERARA Serge

Jul 21 '05 #3

P: n/a
Hi Serge,

Your app will freeze while you wait and sleep, but the new process
shouldn't. Perhaps it would be worth trying UseShellExecute = True? You only
really need to set this to False if you are going to change stdin/out.

Cheers
John

"serge calderara" wrote:
HI john,

Thnaks for your answer. I give a try by changing my code according to your
advise but it is still the same. Having this while loop place my application
in a Non Responding satus and my process is partialy started but lock at the
welcome screen.

Now if I close my main application with task manager, my started process
continue to execute and open fully .....

Any other idea how to start that process and retrive its handle without
locking the whole application ? it seems it all runs in the same context and
threads that is may be the reason.. if so shouzdl I create a thread that
launch my process ?

The modify loop isas follow :
While m_iViewerHandle = 0
------> myProcess.WaitForExit(1000)
System.Threading.Thread.Sleep(500)
m_iViewerHandle = FindWindow(vbNullString, "VIEW
- " & m_sApplicationPath)
End While

Thnaks for your help
serge

"John Roper" wrote:
I suggest calling myProcess.WaitForExit(1000) in your while loop. This will
give control to other apps (allowing them to process events) and come back to
your loop after a second (1000 mS).

Regards
John

"serge calderara" wrote:
Dear all,

I have an application which is suppose to start another executable process.
As soon as that process is running, I need to retrive its handle. The
problem of the particular process I am starting is that it has a welcome
window first which gets displayed and then the real windows after a while,in
other words it means that the process name is the same, but the handle I need
to retrive is the one from the final window and not the welome window.

So in real that means that from my main application I need to wait for a
while that the whol process started is really finishe to load befor getting
the real handle I am interresting on. The problem I get if I set a kind of
small loop in my main application is that the process I try to start is
freezed as long as I remain in my main application loop.

In other words how can I start my process, asynchronously from my main
application and then after a while try to retrive its handle ??

For you infor I use the follwoing code to starting it:
=================================================

......
Dim myProcessStartInfo As New ProcessStartInfo(m_sIntouchInstallPath &
"\view.exe")
myProcessStartInfo.UseShellExecute = False
myProcess.StartInfo = myProcessStartInfo
myProcess.StartInfo.WindowStyle =
ProcessWindowStyle.Normal

myProcess.Start()
AddHandler myProcess.Exited, AddressOf MyProcessExited
' Set 'EnableRaisingEvents' to true, to raise
'Exited' event when process is terminated.
myProcess.EnableRaisingEvents = True

Dim i As Integer

While m_iViewerHandle = 0 Or i < 10
m_iViewerHandle = FindWindow(vbNullString, "VIEW")
i = i + 1
End While

m_iHandle = m_iViewerHandle
Return m_iHandle
......

With this code, as long as I am in my While.... loop, the process I start is
freeze and my all application gets "No responding status".

To sumurize the situation I need to:
- strart my exe process without locking my my application
- retrive the correct window handle
Thanks to help me to solve my issue.
regards
CALDERARA Serge

Jul 21 '05 #4

P: n/a
It sounds like the process is starting on the same thread. Might be good to
start the process on another thread and let it invoke a callback method when
it is ready. This will prevent your primary thread from hanging.

The following example is in C#.
1. Make sure the namespace System.Threading is imported:
using System.Threading;

2. Instead of starting the process, queue the process into the thread pool.
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));

3. The ThreadProc procedure should be declared, which actually starts the
process on the new thread.
private void ThreadProc(object stateInfo)
{
// Start the process here.
// Whenever the process is finished, it can execute another method
// which continues where this leaves off.
}

Hope that helps.

--
Jason Whitted

"serge calderara" wrote:
Dear all,

I have an application which is suppose to start another executable process.
As soon as that process is running, I need to retrive its handle. The
problem of the particular process I am starting is that it has a welcome
window first which gets displayed and then the real windows after a while,in
other words it means that the process name is the same, but the handle I need
to retrive is the one from the final window and not the welome window.

So in real that means that from my main application I need to wait for a
while that the whol process started is really finishe to load befor getting
the real handle I am interresting on. The problem I get if I set a kind of
small loop in my main application is that the process I try to start is
freezed as long as I remain in my main application loop.

In other words how can I start my process, asynchronously from my main
application and then after a while try to retrive its handle ??

For you infor I use the follwoing code to starting it:
=================================================

......
Dim myProcessStartInfo As New ProcessStartInfo(m_sIntouchInstallPath &
"\view.exe")
myProcessStartInfo.UseShellExecute = False
myProcess.StartInfo = myProcessStartInfo
myProcess.StartInfo.WindowStyle =
ProcessWindowStyle.Normal

myProcess.Start()
AddHandler myProcess.Exited, AddressOf MyProcessExited
' Set 'EnableRaisingEvents' to true, to raise
'Exited' event when process is terminated.
myProcess.EnableRaisingEvents = True

Dim i As Integer

While m_iViewerHandle = 0 Or i < 10
m_iViewerHandle = FindWindow(vbNullString, "VIEW")
i = i + 1
End While

m_iHandle = m_iViewerHandle
Return m_iHandle
......

With this code, as long as I am in my While.... loop, the process I start is
freeze and my all application gets "No responding status".

To sumurize the situation I need to:
- strart my exe process without locking my my application
- retrive the correct window handle
Thanks to help me to solve my issue.
regards
CALDERARA Serge

Jul 21 '05 #5

P: n/a
HI John,

I I use what you mentionned it freeze everything, my application and the
process I start.

I have mange to make it working by using the following code :
==============================================
myProcess.EnableRaisingEvents = True
myProcess.WaitForInputIdle(20000)

If myProcess.Responding Then
m_iViewerHandle =
myProcess.MainWindowHandle.ToInt32
Dim sTitle As String
sTitle = myProcess.MainWindowTitle
Me.m_ObjMessagePrvd.LogMessage("Process has ben
catch with hdl: " & CStr(m_iViewerHandle) & " and WIndow name as : " &
sTitle, TraceLevel.Info)
Else
myProcess.Kill()
End If
Then I can play with the timeout value for
myProcess.WaitForInputIdle(20000) as a parameter.

THis the launching application start my process and wait for the idle state
of the launched process, if it fails during that time then this means trouble
with the process.

If I use this, what is the best way afterwards to retrive the handle of the
process window by its window name ?

I use the following if my process is responding:
==================================
m_iViewerHandle = myProcess.MainWindowHandle.ToInt32
Dim sTitle As String
sTitle = myProcess.MainWindowTitle

but I het m_iViewerHandle= 0 and sTitle="", it seems that even the process
is responding the main window is noit yet ready

thnaks for your help
serge

"John Roper" wrote:
Hi Serge,

Your app will freeze while you wait and sleep, but the new process
shouldn't. Perhaps it would be worth trying UseShellExecute = True? You only
really need to set this to False if you are going to change stdin/out.

Cheers
John

"serge calderara" wrote:
HI john,

Thnaks for your answer. I give a try by changing my code according to your
advise but it is still the same. Having this while loop place my application
in a Non Responding satus and my process is partialy started but lock at the
welcome screen.

Now if I close my main application with task manager, my started process
continue to execute and open fully .....

Any other idea how to start that process and retrive its handle without
locking the whole application ? it seems it all runs in the same context and
threads that is may be the reason.. if so shouzdl I create a thread that
launch my process ?

The modify loop isas follow :
While m_iViewerHandle = 0
------> myProcess.WaitForExit(1000)
System.Threading.Thread.Sleep(500)
m_iViewerHandle = FindWindow(vbNullString, "VIEW
- " & m_sApplicationPath)
End While

Thnaks for your help
serge

"John Roper" wrote:
I suggest calling myProcess.WaitForExit(1000) in your while loop. This will
give control to other apps (allowing them to process events) and come back to
your loop after a second (1000 mS).

Regards
John

"serge calderara" wrote:

> Dear all,
>
> I have an application which is suppose to start another executable process.
> As soon as that process is running, I need to retrive its handle. The
> problem of the particular process I am starting is that it has a welcome
> window first which gets displayed and then the real windows after a while,in
> other words it means that the process name is the same, but the handle I need
> to retrive is the one from the final window and not the welome window.
>
> So in real that means that from my main application I need to wait for a
> while that the whol process started is really finishe to load befor getting
> the real handle I am interresting on. The problem I get if I set a kind of
> small loop in my main application is that the process I try to start is
> freezed as long as I remain in my main application loop.
>
> In other words how can I start my process, asynchronously from my main
> application and then after a while try to retrive its handle ??
>
> For you infor I use the follwoing code to starting it:
> =================================================
>
> ......
> Dim myProcessStartInfo As New ProcessStartInfo(m_sIntouchInstallPath &
> "\view.exe")
> myProcessStartInfo.UseShellExecute = False
> myProcess.StartInfo = myProcessStartInfo
> myProcess.StartInfo.WindowStyle =
> ProcessWindowStyle.Normal
>
> myProcess.Start()
> AddHandler myProcess.Exited, AddressOf MyProcessExited
> ' Set 'EnableRaisingEvents' to true, to raise
> 'Exited' event when process is terminated.
> myProcess.EnableRaisingEvents = True
>
> Dim i As Integer
>
> While m_iViewerHandle = 0 Or i < 10
> m_iViewerHandle = FindWindow(vbNullString, "VIEW")
> i = i + 1
> End While
>
> m_iHandle = m_iViewerHandle
> Return m_iHandle
> ......
>
> With this code, as long as I am in my While.... loop, the process I start is
> freeze and my all application gets "No responding status".
>
> To sumurize the situation I need to:
> - strart my exe process without locking my my application
> - retrive the correct window handle
>
>
> Thanks to help me to solve my issue.
> regards
> CALDERARA Serge

Jul 21 '05 #6

P: n/a
I will give a try, but then one important point is that, when the process is
properly launched, I need to retrive the handle of the displayed window...

If I start as a new thread, how can my main application is infromed that my
process window is ready. How can my new created thread can return the window
handle to my main applciatiom?

Thanks for your help

"JWhitted" wrote:
It sounds like the process is starting on the same thread. Might be good to
start the process on another thread and let it invoke a callback method when
it is ready. This will prevent your primary thread from hanging.

The following example is in C#.
1. Make sure the namespace System.Threading is imported:
using System.Threading;

2. Instead of starting the process, queue the process into the thread pool.
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));

3. The ThreadProc procedure should be declared, which actually starts the
process on the new thread.
private void ThreadProc(object stateInfo)
{
// Start the process here.
// Whenever the process is finished, it can execute another method
// which continues where this leaves off.
}

Hope that helps.

--
Jason Whitted

"serge calderara" wrote:
Dear all,

I have an application which is suppose to start another executable process.
As soon as that process is running, I need to retrive its handle. The
problem of the particular process I am starting is that it has a welcome
window first which gets displayed and then the real windows after a while,in
other words it means that the process name is the same, but the handle I need
to retrive is the one from the final window and not the welome window.

So in real that means that from my main application I need to wait for a
while that the whol process started is really finishe to load befor getting
the real handle I am interresting on. The problem I get if I set a kind of
small loop in my main application is that the process I try to start is
freezed as long as I remain in my main application loop.

In other words how can I start my process, asynchronously from my main
application and then after a while try to retrive its handle ??

For you infor I use the follwoing code to starting it:
=================================================

......
Dim myProcessStartInfo As New ProcessStartInfo(m_sIntouchInstallPath &
"\view.exe")
myProcessStartInfo.UseShellExecute = False
myProcess.StartInfo = myProcessStartInfo
myProcess.StartInfo.WindowStyle =
ProcessWindowStyle.Normal

myProcess.Start()
AddHandler myProcess.Exited, AddressOf MyProcessExited
' Set 'EnableRaisingEvents' to true, to raise
'Exited' event when process is terminated.
myProcess.EnableRaisingEvents = True

Dim i As Integer

While m_iViewerHandle = 0 Or i < 10
m_iViewerHandle = FindWindow(vbNullString, "VIEW")
i = i + 1
End While

m_iHandle = m_iViewerHandle
Return m_iHandle
......

With this code, as long as I am in my While.... loop, the process I start is
freeze and my all application gets "No responding status".

To sumurize the situation I need to:
- strart my exe process without locking my my application
- retrive the correct window handle
Thanks to help me to solve my issue.
regards
CALDERARA Serge

Jul 21 '05 #7

P: n/a
An other question I got is:

How my main thread will know that the process has finished started and it
interface is open?

In the other way, how can the new thread informed the maion frame about its
status and its main windows handle ? can the new thread raise an event and
tha main thread is registered to that events?

thanks for your help
serge

"JWhitted" wrote:
It sounds like the process is starting on the same thread. Might be good to
start the process on another thread and let it invoke a callback method when
it is ready. This will prevent your primary thread from hanging.

The following example is in C#.
1. Make sure the namespace System.Threading is imported:
using System.Threading;

2. Instead of starting the process, queue the process into the thread pool.
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));

3. The ThreadProc procedure should be declared, which actually starts the
process on the new thread.
private void ThreadProc(object stateInfo)
{
// Start the process here.
// Whenever the process is finished, it can execute another method
// which continues where this leaves off.
}

Hope that helps.

--
Jason Whitted

"serge calderara" wrote:
Dear all,

I have an application which is suppose to start another executable process.
As soon as that process is running, I need to retrive its handle. The
problem of the particular process I am starting is that it has a welcome
window first which gets displayed and then the real windows after a while,in
other words it means that the process name is the same, but the handle I need
to retrive is the one from the final window and not the welome window.

So in real that means that from my main application I need to wait for a
while that the whol process started is really finishe to load befor getting
the real handle I am interresting on. The problem I get if I set a kind of
small loop in my main application is that the process I try to start is
freezed as long as I remain in my main application loop.

In other words how can I start my process, asynchronously from my main
application and then after a while try to retrive its handle ??

For you infor I use the follwoing code to starting it:
=================================================

......
Dim myProcessStartInfo As New ProcessStartInfo(m_sIntouchInstallPath &
"\view.exe")
myProcessStartInfo.UseShellExecute = False
myProcess.StartInfo = myProcessStartInfo
myProcess.StartInfo.WindowStyle =
ProcessWindowStyle.Normal

myProcess.Start()
AddHandler myProcess.Exited, AddressOf MyProcessExited
' Set 'EnableRaisingEvents' to true, to raise
'Exited' event when process is terminated.
myProcess.EnableRaisingEvents = True

Dim i As Integer

While m_iViewerHandle = 0 Or i < 10
m_iViewerHandle = FindWindow(vbNullString, "VIEW")
i = i + 1
End While

m_iHandle = m_iViewerHandle
Return m_iHandle
......

With this code, as long as I am in my While.... loop, the process I start is
freeze and my all application gets "No responding status".

To sumurize the situation I need to:
- strart my exe process without locking my my application
- retrive the correct window handle
Thanks to help me to solve my issue.
regards
CALDERARA Serge

Jul 21 '05 #8

P: n/a
I wrote a little Console App that should show you what I'm talking about.
This app will start a process in another thread, which launches Internet
Explorer (you might need to change the path to get this to work on your box).
It then pauses for a few seconds and then calls a callback function which
accepts the handle of the window as a parameter. Once the handle of the
window is received, the IE window is flashed and then closed (along with the
process).

The only additional concern I can forsee you having is using this
functionality as part of a windows app instead of a console app. In that
case, instead of calling the callback function directly, you might have to
create a callback delegegate method and invoke it using the Form's Invoke
method. This will be necessary because all GUI modifications should happen
on the primary thread of the window. Any attempt to modify GUI elements from
a child thread usually results in an exception being thrown.

Hope that helps.

--
Jason Whitted

#########################
Class1.cs
#########################
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;

namespace Testing
{
public class Class1
{
private Process proc;

public Class1()
{
Console.WriteLine("Class1 Constructor called");
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
}

public void ThreadProc(object stateInfo)
{
Console.WriteLine("Starting Process");
proc = Process.Start(@"C:\Program Files\Internet
Explorer\IExplore.exe", "http://www.microsoft.com");
Console.Write("Pausing: ");
for(int x=5; x>0; x--)
{
Console.Write(" " + x);
Thread.Sleep(1000);
}
Console.WriteLine();
Callback(proc.MainWindowHandle);

}

public void Callback(IntPtr handle)
{
Console.WriteLine("Callback called (handle=" + handle.ToString()
+ ")");
Thread.Sleep(1000);
Console.WriteLine("Flashing window");
for(int x=0; x<20; x++)
{
FlashWindow(handle, x%2);
Thread.Sleep(50);
}
Console.WriteLine("Closing Window Process");
proc.CloseMainWindow();
Console.WriteLine("Closing Process");
proc.Close();
Console.WriteLine("Done");
}

[DllImport("user32")]
public static extern int FlashWindow(IntPtr handle, int bInvert);

public static void Main(string[] args)
{
new Class1();
Console.ReadLine();
}
}
}

Jul 21 '05 #9

P: n/a
Thnaks for your sample appliction...
It helps me a lot to understand what you mean, but sorry to disturbed you
again, I still have some weal points on really understanding...

1 - CALLBACK term
===============
In your sample in the start process thread you call the function named
CALLBACK to retrive the window handle and close it after a while.
What I am confuse here is the term CALLBACK, because if I name your function
an other manner it works too, so does the CALLBACL terms you are mentionning
here is this wierd and strange system behaviour that developper like me do
not reaal understand. could you details what you means here by callback...

And I do not catch what you means also by Delegates call back events, I
never used that..

I am nearlyy close to the end but I like to do things that I am clearly
understanding

2 Getting handl back toi main applcation
==========================
No problem for doing this in my WIndows applicatin, the only point is that
after my process is launch, I should be able to close it or show it if it has
been minimized and the only way is that my main application should retrive
the window handle.

3 Show the created process window application
Another point that come to my mind.lets say that the process has been
started properly and its main windows application is running. Now my user is
minimizing the windows whcih goes down to taskbar. I should be able from my
main application to show that window in normal mode in case it is called
again has it is already exist. Under VB 5 for instance i will use the
FindWindow and ShowWindow function. How to do then with this new thread to
call window in front.
If I call the same procedure that was callin it initially to start the
process, it will creat an other thred instance, and I would need in fact to
just call the one which have been launched..

Thnaks for your help

Serge

"JWhitted" wrote:
I wrote a little Console App that should show you what I'm talking about.
This app will start a process in another thread, which launches Internet
Explorer (you might need to change the path to get this to work on your box).
It then pauses for a few seconds and then calls a callback function which
accepts the handle of the window as a parameter. Once the handle of the
window is received, the IE window is flashed and then closed (along with the
process).

The only additional concern I can forsee you having is using this
functionality as part of a windows app instead of a console app. In that
case, instead of calling the callback function directly, you might have to
create a callback delegegate method and invoke it using the Form's Invoke
method. This will be necessary because all GUI modifications should happen
on the primary thread of the window. Any attempt to modify GUI elements from
a child thread usually results in an exception being thrown.

Hope that helps.

--
Jason Whitted

#########################
Class1.cs
#########################
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;

namespace Testing
{
public class Class1
{
private Process proc;

public Class1()
{
Console.WriteLine("Class1 Constructor called");
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
}

public void ThreadProc(object stateInfo)
{
Console.WriteLine("Starting Process");
proc = Process.Start(@"C:\Program Files\Internet
Explorer\IExplore.exe", "http://www.microsoft.com");
Console.Write("Pausing: ");
for(int x=5; x>0; x--)
{
Console.Write(" " + x);
Thread.Sleep(1000);
}
Console.WriteLine();
Callback(proc.MainWindowHandle);

}

public void Callback(IntPtr handle)
{
Console.WriteLine("Callback called (handle=" + handle.ToString()
+ ")");
Thread.Sleep(1000);
Console.WriteLine("Flashing window");
for(int x=0; x<20; x++)
{
FlashWindow(handle, x%2);
Thread.Sleep(50);
}
Console.WriteLine("Closing Window Process");
proc.CloseMainWindow();
Console.WriteLine("Closing Process");
proc.Close();
Console.WriteLine("Done");
}

[DllImport("user32")]
public static extern int FlashWindow(IntPtr handle, int bInvert);

public static void Main(string[] args)
{
new Class1();
Console.ReadLine();
}
}
}

Jul 21 '05 #10

P: n/a
hello,

I have implemented your suggested way on starting a process into an external
thread. Thats wiorks ok in my windows applciation.

Problem that I noticed now is that previously my application was register to
the Exited event generated by the launch process and now I cannot catch
anymore that event as it is generated in a different thread.

I think that it joins a bit my previous post in the sens that how to catch
from my main application Events generated from an other thread

thnask for your help
serge

"JWhitted" wrote:
I wrote a little Console App that should show you what I'm talking about.
This app will start a process in another thread, which launches Internet
Explorer (you might need to change the path to get this to work on your box).
It then pauses for a few seconds and then calls a callback function which
accepts the handle of the window as a parameter. Once the handle of the
window is received, the IE window is flashed and then closed (along with the
process).

The only additional concern I can forsee you having is using this
functionality as part of a windows app instead of a console app. In that
case, instead of calling the callback function directly, you might have to
create a callback delegegate method and invoke it using the Form's Invoke
method. This will be necessary because all GUI modifications should happen
on the primary thread of the window. Any attempt to modify GUI elements from
a child thread usually results in an exception being thrown.

Hope that helps.

--
Jason Whitted

#########################
Class1.cs
#########################
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;

namespace Testing
{
public class Class1
{
private Process proc;

public Class1()
{
Console.WriteLine("Class1 Constructor called");
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
}

public void ThreadProc(object stateInfo)
{
Console.WriteLine("Starting Process");
proc = Process.Start(@"C:\Program Files\Internet
Explorer\IExplore.exe", "http://www.microsoft.com");
Console.Write("Pausing: ");
for(int x=5; x>0; x--)
{
Console.Write(" " + x);
Thread.Sleep(1000);
}
Console.WriteLine();
Callback(proc.MainWindowHandle);

}

public void Callback(IntPtr handle)
{
Console.WriteLine("Callback called (handle=" + handle.ToString()
+ ")");
Thread.Sleep(1000);
Console.WriteLine("Flashing window");
for(int x=0; x<20; x++)
{
FlashWindow(handle, x%2);
Thread.Sleep(50);
}
Console.WriteLine("Closing Window Process");
proc.CloseMainWindow();
Console.WriteLine("Closing Process");
proc.Close();
Console.WriteLine("Done");
}

[DllImport("user32")]
public static extern int FlashWindow(IntPtr handle, int bInvert);

public static void Main(string[] args)
{
new Class1();
Console.ReadLine();
}
}
}

Jul 21 '05 #11

P: n/a
hello again

so soirry to disturb you, but as you may know you build your knowledge with
the knowledge of others.
:-)

I point out a document on how to use Delegate events, I will try to
implement it for my case.

In case somethis still dark for me do you might if I conctact you here again

regards and thanks
serge

"JWhitted" wrote:
I wrote a little Console App that should show you what I'm talking about.
This app will start a process in another thread, which launches Internet
Explorer (you might need to change the path to get this to work on your box).
It then pauses for a few seconds and then calls a callback function which
accepts the handle of the window as a parameter. Once the handle of the
window is received, the IE window is flashed and then closed (along with the
process).

The only additional concern I can forsee you having is using this
functionality as part of a windows app instead of a console app. In that
case, instead of calling the callback function directly, you might have to
create a callback delegegate method and invoke it using the Form's Invoke
method. This will be necessary because all GUI modifications should happen
on the primary thread of the window. Any attempt to modify GUI elements from
a child thread usually results in an exception being thrown.

Hope that helps.

--
Jason Whitted

#########################
Class1.cs
#########################
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;

namespace Testing
{
public class Class1
{
private Process proc;

public Class1()
{
Console.WriteLine("Class1 Constructor called");
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
}

public void ThreadProc(object stateInfo)
{
Console.WriteLine("Starting Process");
proc = Process.Start(@"C:\Program Files\Internet
Explorer\IExplore.exe", "http://www.microsoft.com");
Console.Write("Pausing: ");
for(int x=5; x>0; x--)
{
Console.Write(" " + x);
Thread.Sleep(1000);
}
Console.WriteLine();
Callback(proc.MainWindowHandle);

}

public void Callback(IntPtr handle)
{
Console.WriteLine("Callback called (handle=" + handle.ToString()
+ ")");
Thread.Sleep(1000);
Console.WriteLine("Flashing window");
for(int x=0; x<20; x++)
{
FlashWindow(handle, x%2);
Thread.Sleep(50);
}
Console.WriteLine("Closing Window Process");
proc.CloseMainWindow();
Console.WriteLine("Closing Process");
proc.Close();
Console.WriteLine("Done");
}

[DllImport("user32")]
public static extern int FlashWindow(IntPtr handle, int bInvert);

public static void Main(string[] args)
{
new Class1();
Console.ReadLine();
}
}
}

Jul 21 '05 #12

P: n/a
Hello gain,

Dear all,

The new problem I met when implementing Delegate to my application is that
my application is creating dynamic reference to existing plug-ins and the
only way I communicate with my plug in and my applciation is a common
Interface that I have defined.

Now, how can I defined my delegate type in my Interface and in my
application to be able to work correctly ?

Thnaks for your help
Serge

"JWhitted" wrote:
I wrote a little Console App that should show you what I'm talking about.
This app will start a process in another thread, which launches Internet
Explorer (you might need to change the path to get this to work on your box).
It then pauses for a few seconds and then calls a callback function which
accepts the handle of the window as a parameter. Once the handle of the
window is received, the IE window is flashed and then closed (along with the
process).

The only additional concern I can forsee you having is using this
functionality as part of a windows app instead of a console app. In that
case, instead of calling the callback function directly, you might have to
create a callback delegegate method and invoke it using the Form's Invoke
method. This will be necessary because all GUI modifications should happen
on the primary thread of the window. Any attempt to modify GUI elements from
a child thread usually results in an exception being thrown.

Hope that helps.

--
Jason Whitted

#########################
Class1.cs
#########################
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;

namespace Testing
{
public class Class1
{
private Process proc;

public Class1()
{
Console.WriteLine("Class1 Constructor called");
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
}

public void ThreadProc(object stateInfo)
{
Console.WriteLine("Starting Process");
proc = Process.Start(@"C:\Program Files\Internet
Explorer\IExplore.exe", "http://www.microsoft.com");
Console.Write("Pausing: ");
for(int x=5; x>0; x--)
{
Console.Write(" " + x);
Thread.Sleep(1000);
}
Console.WriteLine();
Callback(proc.MainWindowHandle);

}

public void Callback(IntPtr handle)
{
Console.WriteLine("Callback called (handle=" + handle.ToString()
+ ")");
Thread.Sleep(1000);
Console.WriteLine("Flashing window");
for(int x=0; x<20; x++)
{
FlashWindow(handle, x%2);
Thread.Sleep(50);
}
Console.WriteLine("Closing Window Process");
proc.CloseMainWindow();
Console.WriteLine("Closing Process");
proc.Close();
Console.WriteLine("Done");
}

[DllImport("user32")]
public static extern int FlashWindow(IntPtr handle, int bInvert);

public static void Main(string[] args)
{
new Class1();
Console.ReadLine();
}
}
}

Jul 21 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.