473,395 Members | 1,502 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,395 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 5928
<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....
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.