Threading

  • Thread starter Thread starter Michael Culley
  • Start date Start date
M

Michael Culley

I have some code that returns an event on a different thread. This is
causing all sorts of problems, so I would like to just call a method on the
main thread of the app and eliminate the second thread. The very first thing
I want to do anyway is show a modal window so I don't need a second thread.
Being that I know very little about threads can anyone gimme a solutions.

Thanks,
Michael Culley
 
Hi Michael,

I think I do not quite understand your meanning.

1.What does your "return an event" mean?
Does it mean that your second thread fires this event or creates this event
handler for the
main thread?

2.How did all your problem arise?
Did they all cause by sychronization problem?
I think you should show me where the problem is(such as sample code of
reproducing these problem)

3.Why you can eliminate the second thread by showing a modal window?

Any details will help us have a clear understand of your problem.

Thanks

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]>
| Subject: Threading
| Date: Mon, 15 Sep 2003 17:40:04 +1000
| Lines: 10
| 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!TK2MSFTNGP11.phx.gbl
| Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.languages.csharp:184109
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| I have some code that returns an event on a different thread. This is
| causing all sorts of problems, so I would like to just call a method on
the
| main thread of the app and eliminate the second thread. The very first
thing
| I want to do anyway is show a modal window so I don't need a second
thread.
| Being that I know very little about threads can anyone gimme a solutions.
|
| Thanks,
| Michael Culley
|
|
|
 
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.
}
 
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.
| }
|
|
|
 
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,

Yes, it seems of that when invoking clipboard method in a seperate
thread, the exception will generate.
Also, the Form.ShowDialog() method does not show modal dialog.

I will do some research for you, I will rely to you after my research.

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.
| > | }
| > |
| > |
| > |
| >
|
|
|
 
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.
| > | }
| > |
| > |
| > |
| >
|
|
|
 
Jeffrey Tan said:
I think you should use the messaging technology notifying the main thread to
execute the related code.(Pinvoke the SendMessage API)

Hi Jeff,

Thanks for the reply. I was considering something like this but found a
built in solution that does the same thing. The solution was to use
this.Invoke(Delegate), this is a form or control. From what I can tell this
sends a message to the window. This means that the window doesn't process
the message until it is idle. This is perfect because it doesn't interupt
running code until it is completed.

Thanks for your help.

Cheers,
Michael Culley
 
Hi Michael,

Yes, it seems that it really works well using invoke method.
I am glad that it works finally.

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]>
<#[email protected]>
<[email protected]>
| Subject: Re: Threading
| Date: Sun, 28 Sep 2003 10:46:02 +1000
| Lines: 21
| 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: c210-49-163-120.chirn1.vic.optusnet.com.au
210.49.163.120
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:187738
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| | > I think you should use the messaging technology notifying the main
thread
| to
| > execute the related code.(Pinvoke the SendMessage API)
|
| Hi Jeff,
|
| Thanks for the reply. I was considering something like this but found a
| built in solution that does the same thing. The solution was to use
| this.Invoke(Delegate), this is a form or control. From what I can tell
this
| sends a message to the window. This means that the window doesn't process
| the message until it is idle. This is perfect because it doesn't interupt
| running code until it is completed.
|
| Thanks for your help.
|
| Cheers,
| Michael Culley
|
|
|
 
Back
Top