Dear kfrost,
from what you've written in your last post I understand you need to update
your TextBox during execution of some xml processing. Correct me if I'm
wrong please. You were also asking how to accomplish this if you don't want
to pass a reference to that TextBox to the class that does the processing.
One possible solution is using events.
Please don't take this example absolutely strict, it just shows how to do it
in principle, not exactly in your case.
Please try to modify it to suit your concrete needs, it shows just how to
"do events" not exactly how you should do it. Refer to [1] or [2] if you
need more help on events and delegates.
[1]
http://www.c-sharpcorner.com/Code/2003/Nov/EventsinNet.asp
[2]
http://www.google.com
public delegate void NotificationHandler(object sender,
NotificationEventArgs e);
public class NotificationEventArgs : System.EventArgs
{
private string message;
public string Message { get {return message;} }
public NotificationEventArgs(string message)
{
this.message = message;
}
}
public class ClassWithNotifications
{
public event NotificationHandler Notification;
public void DoSomeProcessing()
{
//now you get some data that you need to show in the TextBox.
OnNotification("We have some data: " + data.ToString());
//do some other processing
OnNotification("Now the processing is over");
}
private void OnNotification(string message)
{
if (Notification != null)
{
Notification(this, new NotificationEventArgs(message);
}
}
}
usage:
on your form, let's say class MainForm
class MainForm
{
private ClassWithNotifications c;
public MainForm()
{
InitializeComponentsIfYouWish();
c = new ClassWithNotifications();
c.Notification += new
NotificationHandler(this.ShowNotificationInTextBox);
}
private void someButton_Click(object sender, EventArgs e)
{
c.DoSomeProcessing();
}
private void ShowNotificationInTextBox(object sender,
NotificationEventArgs e)
{
this.textBoxWhichIsSupposedToShowNotifications.Text = e.Message;
}
}
kfrost said:
Lebesque,
Thank you. I think you've hit it on your last set of comments. That is
what I'm doing because I need to call the delegate from from a class inside
say the XmlApi. With my limited knowlege of delegates and multithreading,
that was the one way I could come up with so far.
To save anyone responding, I'll lay out this scenario for some hopeful
input. Lebesgue, sorry for switching things up on you here, but I'm going to
use the names I am in the app so I hopefully don't miss anything like I did
in the beginning of this thread. (I'm going to cut out a lot of irrelevent
code but the same naming structure.)
namespace SBASynch
{
public class Form1 : System.Windows.Forms.Form
private System.Windows.Forms.TextBox txtResults;
private System.Windows.Forms.Button btnSynch;
public delegate void DisplayStringDelegate(string strText);
this.txtResults = new System.Windows.Forms.TextBox();
this.btnSynch = new System.Windows.Forms.btnSynch();
private void btn_Click(object sender, System.EventArgs e)
{
Thread WorkThread = new Thread(new ThreadStart(CopyInvoices));
WorkThread.IsBackground = true;
WorkThread.Start():
}
private void CopyInvoices()
{
// I left out the 2 text boxes initialization code above to be concise.
string serverName = txtServerName.Text;
string databaseName = txtDBName.Text;
try
{
using (ListingQuotes getQuotes = new ListingQuotes(serverName,
clientName, databaseName)
{
getQuotes.RunQuoteView();
}
catch(Exception ex)
{
}
public void DisplayString(string strText)
{
BeginInvoke(new DisplayStringDelegate(DisplayString), new object[]
{strText});
txtResults.Text += strText + "\r\n";
}
}
class ListingQuotes : IDisposable
{
Public void RunQuoteView()
{
Form1 cForm1 = new Form1();
cForm1.DisplayString("Delegate Called");
}
}
Keep in mind that the ListingQuotes is a class from some SDK examples and in
the RunQuoteView(), most of the code in the application is ran.
Multiple times in this codes at different points I need to update controls
immediately such as the textbox which is initialized in the Form1 class.
The code you see in the Form1 btnSynch.Click most of the code that runs int
the form class and execution will not return to there until the app is
finished. That's what led to my original post. Now the only thing I've
found to work is pass the txtResult object to RunQuotesNow().
But I've kind of got stuck on trying to figure out how to update the textbox
from Form1 as you and Paul have suggested. So that's what got me on this
delegation. So again, from what you stated, the way I'm calling it is the
problem but how else would you go about doing this?
Thanks again for your input!