|
Thats excellent, I shall have a little play around and see what I can do with it. I have spent the past few years programming in Delphi and not really had to deal with threads, this is all taking me back to my days at university... the brain has slowed down a bit since then, doesnt like new concepts anymore.
Thanks for your help
|
|
|
|
|
Threading can be a right pain i nthe arse :P
But since it's used so much in the UI, MS have made it a bit easier up at the front end
|
|
|
|
|
Hi,
I have been playing around with the backgroundworker again, I get the following errors when I try using InvokeRequired and Invoke in the backgroundworker DoWork code
Error 1 The best overloaded method match for 'System.Windows.Forms.Control.Invoke(System.Delegate)' has some invalid arguments
Error 2 Argument '1': cannot convert from 'int' to 'System.Delegate'
if (cboComments.InvokeRequired)
cboComments.Invoke(cboComments.Items.Add(tempString));
else
cboComments.Items.Add(tempString));
What am i doing wrong? I need a brain upgrade!
Thanks for you time
|
|
|
|
|
ahhhh dang it. In this case the method we're calling doesn't conform to the System.Delegate, which wants a void Method();.
Add this code to your class:
<br />
private delegate void AddItemDelegate(object item);<br />
<br />
private void AddComboBoxItem(object item)<br />
{<br />
cboComments.Items.Add(item);<br />
}<br />
see how the delegate defines the method signature of the method?
private delegate "void AddItemDelegate(object item)"
private "void AddComboBoxItem(object item)"
And change your Invoke method call to:
<br />
cboComments.Invoke(new AddItemDelegate(AddComboBoxItem), item);<br />
this creates a new delegate of our specific definition, tells it to point at our 'AddComboBoxItem' method and passes item as a parameter.
The reason you have to use delegates and Invoke is that now you have said ... I want to use this method please, and the control will execute that method on the appropriate thread (i.e. the UI one), which prevents the cross threading issues.
|
|
|
|
|
Karlos_V wrote: I have looked at background workers but that leads to cross threading.
Control.CheckForIllegalCrossThreadCalls = false;
Although not healthy, it will do the trick.
Smile: A curve that can set a lot of things straight!
(\ /)
(O.o)
(><)
|
|
|
|
|
yeah might was well switch to VB, turn off option strict and use goto error :P
The check is there for a reason, as you say it's not healthy and so theres no point even metioning it. It's also much harder to debug threading bugs than to explain how to do things correctly in the first place!
|
|
|
|
|
originSH wrote: well switch to VB
That should do it
Smile: A curve that can set a lot of things straight!
(\ /)
(O.o)
(><)
|
|
|
|
|
Why would you tell a newbie that information? My team leader's head would explode if I tried anything like that.
|
|
|
|
|
Oops, sorry
Smile: A curve that can set a lot of things straight!
(\ /)
(O.o)
(><)
|
|
|
|
|
in case of normal listbox it is possible with the usage of "IndexFromPoint" method but for checked list box there is no such method. So pls can any one tell me how to proceed
string strTip = "";
//Get the item
int nIdx = lblRecords.IndexFromPoint(e.Location);
if ((nIdx >= 0) && (nIdx < lblRecords.Items.Count))
strTip = lblRecords.Items[nIdx].ToString();
toolTip1.SetToolTip(lblRecords, strTip);
Thanks in advance.
|
|
|
|
|
I have a webapp where users can search for PDF-documents in database. They enter the searchcritera in a textbox and presses search. All that works fine but I have problem with clearing the textbox after pressing the searchbutton. The code for the button is:
protected void btnSearchNumber_Click(object sender, EventArgs e)
{
try
{
//readPDF gets the document from database
readPdf(TextBox1.Text);
TextBox1.Text = "";
}
catch
{
lblNoPDF.Text = "NO PDF-DOCUMENT FOR GIVEN MRB WAS FOUND";
TextBox1.Text = "";
}
}
If no document was found the catchblock successfully clears the textbox. But if document successfully opens, the textbox is not cleared. I have also tried to clear the textbox in a finally block with no success. There is probably a simpel solution to this but I havn't found it.
Anyone who can help?
Gitsu
|
|
|
|
|
Your code is obviously not getting past the readPdf so I would guess that the method isn't returning an expected value if successful.
If the readPdf method isn't something you can change, you could copy the TextBox1.Text value to a string variable at the top of the try block, clear the text box then call the readPdf method passing the string.
I'd alter the readPdf method if possible though as things are not going to be disposing properly otherwise.
Dave
|
|
|
|
|
I have tried copying the text to a string, but that doesn´t work either. The readPDF method is:
void readPdf(string mrb)
{
try
{
DataRow row = handler.getReport(mrb).Tables[0].Rows[0];
Byte[] pdfData = (byte[])row["reportFile"];
int mrbID = (int)row["mrbID"];
Response.ContentType = "application/PDF";
Response.AddHeader("content-disposition", "attachment; filename = " + mrbID + ".pdf");
Response.BinaryWrite(pdfData);
}
catch
{
throw;
}
}
Can you see if there is something wrong here? The method returns the PDFfile successfully if it exists.
|
|
|
|
|
It looks fine to me
This should work to get you out of trouble:
protected void btnSearchNumber_Click(object sender, EventArgs e)
{
try
{
string thisPDF = TextBox1.Text;
TextBox1.Text = "";
System.Windows.Forms.Application.DoEvents();
//readPDF gets the document from database
readPdf(thisPDF);
}
catch
{
lblNoPDF.Text = "NO PDF-DOCUMENT FOR GIVEN MRB WAS FOUND";
}
}
but I would like to know why it doesn't work in your original code!
Maybe, as an experiment, try returning a bool value from the readPdf
bool readPdf(string mrb)
{
try
{
DataRow row = handler.getReport(mrb).Tables[0].Rows[0];
Byte[] pdfData = (byte[])row["reportFile"];
int mrbID = (int)row["mrbID"];
Response.ContentType = "application/PDF";
Response.AddHeader("content-disposition", "attachment; filename = " + mrbID + ".pdf");
Response.BinaryWrite(pdfData);
return true;
}
catch
{
throw;
return false;
}
}
and show the result in a message box
protected void btnSearchNumber_Click(object sender, EventArgs e)
{
bool result;
try
{
//readPDF gets the document from database
result = readPdf(TextBox1.Text);
System.Windows.Forms.MessageBox.Show(result.ToString());
TextBox1.Text = "";
}
catch
{
lblNoPDF.Text = "NO PDF-DOCUMENT FOR GIVEN MRB WAS FOUND";
TextBox1.Text = "";
}
}
If you don't get a message box when the readPdf is successful then there's something seriously screwy going on somewhere.
|
|
|
|
|
Just noticed you said you're creating a web app so ignore the above as I don't think there's any equivalent in the System.Web namespce.
|
|
|
|
|
Hi all,
This week I’ve been doing research on wcf but I’m a bit confused heres my question ....
If I have a wcf service with http transport containing a method called send mail that method uses system.net mail class to send email via smtp wont it be using the underlying mechanics to send an email using smtp via tcp.
|
|
|
|
|
The System.Net.Mail class has nothing to do with WCF as far as I know and so will do it's usual SMPT over TCP thing.
The WCF transport only applies to the WCF stuff.
|
|
|
|
|
Thank you that makes sense now,
|
|
|
|
|
Hi all,
Right we have application using Microsoft Visual C# 2.0.
Application divided by two programs
1. Win forms
2. Console Application.
We already completed that program using C# 2.0.
Console application, I like to run as a windows service or command line. (It’s depends on the user option).
How can I run a console application as command line or windows service?
[I don’t want separate EXE for console or windows service, I need to run one EXE as a Console or Windows Service).
How can I implement this functionality?
Kindly let me know the technical idea or sample code for that.
Thank you,
Sundar.
B.Sundarapandian
|
|
|
|
|
It would probably be easier to have 2 seperate apps that just use the same code from a shared lib.
I really think it would be more trouble than it's worth trying to have the single exe, and for very little gain.
A service needs to be installed, so the application would have to install itself depending on the supplied options, you'd probably also need to add in code to ensure that multiple copies can't be run and more code to handle the controlling of the service. All of which can be easily avoided.
|
|
|
|
|
|
Hi,
Thanks for prompt reply.
I have suggested the same opinion to my mentor (client).
But he wants one single program, run as a command line or windows service.
Already my client developed the console application run a command line. Now I am making the enhancement for that application.
Thank you.
B.Sundarapandian
|
|
|
|
|
Why not make two separate dll files, one is for command line and the other a service. Then all the exe has to do is determine which one to run, then invoke the proper dll. It meets your customer's spec of only having one exe, while making implementing it easy on you.
|
|
|
|
|
Hi guys!
I'm trying to install a printer driver via WMI and C#
I can invoke the method but always get a "87" (invalid parameter) as return value.
But I have absolutly no idea what could be wrong with the parameter (I suppose that it's the "DriverInfo" param).
<br />
ManagementClass classInstance = new ManagementClass("root\\CIMV2", "Win32_PrinterDriver", null);<br />
<br />
ManagementObject driverInfo = classInstance.CreateInstance();<br />
driverInfo["Name"] = "Adobe Postscript 5";<br />
driverInfo["Version"] = "3";<br />
driverInfo["SupportedPlatform"] = "Windows NT x86";<br />
driverInfo["DataFile"] = "C:\\Temp\\adobe\\DEFPRTR2.PPD";<br />
driverInfo["ConfigFile"] = "C:\\Temp\\adobe\\PS5UI.DLL";<br />
driverInfo["DriverPath"] = "C:\\Temp\\adobe\\PSCRIPT5.DLL";<br />
driverInfo["HelpFile"] = "C:\\Temp\\adobe\\PSCRIPT.HLP";<br />
string[] depfiles = {"C:\\Temp\\adobe\\PSCRIPT.NTF", "C:\\Temp\\adobe\\PSCRPTFE.NTF"};<br />
driverInfo["DependentFiles"] = depfiles;<br />
<br />
ManagementBaseObject inParams = classInstance.GetMethodParameters("AddPrinterDriver");<br />
<br />
inParams["DriverInfo"] = driverInfo;<br />
<br />
ManagementBaseObject outParams = classInstance.InvokeMethod("AddPrinterDriver", inParams, null);<br />
<br />
Console.WriteLine("Out parameters:");<br />
Console.WriteLine("ReturnValue: " + outParams["ReturnValue"]);<br />
Perhaps someone can help me?
|
|
|
|
|
I'm working on some peer-to-peer stuff and I need to make this service available to those behind routers. Two things need to be accomplished:
1. Detect whether or not the user is behind a router.
2. If the user is behind a router, forward port X to the user's network IP.
I've seen this done using COM and C, but I can't translate into C#.
Thanks!
|
|
|
|