|
it would be a mess if i do it with global variables. it would be my last resort. if it ever is.
|
|
|
|
|
You don't have to use global data. Instaed, you can create a wrapper class which contains your thread function and all other related methods and data members. The only constraint is that all of the methods and data members should be static. The following C++ code snippets are extracted from my CP article http://www.codeproject.com/useritems/xlogoff.asp[^]. My thread function processes lots of data within the class. One thing to note is that I used mutex to protect the data accessed by more than one thread.
Here is the OnStart() in my Windows service code, which simply starts a thread:
void OnStart(String* args[])
{
Thread *oThread = new Thread(new ThreadStart(0, &XLogoffThread::ThreadProc));
oThread->Start();
}
Here is the thread class which contains a thread function as the delegate function to ThreadStart and all other methods and data:
public __gc class XLogoffThread
{
private: static String* prevSession;
private: static String* DATA_FILE = S"session.dat";
private: static ArrayList* baseline = new ArrayList;
public: static Mutex * mut = new Mutex();
private: static SnippetsCPU::ProcessAsUser *cpau = new SnippetsCPU::ProcessAsUser();
public: static void ThreadProc()
{
try
{
if ( !File::Exists(DATA_FILE) )
{
File::Create(DATA_FILE);
}
Int32 port = 30000;
IPAddress* localAddr = IPAddress::Parse(S"127.0.0.1");
TcpListener* server = new TcpListener(localAddr, port);
Byte bytes[] = new Byte[128];
server->Start();
while (true)
{
TcpClient* client = server->AcceptTcpClient();
String* data = 0;
NetworkStream* stream = client->GetStream();
do
{
Int32 i = stream->Read(bytes, 0, bytes->Length);
data = Text::Encoding::ASCII->GetString(bytes, 0, i);
ProcessCmd(data);
} while (stream->DataAvailable);
client->Close();
}
} catch (SocketException* except)
{
MessageBox::Show( except->get_Message(), "XLogoff Service Error" );
}
}
}
Hope this helps.
Best,
Jun
|
|
|
|
|
thanx alot buddy. i will try it out.
but one thing about ur code of class XLogoffThread. it never use those static properties in the ThreadProc(). so whats the use of those fields. i asked because thats the concept iam going to implement.
|
|
|
|
|
ProcessCmd(data) and its delegates use these data. Most of the code has been omitted in the post. You might want to grab the complete code following the link in that post.
Best,
Jun
|
|
|
|
|
why don't you just use a delegate?!
public delegate void Name(object param1, object param2);
private void MyMethod(object param1, object param2)
{
// do what you need.
}
then call
Name n = new Name(MyMethod);
// control being a listView or form etc.
control.Invoke(n, new object[] { "test", "test2" });
|
|
|
|
|
yeh but annoymus delegate wont work with .Net 1.1. i have that one.
there got to be other solutions for this version as well.
|
|
|
|
|
i got ur point and i have already transformed my code in way u said. its doing great. thanx buddy
|
|
|
|
|
|
i meant golab in a class. its bit vague term but i use it for public members of a class.
|
|
|
|
|
I am trying to implement the sorting of a ListView object by clicking on a column (using the example at: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwinforms/html/listviewsort.asp[^]). While debugging it I get the following exception:
An unhandled exception of type 'System.OutOfMemoryException' occurred in System.Windows.Forms.dll
Additional information: Error creating window handle.
It is occuring at any line where I try to set the listView.Sorting property. For example:
this.listView.Sorting = SortOrder.Ascending;
The moment I comment this line out, the exception goes away. I was wondering if anyone had encountered this before or knew what is causing this.
Thanks,
Steve
|
|
|
|
|
SteveZWI wrote: An unhandled exception of type 'System.OutOfMemoryException' occurred in System.Windows.Forms.dll
I've seen that exception when dealing WinForms controls if the data object associated with an item in the list returns null from ToString(). HTH
:josh:
My WPF Blog[^]
|
|
|
|
|
Hello,
I am wondering if it's possible to make TreeView control accept multi-selection in .NET 2.0.
Please advise. Thanks.
|
|
|
|
|
Not using the standard control, no, you can't. Only one leaf, or node, of the TreeView can be selected at any one time. But, there's nothing that says that each node has to be a single string. Search the articles for "TreeListView" and you'll find a article that implements a TreeView wil ListView nodes. It might be what you're looking for.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
This is part 2 of wanting to send more than one parameter to Response.Redirect. I got a response on using the & operator, but since I'm using strings I kept getting errors. This is what I currently have:
Response.Redirect ("Add_Weight.aspx?IDOutput3=" + IDOutput3 + "strPartNumberInputReference=" + strPartNumberInputReference);
Which isn't right because it just smooshing everything together. Which make sense really how I'm looking at it, but I can't figure out the correct syntax. I swear I'm trying.
|
|
|
|
|
Try
Response.Redirect ("Add_Weight.aspx?IDOutput3=" + IDOutput3 + "&strPartNumberInputReference=" + strPartNumberInputReference);
A man said to the universe:
"Sir I exist!"
"However," replied the Universe, "The fact has not created in me A sense of obligation."
-- Stephen Crane
|
|
|
|
|
|
I have a Winform app that I need to be able to send an email from with attachments. I don't want to use SMTP.Client for various reasons. I want to do the equivelant of mailto: someone@somewhere.com?subject=my subject&body=this is my message. I can do this using System.Diagnostic.Process.Start("mailto: someone@somewhere.com?subject=my subject&body=this is my message"); It is not pretty, but it works. I cannot assume Outlook is installed so I cannot use office automation.
The problem is I would like to attach a file to the email window that is opened when doing this. Winzip does this when choosing "Compress and email" from their context menu. I don't know if that option only shows up if you have outlook or if it works for other clients. If it works for other clients, that is what I am trying to do.
Anybody have any ideas on how they are doing this? Can it be done to work without knowing the email client installed?
Thanks.
|
|
|
|
|
Then you will have to list a very good reason. System.Net.Mail.SmtpClient seems aptly suited. You could use System.Net.TcpClient instead but it would be more tedious. BTW, mailto:, does not work according to your specs at it requires outlook to be installed and configured. (or another email client)
A man said to the universe:
"Sir I exist!"
"However," replied the Universe, "The fact has not created in me A sense of obligation."
-- Stephen Crane
|
|
|
|
|
System.Net.Mail.SmtpClient would require my application to store UserID, Password, Server, Port, etc. to send an email using an SMTP server. Additionally, this would bypass their mail client and I would be responsible for keeping sent messages, etc. I was trying to avoid building an email client into my application and the complexities of configuring that for the end user. That is the very good reason.
I am trying to leverage the existing client and configuration that most likely already exists. mailto is working fine as described (as long as they have an email client installed). I am trying to figure out how to extend that functionality and attach a file to the opened email message window programmatically.
As mentioned, similar to Winzip "Compress and email". They may be using Outlook automation to do that. Anyone using Winzip something other than outlook or outlook express for an email client? If so, let me know if Winzip still provides that option and how it works.
Thanks.
|
|
|
|
|
May be available through a windows interface. You can most definately get the email settings from outlook. For Thunderbird, I dunno. One option with the mailto: would be sending the attachment in the body using UUEncoding however I imagine the console has a character limit.
A man said to the universe:
"Sir I exist!"
"However," replied the Universe, "The fact has not created in me A sense of obligation."
-- Stephen Crane
|
|
|
|
|
|
I am new to C# and have a class project to work on. I am trying to create an Amortization calulator the display the payment results based on the Time period thay have to repay the loan. The calculation part of it is working fine but I can not get it to display the result. Can I please get some help with this? See code Below:
<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
Label6.Text = "Final Balance: " +
CalculateBalance(Convert.ToInt32(TextBox1.Text),
(Convert.ToDouble(TextBox2.Text) / 100),
Convert.ToInt32(TextBox3.Text),
Convert.ToInt16(DropDownList1.SelectedItem.Value)).ToString();
}
private string CalculateBalance(int Principal, double Rate, int Years, int Period)
{
double result;
double NumToBeRaised = (1 + Rate / Period);
result=Principal * System.Math.Pow(NumToBeRaised, (Years * Period));
return (result.ToString("C"));
}
private string AmortDisplay(double Principal, double results, int Years, int Period)
{
int AmortTime;
double AmortPayment;
double RemBalance;
string Output;
AmortTime = (Years * Period);
AmortPayment = (results / AmortTime);
RemBalance = -(Principal - AmortPayment);
TextBox4.Text = "Duration\t \tPayment\t \tRemaining Balance \n" + AmortTime + "\t\t" + Convert.ToString(AmortPayment) + "\t\t" + Convert.ToString(RemBalance) + "\n";
}
protected void Button2_Click(object sender, EventArgs e)
{
AmortDisplay(Convert.ToDouble(TextBox1.Text),
Convert.ToDouble(Label6.Text),
Convert.ToInt32(TextBox3.Text),
Convert.ToInt16(DropDownList1.SelectedItem.Value)).ToString();
}
</script>
Greg
The Belizean Dan Dada!!!
|
|
|
|
|
Skanless wrote: Can I please get some help with this?
Someone might help you, especially if you post a question or problem instead of just saying "I can not get it to display the result."
:josh:
My WPF Blog[^]
|
|
|
|
|
this is a re-post. I would suggest that you read 'how to get an answer to your question' at the top of the forum. I am sure that someone could answer a question for you if you ventured to actually ask a question, provide some detail, etc...
cje
|
|
|
|
|
The code you posted will not compile. The function AmortDisplay, as written, does not return a value but is declared as returning a string.
In the calculation of Textbox4.text, AmortTime is not converted to a string.
This would be better done using String.Format anyway.
There is no error handling (try...catch) to capture conversion errors,
so any exception will just propagate up to the top and crash...
Why do you convert the result of CalculateBalance nto a string
(as the return value) and then immediatly convert it back
to a double in order to pass it to AmmortDisplay?
Clean up your code. avoid conversion operations in the parameters passed
to a function (do these as separate steps with local values,
pass the local values) - this:
AmortDisplay(Convert.ToDouble(TextBox1.Text),
Convert.ToDouble(Label6.Text),
Convert.ToInt32(TextBox3.Text),
Convert.ToInt16(DropDownList1.SelectedItem.Value)).ToString();
will be very hard to debug, and there are at least 5 opportunities
for a mistyped (or empty) entry field to cause an exception to be thrown
in that single function call.
Why the .ToString() at the end? the function called should have returned a string
(and would have, but you left out the return statement) anyway..
You definately need to study harder. This is really poor code, and would be unacceptable
in any production work.
Some advice:
1. Keep it simple, avoid complex function calls with nested calls
2. do things in clear descrete steps so you can inspect each easily.
3. don't change data types any earlier than necessary.
4. inspect your code with the presumption that there is a stupid error or oversight,
the best of us still make these occaisionally.
|
|
|
|