473,237 Members | 1,253 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,237 software developers and data experts.

Multithreading, Windows Forms vs Console Apps

I will try and make my question with out being too long winded. I have
been doing a lot of reading on how to do multithreading and I have
implemented the code from the following example on codeproject and
msdn.

http://www.codeproject.com/csharp/LaunchProcess.asp
http://msdn.microsoft.com/msdnmag/is...g/default.aspx

In the eample Standard out is read like this:

void ReadStdOut()
{
string str;
while ((str = process.StandardOutput.ReadLine()) != null)
{
FireAsync(StdOutReceived, this, new
DataReceivedEventArgs(str));
}
}

The reason for this is because ReadStdOut is in a separate thread. And
then FireAsync Calls methods registered on the UI Thread to the
StdOutRecieved delegate.

This works great if I am running a Windows Forms app and I can pass a
control into the contructor of ProcessCaller. Then FixAsync just uses
the controls BeginInvoke method to call back to the UIThread.

My Question is, If I want this to work for a console app as well will
the events get to the proper Main thread if I just fire the delegate
instead of using BeginInvoke? There are no controls in the Console
App, so I don't have a BeginInvoke call.

Please let me know if I can clairify any of this.
Thanks for your help

Adam dR.

Dec 8 '05 #1
6 5918
<Me*****@gmail.com> wrote:

<snip>
My Question is, If I want this to work for a console app as well will
the events get to the proper Main thread if I just fire the delegate
instead of using BeginInvoke? There are no controls in the Console
App, so I don't have a BeginInvoke call.


Yes, Console.WriteLine is threadsafe, so you can write it synchronously
in your reading thread.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 8 '05 #2
Hi,
My Question is, If I want this to work for a console app as well will
the events get to the proper Main thread if I just fire the delegate
instead of using BeginInvoke? There are no controls in the Console
App, so I don't have a BeginInvoke call.


Hi, I'm not very clear of where your problem is, for you to force an event
to be handled in a given thread that thread implements a message pump from
where it's constantly checking to see if there is a message waiting.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Dec 8 '05 #3
Okay let me explain this another way.

ProcessCaller Constructor:
public ProcessCaller(ISynchronizeInvoke isi);

This constructor takes an ISynchronizeInvoke (aka a Control) The
control is used to by the FireAsync method above to Fire the
StdOutRecievd Event on the UIThread. (NOTE all of this works fine)

processCaller = new ProcessCaller(this);
processCaller.StdOutReceived += new
DataReceivedHandler(writeStreamInfo);
processCaller.Start();

What I would like to do is make this ProcessCaller work for both
Windows Forms Apps and Console apps. But I don't have a control to
pass to the constructor of Process Caller.

This is the original FireAsync used to return event to the UIThread
from a working thread. Target is the ISynchronizeInvoke isi passed
into the constructor.

protected void FireAsync(Delegate dlg, params object[] pList)
{
if (dlg != null)
{
if (Target != null)
{
Target.BeginInvoke(dlg, pList);
}
}
}

What I would like to do is:
protected void FireAsync(Delegate dlg, params object[] pList)
{
if (dlg != null)
{
if (Target != null)
{
Target.BeginInvoke(dlg, pList);
}
else
{
dlg(pList[0], pList[1]);
}
}
}

This way I can Create a new ProcessCaller() with no ISynchronizeInvoke
object. My question is, If I am in a console application and this code
is in the main thread

processCaller = new ProcessCaller();
processCaller.StdOutReceived += new
DataReceivedHandler(writeStreamInfo);
processCaller.Start();

and the call to fire the StdOutRecieved Delegate is in a worker thread.
Is the event guarenteed to get back to my main thread and call
writeStreamInfo even though the delegate is called from the worker
thread?

Dec 8 '05 #4

<Me*****@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
Okay let me explain this another way.

ProcessCaller Constructor:
public ProcessCaller(ISynchronizeInvoke isi);

This constructor takes an ISynchronizeInvoke (aka a Control) The
control is used to by the FireAsync method above to Fire the
StdOutRecievd Event on the UIThread. (NOTE all of this works fine)

