update label on form from within class file

J

John Devlon

Hi,

Can anyone please help me?

I'm building I small tool to process some files in a directory.

I've created a class file and added the process to the class-file.
When clicking on a button on the form, I create an object of the class and
start the process by calling the methode...

On my form, I would like a label that shows the filename of the file
currenly be processed.

I've tried raising an event form within the class but that forces the
process to stop after the first occurens...

Does aynone have an idea on how to update a label on a form from within
another class....

Thanx

John
 
P

Peter Duniho

John said:
[...]
On my form, I would like a label that shows the filename of the file
currenly be processed.

I've tried raising an event form within the class but that forces the
process to stop after the first occurens...

No, it doesn't. Raising an event doesn't in any way force the
termination of processing. It's simply a delegate invocation.

If your processing stops after raising an event, it's because of
something else you did in the code. Not the event itself.
Does aynone have an idea on how to update a label on a form from within
another class....

Raising an event is actually a good approach, if done correctly. It
encapsulates the data transfer in a way that prevents your worker class
from having to know anything at all about the Form for which it's doing
work.

Without a code example, however, it's not possible to point out what's
wrong with your attempt to implement an event, and how to fix it.
Ideally, you should post a concise-but-complete code example that
reliably demonstrates the problem. But at a minimum, you need to post
the relevant code from your worker class, as well as the event handler
in your Form class that is handling the event in the worker class.

Pete
 
G

Gregory A. Beamer

I've tried raising an event form within the class but that forces the
process to stop after the first occurens...

It should not. Something in your event handler is stalling things and
that is where the problem is.

If you have separation of concerns, the windows form is a UI element. As
such, the application (business) classes should NOT control the form
directly. They should, instead, inform the UI of changes and have the UI
handle it.

Can you have a class update a form? Sure, pass the form to the class. It
can solve YOUR problem, but presents others.

1. The application is now tightly bound to a windows forms UI, with no
easy way of changing out UI

2. You can still end up with a stalled application if the class does not
let go of the form when it is done.

3. You end up having to define the form outside of the UI so both
projects can share it, making the UI a business layer library.

All in all nastiness, tight coupling, combination of concerns, etc.

I would look at your event handler and find how you are locking things
up.

Peace and Grace,

--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
J

John Devlon

Hi Pete and Gregory,

The last few day's, I was working on my problem and I noticed something
strang... When I launch my methode wich envokes my event, It stops after the
first time the event was raised...

When I remove the object from the tread I use, everything works fine....


this doesn't work...

clsBackup myBackup = new clsBackup(myList);
myBackup.NotifyOfProcessed += new ChangingHandler(FileUpdateEvent);

Thread myThread2 = new Thread(myBackup.startBackup);
myThread2.Start();

private void FileUpdateEvent(string strFilename) {
lblFileName.Text = strFilename;
}


this works....

clsBackup myBackup = new clsBackup(myList);
myBackup.NotifyOfProcessed += new ChangingHandler(FileUpdateEvent);

myBackup.startBackup();

private void FileUpdateEvent(string strFilename) {
lblFileName.Text = strFilename;
}


Does anyone know way ?

Thanx

John
 
P

Peter Duniho

John said:
[...]
this doesn't work...

clsBackup myBackup = new clsBackup(myList);
myBackup.NotifyOfProcessed += new ChangingHandler(FileUpdateEvent);

Thread myThread2 = new Thread(myBackup.startBackup);
myThread2.Start();

private void FileUpdateEvent(string strFilename) {
lblFileName.Text = strFilename;
}

If you run the above in the debugger, you will see a "invalid
cross-thread operation" exception in the event handler.

Most likely, you just wrap the assignment in a Control.Invoke() call, it
will work fine:

private void FileUpdateEvent(string strFilename) {
lblFileName.Invoke((MethodInvoker)delegate
{
lblFileName.Text = strFilename;
});
}

If that doesn't fix your problem, then you really need to post a
concise-but-complete code example, just as I suggested in my previous reply.

Pete
 
G

Gregory A. Beamer

Does anyone know way ?

I think Peter did a good job of explaining the why, so I am just going
to add something about the thought process.

When you start spinning off threads, you have to pay attention to what
thread has control of what. This gets a bit more difficult when you are
talking bits controlled by .NET.

In general, anything that spins off a thread to control the UI is a
dangerous operation (only briefly examined the code, so I am not sure
you are doing this, plus it is incomplete), as the main thread controls
the UI. This is not a "completely true" statement, but a good rule of
thumb. The takeaway is this: Unless you wish to circumvent or rewrite
sections of the framework, you have to respect its rules (or perhaps
limitations).

In most cases, if you can accomplish something without multi-threading,
that is your best solution. Notice "most cases", as there are always
exceptions to every rule (except perhaps, jumping from the death zone on
Mount Everest guarantees you will die). ;-)

Peace and Grace,

--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top