473,657 Members | 2,475 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem With Threading Using Separate Class Files

I'm writing some sample threading code, and have already got many examples
working that look a lot similar to the ones mentioned here. What I'm seeing
is that most people put their method that will be run as a thread inside the
overall Form's class, which is OK for examples, but not really what you'd
see in real life.

So, I decided to write a class (called BusinessOperati on) and have
properties & methods in it. Then, I have 1 method in it called
LongRunningBusi nessOp.

So, in my Form UI code I create the thread by instantiating an object of
type BusinessOperati on and then using the fpr to the LongRunningBusi nessOp
to start my thread.

All works well, except when I want to use delegates to update my UI back
from LongRunningBusi nessOp. For whatever reason, by moving the threaded
code outside of the Form, the compiler now says that the method (I call it
UpdateProgressM eter) I point to when creating an instance of the delegate
(not actually defining the delegate), needs to be static. This wasn't the
case before. Problem is, that if I make it static in the Form class, then
it cannot access the controls within the form.

So, what used to work:

public delegate void ProgressDelegat e(string strPercentage);
private void LongRunningBusi nessOp()
{
// Do code
ProgressDelegat e fpr = new ProgressDelegat e(UpdateProgres sMeter);
// More code then call fpr.Invoke to update the status
}

Now yields a compile error (on the statement after the "//Do code" comment)
when I move the method to outside of the Form class and into it's own class
file. Like I said, all works fine if I just stuff this method and delegate
declaration back into the Form class, but that's not what I want to do.

I know it's probably something simple, like semantics, but sometimes the
obvious ones are the hardest to find when you're the closest to them.

Input would be appreciated.

--
Doug Thews
Director, Customer Solutions
D&D Consulting Services
----------------
Visit my Tech Blog at:
http://www.ddconsult.com/blogs/illuminati/


Nov 16 '05 #1
7 1648
Doug Thews <do*******@remo veme.ddconsult. com> wrote:
I'm writing some sample threading code, and have already got many examples
working that look a lot similar to the ones mentioned here. What I'm seeing
is that most people put their method that will be run as a thread inside the
overall Form's class, which is OK for examples, but not really what you'd
see in real life.

So, I decided to write a class (called BusinessOperati on) and have
properties & methods in it. Then, I have 1 method in it called
LongRunningBusi nessOp.

So, in my Form UI code I create the thread by instantiating an object of
type BusinessOperati on and then using the fpr to the LongRunningBusi nessOp
to start my thread.

All works well, except when I want to use delegates to update my UI back
from LongRunningBusi nessOp. For whatever reason, by moving the threaded
code outside of the Form, the compiler now says that the method (I call it
UpdateProgressM eter) I point to when creating an instance of the delegate
(not actually defining the delegate), needs to be static. This wasn't the
case before. Problem is, that if I make it static in the Form class, then
it cannot access the controls within the form.


I suspect you were trying to use

new ProgressDelegat e (MyForm.UpdateP rogressMeter) where MyForm is the
name of the class. Instead, what you need is

new ProgressDelegat e (someFormInstan ce.UpdateProgre ssMeter) where
someFormInstanc e is a reference to the form you want to update.

When you instantiate BusinessOperati on, you need to pass it the form it
"belongs" to so that it knows what to update.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
That sounds very plausible, and it is what I'm doing. This may be a very
stupid question, but how can I get access to the instance of the running
form when I can't pass anything to the thread that's started? I guess I
could make a public property in the BusinessOperati on class that holds the
value after I instantiate the object, and then reference it from within the
delegate creation. Is this the "standard" way to do it? How would
something like this actually fix a compiler error? Wouldn't the signatures
be detected the same, thus I'd still get the error about not being a static
method?

--
Doug Thews
Director, Customer Solutions
D&D Consulting Services
----------------
Visit my Tech Blog at:
http://www.ddconsult.com/blogs/illuminati/

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Doug Thews <do*******@remo veme.ddconsult. com> wrote:
I'm writing some sample threading code, and have already got many
examples
working that look a lot similar to the ones mentioned here. What I'm
seeing
is that most people put their method that will be run as a thread inside
the
overall Form's class, which is OK for examples, but not really what you'd
see in real life.

So, I decided to write a class (called BusinessOperati on) and have
properties & methods in it. Then, I have 1 method in it called
LongRunningBusi nessOp.

So, in my Form UI code I create the thread by instantiating an object of
type BusinessOperati on and then using the fpr to the
LongRunningBusi nessOp
to start my thread.

