|
If you're using .Net 4, look at the Task Parallel Library. It is designed specifically for running parallel tasks which is what you appear to be trying to do here.
In the old days I would create a thread (now I'd name it task, but this comes from the Java model where I first learnt multithreading) class that stored the state of a particular operation:
class MyTask {
double a,b;
Thread t;
ManualResetEvent wh;
ManualResetEvent WaitHandle { get { return wh; } }
public Thread Thread { get { return t; } }
MyTask(double a, double b) {
this.a = a; this.b = b;
t = new Thread(new ThreadStart(Run));
wh = new ManualResetEvent();
}
private void Run(){
wh.Signal();
}
}
... and call it like
Thread thread = new MyTask(11, 15).Thread;
thread.Start();
or, if you care about the wait handle:
MyTask task = new MyTask(11, 15);
task.Thread.Start();
task.WaitHandle.WaitOne();
But I do stress that the TPL will do all this stuff fairly nicely without requiring you to write code.
|
|
|
|
|
thanks a lot for your answer
I will study TLP. It first sight it looks really very nice
|
|
|
|
|
Hi there
Im pretty new to C# and im having a real problem when i getting data from my MySql Database.
Firstly I read the data in to a Datatable then i read it in to a Dictionary(Like vb Collection) however when i tried to convert it straight to a string i comes out as DBNULL this was pretty annoying!!
so to get round it it tried conveting it to datetime first and then to string which worked as long as im stepping over, then when i ran it normally i got
'Object cannot be cast from DBNull to other types.'
this is really annoying
when it brings me back in to the code i can see that it has taken the data in the datetime var but still kick up the error
if (datatype == "MySql.Data.Types.MySqlDateTime")
{
DateTime Ddata = Convert.ToDateTime(dt.Rows[0][colname]);
data = Convert.ToString(Ddata);
}
else
{
data = Convert.ToString(dt.Rows[0][colname]);
}
I'll be real grateful if someone can help with this as im Stumped
Regards Ian
|
|
|
|
|
I guess that the field in the database can contain a null value, and whenever you get the error, you hit a row where that column actually was null. Hence check first:
if (dt.Rows[0][colname] != DBNull.Value) ...
|
|
|
|
|
Hi Bernhard
Thanks for your reply
well in this instance the code is only getting one row.
the database is set up to accept nulls but for this record i know there is a value there.
when i look at the datatable in dataset visualizer (Whilst de-bugging) i can see that the value is there.
i do a check very simerlar to your lower down but that just disregards the date and puts it as null. I only wrote that if statement to catch MySQL dates as they didn't seem to be working, i figured if i converted to a date first that it would be ok.
I was wrong
Ian
|
|
|
|
|
... and when it is a DateTime, just cast it, no need to convert:
System.DateTime dt = (System.DateTime) dt.Rows[0][colname] ;
Alternatively, if you really want the date in a string (which you shouldn't until you output it, just use ToString:
string dt = dt.Rows[0][colname].ToString() ;
or, to output to the console:
System.Console.WriteLine ( "DateTime={0}" , dt.Rows[0][colname] ) ;
In summary: Don't ever use the Convert class -- except for the ChangeType method when necessary.
|
|
|
|
|
Awesome PIEBALDconsult
that worked a treat thanks soo much.
Ian
|
|
|
|
|
PIEBALDconsult wrote: Don't ever use the Convert class
I've seen you say this before. Why is that the case?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Because it's needless, nearly all its methods simply wrap other methods (e.g. Int32.Parse). It offers no benefit and adds an extra method call.
|
|
|
|
|
PIEBALDconsult wrote: Because it's needless, nearly all its methods simply wrap other methods (e.g. Int32.Parse). It offers no benefit and adds an extra method call.
You do of course realize that almost every non-trivial class/method that you use in the .Net API does the same thing?
You also realize that in any real application that would not even be able to measure the performance impact which, presumably, is the reason you think this matters? And I suspect that exclude badly written/designed applications (real ones) I doubt you would ever have significant impact. If it isn't significant it doesn't matter.
|
|
|
|
|
It's more a matter of style, discipline, and clarity of thought. All of which do matter if you want to produce a quality product.
|
|
|
|
|
PIEBALDconsult wrote: It's more a matter of style, discipline, and clarity of thought.
For "clarity" are you claiming that the Convert methods are not in fact obvious? Do you have a problem understanding what they are doing when you see one?
For "discipline" if you were writing in il then that would demonstrate even more discipline - right?
And "style" is always a subjective preference.
So just a bunch of feel good words strung together to rationalize a subjective preference.
Again, objectively, you will not be able to measure the performance impact in real code.
So what exactly do you think is the objective benefit that you are achieving?
PIEBALDconsult wrote: All of which do matter if you want to produce a quality product.
If you think that using Convert or not has any measurable much less significant impact on code quality then I am rather certain that I would not want to maintain your code.
|
|
|
|
|
Hello,
Why would someone use this technique instead of letting apps directly query the MySQL server? Are there any security benefits or what?
Thanks,
Adrian
|
|
|
|
|
It's dangerous to directly expose the database over the internet. It's much safer to use a proxy in between the clients and the database, because you can enforce authentication and data access security.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
You don't have to build a socket server... could do something much easier like WCF or a web service. Never expose your DB server to the internet. Do you want script kiddies connecting and doing DROP TABLEs ? Any company with even a remotely real IT department will not allow this anyways. Standard IT security protocol.
|
|
|
|
|
Hi,
I have a problem with adding an external Ms-SQL server in de server explorer (visual studio 2008).
Testing te connection is ok, but when i press de ok button i'll get a messagebox with following text:
Unable to add data connection.
Failed to retrieve data for this request.
An exeption occurred while executing a Transact- SQL statement or batch.
The Select permission was denied on the object ‘filegroups’ , database ‘mssqlsystemresource’, schema ‘sys’.
do i have to let change de hosting-company something?
can someone help me?
|
|
|
|
|
nuyts K wrote: do i have to let change de hosting-company something?
Hosting companies rarely allow direct access to the database, there's usually a firewall. Can you connect using other applications?
Bastard Programmer from Hell
|
|
|
|
|
can someone give me a very very very very very simple example how I will use thermometer in dotnetbar?
http://www.devcomponents.com/kb2/?p=989[^]
since I find their guide to be confusing and just defining methods but not how to use them. And also there are methods such as BulbOffset that cannot be seen directly so please help me
|
|
|
|
|
isang puntos wrote: simple example
When you bought the control, I assume suite, did it not come with examples of how to use the controls? You might get a faster response if you ask the vendor.
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Individuality is fine, as long as we do it together - F. Burns
|
|
|
|
|
their example in that site is too vague.. what I want is how I will control temperature gauges... they only give examples on circular gauges not linear gauges
|
|
|
|
|
Hi,
No, I am not page-scraping...
I have a little netstat-type app which lists the RemoteEndPoint.Address, I am trying create a whois type lookup so that I can resolve the remote IP to an organisation/ISP.
I can connect to a website which does the resolving part using WebClient and pull the page down with DowloadString but I would like to pull 3 pieces of information off the page, namely ISP, Organization & Host Name.
If you run the following code in a Console Application you will see the fields I am talking about.
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
TcpConnectionInformation[] connections = properties.GetActiveTcpConnections();
foreach (TcpConnectionInformation t in connections)
{
WebClient ExternalIP = new WebClient();
string code = ExternalIP.DownloadString("https://ipdb.at/ip/217.156.169.187#ip/");
Console.WriteLine(code);
Console.ReadLine();
}
How could I do this? I have seen many posts about how to get the page but none detailing how to get specific text off the page (in C# anyway)
|
|
|
|
|
You are downloading a web page into the string. The string can be analysed with "Regular Expressions ".
Are you trying to do a Whois query?
|
|
|
|
|
Hi,
I tried RegEx but got caught in trying to isolate, for e.g. "Global Crossing" from the line containing ISP: id="MainContent_ISPHyperLink" href="../isp/Global_Crossing">"Global Crossing" id="MainContent_ISPLabel"
Do you know a way I can do it?
I am doing a whois but there doesn't seem to be anywhere that allows multiple queries. I would ideally like my app to resolve the owner/organisation behind the IP address / Hostname that appears in a netstat.
|
|
|
|
|
I have a simple WPF form that calls an external .VBS file. I am able to call the .VBS file from the WPF project without any issues. I would now like to pass a couple variables set in the WPF project to the .VBS file, but can't get this to work. Below is a simplified version of my C# code, and .VBS file.
Thanks in advance!
WPF/C# contents
Process scriptProc = new Process();
scriptProc.StartInfo.FileName = @"cscript";
string fName, lName;
fName "John "
lName = "Smith"
string qParams = fName + lName;
scriptProc.StartInfo.Arguments = "c:\\PrepapreExcelReports.vbs %" + qParams + "%";
scriptProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
scriptProc.Start();
scriptProc.WaitForExit();
scriptProc.Close();
.VBS contents
dim aTest
aTest = qParams
msgbox("Hello " + aTest)
|
|
|
|
|
bolbag wrote: aTest = qParams
I do not know anything about vbscript but I believe that you cannot do this. I think that you need to do something like
aTest = Wscript.Arguments.named
or something like that.
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Individuality is fine, as long as we do it together - F. Burns
|
|
|
|