Click here to Skip to main content
15,920,438 members
Home / Discussions / C#
   

C#

 
GeneralRe: Report Progress from a class with a backgroundWorker Pin
MacRaider414-Feb-11 7:57
MacRaider414-Feb-11 7:57 
GeneralRe: Report Progress from a class with a backgroundWorker Pin
Luc Pattyn14-Feb-11 8:13
sitebuilderLuc Pattyn14-Feb-11 8:13 
GeneralRe: Report Progress from a class with a backgroundWorker Pin
MacRaider414-Feb-11 8:21
MacRaider414-Feb-11 8:21 
AnswerRe: Report Progress from a class with a backgroundWorker Pin
Ian Shlasko14-Feb-11 8:11
Ian Shlasko14-Feb-11 8:11 
GeneralRe: Report Progress from a class with a backgroundWorker Pin
MacRaider414-Feb-11 8:28
MacRaider414-Feb-11 8:28 
GeneralRe: Report Progress from a class with a backgroundWorker Pin
Ian Shlasko14-Feb-11 9:31
Ian Shlasko14-Feb-11 9:31 
GeneralRe: Report Progress from a class with a backgroundWorker Pin
MacRaider414-Feb-11 9:46
MacRaider414-Feb-11 9:46 
GeneralRe: Report Progress from a class with a backgroundWorker Pin
Ian Shlasko14-Feb-11 12:11
Ian Shlasko14-Feb-11 12:11 
MacRaider4 wrote:
(as if it were a bgw perhaps in a thread you can use the variable directally)?

Nope, that limitation is universal to all types of multithreading in WinForms (And WPF, for that matter). No touching the GUI controls except from the GUI thread.
MacRaider4 wrote:
Plus when I was getting into more advanced someone reccomended keeping as little as you can on your main form and using classes for as much as possible. Is this a good habbit, bad habbit or just personal preference?

That's generally a good idea, except in really small tools when it would be complete overkill. The form is your connection to the GUI, and business logic goes elsewhere... In your particular situation, I think I would probably handle it something like this:

(Consider this C#ish pseudocode, as I don't have VS installed on my home machine at the moment)
// This can go in a static class somewhere
public static void SendMessagesAsync(ProgressChangedHandler progressCallback, params SomeClass[] messages)
{
  for (int idx = 0; idx < messages.Length; idx++)
  {
    BackgroundWorker wkr = new BackgroundWorker();
    wkr.WorkerSupportsProgress = true;
    wkr.ProgressChanged += progressCallback;  // This will go back to your main form and display something
    wkr.DoWorkEventArgs += wkr_DoWork;  // This is inside the same static class, below, not part of its public interface
    wkr.RunWorkerAsync(messages[idx]);
  }
}

// All of your workers can use the same DoWork, as long as they're given different arguments
private static void wkr_DoWork(object sender, DoWorkEventArgs e)
{
  BackgroundWorker wkr = sender as BackgroundWorker; // Use this to report progress
  SomeClass args = e.Argument as SomeClass;  // Here's any info you need to pass to the worker

  // ... Do your processing here ... WriteMail, or whatever
}

// And on your form...

private void SomethingCalledFromYourForm()
{
  MyStaticClass.SendMessagesAsync(wkr_ProgressChanged, new SomeClass[] {
    new SomeClass() { Whatever = parameters, YouWant = toset },
    new SomeClass() { Subject = someone, Body = "something else?", Recipient = "someone@somewhere.sometime" }
  });
}

I don't know what you need to pass to the WriteMail function, but that's generally how you get information into a background worker... Make a class to hold it, and pass it as the argument to RunWorkerAsync().
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels)

GeneralRe: Report Progress from a class with a backgroundWorker Pin
DaveyM6914-Feb-11 11:24
professionalDaveyM6914-Feb-11 11:24 
QuestionSet a picture transparently in the foreground Pin
Mschauder14-Feb-11 2:21
Mschauder14-Feb-11 2:21 
AnswerRe: Set a picture transparently in the foreground Pin
musefan14-Feb-11 3:03
musefan14-Feb-11 3:03 
GeneralRe: Set a picture transparently in the foreground Pin
Mschauder14-Feb-11 3:15
Mschauder14-Feb-11 3:15 
GeneralRe: Set a picture transparently in the foreground Pin
musefan14-Feb-11 3:42
musefan14-Feb-11 3:42 
GeneralRe: Set a picture transparently in the foreground Pin
Mschauder14-Feb-11 22:08
Mschauder14-Feb-11 22:08 
Questiondirectory path Pin
arkiboys13-Feb-11 22:12
arkiboys13-Feb-11 22:12 
AnswerRe: directory path Pin
Pete O'Hanlon13-Feb-11 22:17
mvePete O'Hanlon13-Feb-11 22:17 
GeneralRe: directory path Pin
arkiboys13-Feb-11 22:23
arkiboys13-Feb-11 22:23 
GeneralRe: directory path Pin
Pete O'Hanlon13-Feb-11 22:45
mvePete O'Hanlon13-Feb-11 22:45 
GeneralRe: directory path Pin
arkiboys13-Feb-11 23:22
arkiboys13-Feb-11 23:22 
AnswerRe: directory path Pin
Dalek Dave14-Feb-11 0:14
professionalDalek Dave14-Feb-11 0:14 
AnswerRe: directory path Pin
GenJerDan14-Feb-11 0:29
GenJerDan14-Feb-11 0:29 
GeneralRe: directory path Pin
Pete O'Hanlon14-Feb-11 0:34
mvePete O'Hanlon14-Feb-11 0:34 
AnswerRe: directory path Pin
Luc Pattyn14-Feb-11 1:01
sitebuilderLuc Pattyn14-Feb-11 1:01 
AnswerRe: directory path Pin
PIEBALDconsult14-Feb-11 2:00
mvePIEBALDconsult14-Feb-11 2:00 
QuestionCon open wait Pin
Ajay Kale New13-Feb-11 22:01
Ajay Kale New13-Feb-11 22:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.