All works well, except when I want to use delegates to update my UI back
from LongRunningBusi nessOp. For whatever reason, by moving the threaded
code outside of the Form, the compiler now says that the method (I call
it
UpdateProgressM eter) I point to when creating an instance of the delegate
(not actually defining the delegate), needs to be static. This wasn't
the
case before. Problem is, that if I make it static in the Form class,
then
it cannot access the controls within the form.


I suspect you were trying to use

new ProgressDelegat e (MyForm.UpdateP rogressMeter) where MyForm is the
name of the class. Instead, what you need is

new ProgressDelegat e (someFormInstan ce.UpdateProgre ssMeter) where
someFormInstanc e is a reference to the form you want to update.

When you instantiate BusinessOperati on, you need to pass it the form it
"belongs" to so that it knows what to update.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
BTW ... I did try it and it seemed to work. I saved off a ptr to the form's
class inside my business operation class, and then referenced it within the
create. It's interesting that the method signatures were the same, so I'm
still a little confused on why I got the compiler error (I would've expected
a null reference exception given the problem)

--
Doug Thews
Director, Customer Solutions
D&D Consulting Services
----------------
Visit my Tech Blog at:
http://www.ddconsult.com/blogs/illuminati/

"Doug Thews" <do*******@remo veme.ddconsult. com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
That sounds very plausible, and it is what I'm doing. This may be a very
stupid question, but how can I get access to the instance of the running
form when I can't pass anything to the thread that's started? I guess I
could make a public property in the BusinessOperati on class that holds the
value after I instantiate the object, and then reference it from within
the delegate creation. Is this the "standard" way to do it? How would
something like this actually fix a compiler error? Wouldn't the
signatures be detected the same, thus I'd still get the error about not
being a static method?

--
Doug Thews
Director, Customer Solutions
D&D Consulting Services
----------------
Visit my Tech Blog at:
http://www.ddconsult.com/blogs/illuminati/

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Doug Thews <do*******@remo veme.ddconsult. com> wrote:
I'm writing some sample threading code, and have already got many
examples
working that look a lot similar to the ones mentioned here. What I'm
seeing
is that most people put their method that will be run as a thread inside
the
overall Form's class, which is OK for examples, but not really what
you'd
see in real life.

So, I decided to write a class (called BusinessOperati on) and have
properties & methods in it. Then, I have 1 method in it called
LongRunningBusi nessOp.

So, in my Form UI code I create the thread by instantiating an object of
type BusinessOperati on and then using the fpr to the
LongRunningBusi nessOp
to start my thread.

All works well, except when I want to use delegates to update my UI back
from LongRunningBusi nessOp. For whatever reason, by moving the threaded
code outside of the Form, the compiler now says that the method (I call
it
UpdateProgressM eter) I point to when creating an instance of the
delegate
(not actually defining the delegate), needs to be static. This wasn't
the
case before. Problem is, that if I make it static in the Form class,
then
it cannot access the controls within the form.


I suspect you were trying to use

new ProgressDelegat e (MyForm.UpdateP rogressMeter) where MyForm is the
name of the class. Instead, what you need is

new ProgressDelegat e (someFormInstan ce.UpdateProgre ssMeter) where
someFormInstanc e is a reference to the form you want to update.

When you instantiate BusinessOperati on, you need to pass it the form it
"belongs" to so that it knows what to update.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 16 '05 #4
It is not semantics, it is OOPS. Since the method is in another class,
you need to use an object reference to pass its address to the delegate
as below

ProgressDelegat e fpr = new ProgressDelegat e(ob.UpdateProg ressMeter);

where ob is the instance of the class where you have declared the
method.

with regards,
J.V.Ravichandra n
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandra n+J.V.&cob=aspn etpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID= P3966388&BN=999 &PN=2
- Or, just search on "J.V.Ravichandr an"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #5
Doug Thews <do*******@remo veme.ddconsult. com> wrote:
That sounds very plausible, and it is what I'm doing. This may be a very
stupid question, but how can I get access to the instance of the running
form when I can't pass anything to the thread that's started?
See http://www.pobox.com/~skeet/csharp/t...arameters.html
I guess I
could make a public property in the BusinessOperati on class that holds the
value after I instantiate the object, and then reference it from within the
delegate creation. Is this the "standard" way to do it?
Well, there are lots of ways of passing parameters to methods, but
effectively you'll need a reference to the appropriate form
*somewhere*.
How would
something like this actually fix a compiler error? Wouldn't the signatures
be detected the same, thus I'd still get the error about not being a static
method?