processCaller = new ProcessCaller(this);
processCaller.StdOutReceived += new
DataReceivedHandler(writeStreamInfo);
processCaller.Start();

What I would like to do is make this ProcessCaller work for both
Windows Forms Apps and Console apps. But I don't have a control to
pass to the constructor of Process Caller.

This is the original FireAsync used to return event to the UIThread
from a working thread. Target is the ISynchronizeInvoke isi passed
into the constructor.

protected void FireAsync(Delegate dlg, params object[] pList)
{
if (dlg != null)
{
if (Target != null)
{
Target.BeginInvoke(dlg, pList);
}
}
}

What I would like to do is:
protected void FireAsync(Delegate dlg, params object[] pList)
{
if (dlg != null)
{
if (Target != null)
{
Target.BeginInvoke(dlg, pList);
}
else
{
dlg(pList[0], pList[1]);
}
}
}

This way I can Create a new ProcessCaller() with no ISynchronizeInvoke
object. My question is, If I am in a console application and this code
is in the main thread

processCaller = new ProcessCaller();
processCaller.StdOutReceived += new
DataReceivedHandler(writeStreamInfo);
processCaller.Start();

and the call to fire the StdOutRecieved Delegate is in a worker thread.
Is the event guarenteed to get back to my main thread and call
writeStreamInfo even though the delegate is called from the worker
thread?


The delegate target (writeStreamInfo) will run on the worker thread, not on
your "main" thread.
Is there any reason you want this to happen, is there anything in
writeStreamInfo that has thread affinity?
The only reason you have to delegate the target to run on the "main" thread
in an GUI based application is that you may not read/update the UI from
another thread than the thread that created the UI element(s).
Or otherwise stated - Windows UI elements (basically HWND's) have thread
affinity, your console application doesn't touch Windows UI elements, so you
can update the "console" from any thread in the process (unless you are
using objects that have thread affinity, but I assume you don't).

Willy.

Dec 8 '05 #5
Hi,

and the call to fire the StdOutRecieved Delegate is in a worker thread.
Is the event guarenteed to get back to my main thread and call
writeStreamInfo even though the delegate is called from the worker
thread?


What does writeStreamInfo do that is SO important for you to run in the main
thread?
In a win app this is important, it not the same in a console app.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Dec 8 '05 #6
Hey Guys thanks a lot. I get it now. writeStreamInfo won't have any
code that isn't thread safe, on the console application so it should
work just fine. For some reason I thought I had to get the event back
to the main thread. Everything makes sence now. Just learning
threading and this helped a lot, I appreciate it!!!

~Adam dR.

Dec 8 '05 #7

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

Similar topics

1
by: dixp | last post by:
I'm new to writing multithreaded apps and I have a design question. I have a winforms app and a class which has a method that does processing which is time intensive. I want the user to be able...
8
by: Bf | last post by:
I was creating test projects using c# and was surprised that there seems to be only a form based windows applications available. Is it safe to assume that classic window applications that utilize a...
9
by: Popoxinhxan | last post by:
Dear experts, i want to develop an client application that consume the google search web service. In my MainForm i have a method to retrieve all the search result e.g. GetGoogleResults(). Now i...
2
by: shonend | last post by:
**** sorry about the length of the message. If you can't read the whole thing and still willing to help, read the last 2 paragraphs where the main problem is described. The introduction story is...
4
by: Charts | last post by:
I have a C# console application and I need pop up a Messagebox. However the IntelliSense does not show for using System.Windows.Forms; So, the application does not recognize the MessageBox. Please...
0
by: Need2CSharp | last post by:
Hi All, Following is a quote from an article on MSDN. Article Title: Safe, Simple Multithreading in Windows Forms URL:...
18
by: jello_world | last post by:
I am a VB6 programmer and I know how to build console apps.. I just dont understand how to get my mind around WinForms; they just seem a lot more complex than VB6. Thanks -Charlie
2
by: lewisms | last post by:
Hello all, I am quite new to c++/. Net so please don't shoot me down for being a newbie. Any way I am trying to make a simple multithreading program that is just to learn the ideas behind it...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.