Hi Michael,
Sorry for letting you waiting for so long time.
I think you should use the messaging technology notifying the main thread to
execute the related code.(Pinvoke the SendMessage API)
Sample code listed below:
public int msgid;
[DllImport("user32.dll")]
public static extern bool PostMessage(IntPtr hwnd,int wMsg,IntPtr
wParam,IntPtr lParam);
[DllImport("user32.dll")]
public static extern int RegisterWindowMessage(string lpstring );
private void mProcess_Exited(object sender, EventArgs e)
{
int msg=RegisterWindowMessage("Notify");
this.msgid=msg;
bool rel=PostMessage(this.Handle ,msg,IntPtr.Zero,IntPtr.Zero );
}
protected override void WndProc(ref Message m)
{
// Listen for operating system messages.
if(m.Msg ==this.msgid)
{
Form2 f2=new Form2();
f2.ShowDialog(this);
}
base.WndProc(ref m);
}
It works well on my machine, if you still have any question, please feel
free
to let me know , I am glad to work with you.
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
| From: "Michael Culley" <
[email protected]>
| References: <
[email protected]>
<
[email protected]>
<#
[email protected]>
<
[email protected]>
| Subject: Re: Threading
| Date: Wed, 17 Sep 2003 11:26:18 +1000
| Lines: 97
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <#
[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: cpe-144-137-26-13.vic.bigpond.net.au 144.137.26.13
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:185302
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Hi Jeffrey,
|
| Thanks for taking the time to look at this. The main problem I have is
that
| I show a modal form in the event but it is not really modal, it is
possible
| for the user to still interact with other windows in the app. I also
| implement a copy-to-clipboard function on this modal form which throws an
| exception saying that ole operations cannot execute in this thread. I
would
| just like to call another function as if the main thread is calling it.
|
| I am going to buy a book and learn about threads properly (it's about
time I
| did), but if you have a quick solution in the mean time that would be
great.
|
| Regards,
| Michael Culley
|
|
| | >
| > Hi Michael,
| >
| > I think I understand your meanning.
| > I debuged into the mProcess_Exited() method and found that this method
ran
| > in
| > a seperate thread.
| >
| > I think this is by design. The .Net Framework uses this way to implement
| > the Process object's
| > Exited event.(Because this event related to 2 process, the event must
| > invoke some inter-process
| > communication, it can not be down through regular way).
| >
| > Can you show me what is the problem if you do not eliminate the second
| > thread?
| > May be I can provided you a workaround for this problem.
| >
| > Best regards,
| > Jeffrey Tan
| > Microsoft Online Partner Support
| > Get Secure! -
www.microsoft.com/security
| > This posting is provided "as is" with no warranties and confers no
rights.
| >
| > --------------------
| > | From: "Michael Culley" <
[email protected]>
| > | References: <
[email protected]>
| > <
[email protected]>
| > | Subject: Re: Threading
| > | Date: Tue, 16 Sep 2003 12:30:45 +1000
| > | Lines: 27
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | Message-ID: <#
[email protected]>
| > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | NNTP-Posting-Host: cpe-144-137-26-13.vic.bigpond.net.au 144.137.26.13
| > | Path:
| >
|
cpmsftngxa07.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTNGXA05.phx.gbl!TK2MSFTNGP08
| > phx.gbl!TK2MSFTNGP09.phx.gbl
| > | Xref: cpmsftngxa07.phx.gbl
| microsoft.public.dotnet.languages.csharp:184381
| > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > |
| > | Hi Jeffrey,
| > |
| > | Thanks for the reply. I am creating a process and responding to the
| > | Process_Exited event. This event appears to get called on a seperate
| > thread.
| > | I would just like to eliminate this second thread and call a function
as
| > if
| > | it was called from the apps main thread. I've pasted in some sample
code
| > | below.
| > |
| > | Thanks again,
| > | Michael Culley
| > |
| > |
| > | mProcess = new Process();
| > | mProcess.StartInfo.FileName = "C:\\whatever\\psp.exe";
| > | mProcess.EnableRaisingEvents = true;
| > | mProcess.Exited += new EventHandler(mProcess_Exited);
| > | mProcess.Start();
| > |
| > | ....
| > |
| > | private void mProcess_Exited(object sender, EventArgs e)
| > | {
| > | //Show modal form here
| > | //but want to do this from the apps main thread.
| > | }
| > |
| > |
| > |
| >
|
|
|