No, because you wouldn't be trying to access it as if it *were* a
static method.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Doug Thews <do*******@remo veme.ddconsult. com> wrote:
BTW ... I did try it and it seemed to work. I saved off a ptr to the form's
class inside my business operation class, and then referenced it within the
create. It's interesting that the method signatures were the same, so I'm
still a little confused on why I got the compiler error (I would've expected
a null reference exception given the problem)


Why would you expect a NullReferenceEx ception, if the reference wasn't
null?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
Great. Thanks for the assistance everyone.

--
Doug Thews
Director, Customer Solutions
D&D Consulting Services
----------------
Visit my Tech Blog at:
http://www.ddconsult.com/blogs/illuminati/

"Ravichandr an J.V." <jv************ @yahoo.com> wrote in message
news:e2******** ******@TK2MSFTN GP12.phx.gbl...
It is not semantics, it is OOPS. Since the method is in another class,
you need to use an object reference to pass its address to the delegate
as below

ProgressDelegat e fpr = new ProgressDelegat e(ob.UpdateProg ressMeter);

where ob is the instance of the class where you have declared the
method.

with regards,
J.V.Ravichandra n
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandra n+J.V.&cob=aspn etpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID= P3966388&BN=999 &PN=2
- Or, just search on "J.V.Ravichandr an"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #8

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

Similar topics

3
2380
by: Elliot Rodriguez | last post by:
Hi: I am writing a WinForm app that contains a DataGrid control and a StatusBar control. My goal is to update the status bar using events from a separate class, as well as some other simple things. The method I am writing queries a large dataset. As part of my feedback to the user, I am updating the status bar when the connection is made and the dataset is actually retrieved. The dataset retrieval method I have placed on a separate...
2
1744
by: Don Tucker | last post by:
Hello, I am using Visual Studio 2005 .Net, coding in C#. I am working through the threading walkthrough: ms-help://MS.VSCC.v80/MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_fxmclicc/html/7bc03b7b-d680-499b-8179-5f414b2d650c.htm and have been able to get that to work as designed. However, I coded up a slight variant of that example to update the GUI while each thread is running rather than only at the end of each thread. My...
1
1230
by: RahimAsif | last post by:
Hi guys, I would like some advice on thread programming using C# so I wanted some advice. I am writing an application that communicates with a panel over ethernet, collects data and writes it to a file. The way the data is collected is that we have different schedules (so one set of data is collected say every second, another set of data might be collected every 30 seconds, and so on).
7
2912
by: jsale | last post by:
I'm currently using ASP.NET with VS2003 and SQL Server 2003. The ASP.NET app i have made is running on IIS v6 and consists of a number of pages that allow the user to read information from the database into classes, which are used throughout the application. I have made class collections which, upon reading from the DB, create an instance of the class and store the DB values in there temporarily. My problem is that if user1 looks at...
0
2948
by: Pawan Narula via DotNetMonster.com | last post by:
hi all, i'm using VB.NET and trying to code for contact management in a tree. all my contacts r saved in a text file and my C dll reads them one by one and sends to VB callback in a sync mode thread. so far so good. all contacts r added properly. now when another login adds me in his contact, i recv a subscription, so i popup a form and ask for accept/reject. this all happens in a separate thread. popup form gets opened and choice is...
3
1516
by: Keith Mills | last post by:
Hello, please find attached a basic outline of what I am attempting to accomplish... basically I want to create a number of THREADS (which I can do fine), but I then need a method for them to be able to communicate with each other, either through a message loop, or some other manner. I ALSO need to be able to CALL specific functions / subs WITHIN a thread, based on what another THREAD is doing... here is the attached code... WHEN I click...
2
1415
by: hecklar | last post by:
This is my first time posting here, so i apologize if i'm posting in the wrong subgroup or whatever, but here goes... I’m having a problem with threading and events (permissions?) in a VB.net Windows application (background service). I’m trying to write an application that processes files, launching a new thread for each file that is dropped into a certain folder. Now, the application works like a charm on my Win2000 machine, but...
9
3265
by: esakal | last post by:
Hello, I'm programming an application based on CAB infrastructure in the client side (c# .net 2005) Since my application must be sequencally, i wrote all the code in the UI thread. my problem occurs when i try to show a progress bar. The screen freezes. I know i'm not the first one to ask about it. but i'm looking
4
2973
by: MR | last post by:
I have two C#.Net projects, both of which create instances of the same C# class from a separate assembly; the class creates its own thread. When either project is run alone, it works correctly. However, when they are run simultaneously, I get a threading error (from System.Threading.ThreadHelper.ThreadStart_Context) in the project that starts last. Don't C# .Net programs run in their own process spaces? Does the fact that they both...
0
8411
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8323
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8838
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8513
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8613
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7351
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5638
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.