|
Hi,
sorry - I got nothing usable. I switched to a neural network for my application.
Hendrik
|
|
|
|
|
Hi Hendrik,
How does your neural network work? If it is possible could you share your source code, or just the dll file? Thank you very much!
Eddrick
|
|
|
|
|
I'm trying to learn async threading and I stumbled on the first piece of code. It works but with weird results. I'd be glad if you could point out what's wrong with it. Here's the code I'm trying:
private static void LongProcess(int waitTime)
{
Trace.WriteLine("LongProcess started");
Thread.Sleep(waitTime);
Trace.WriteLine("LongProcess finished");
}
private delegate void LongProcessDelegate(int waitTime);
private void Start()
{
LongProcessDelegate lp = LongProcess;
IAsyncResult ar = lp.BeginInvoke(2000, null, null);
while(!ar.Completed)
{
Trace.WriteLine("Not finished yet");
Thread.Sleep(100);
}
lp.EndInvoke(ar);
Trace.WriteLine("All completed");
}
The problem is with the results. This is what I expected:
LongProcess started
Not finished yet
..
Not finished yet
LongProcess finished
All completed
But here's the Trace log when I set it to 1000ms:
LongProcess finished
Not finished yet
..
Not finished yet
LongProcess finished
All completed
At 2000ms:
Not finished yet
LongProcess finished
Not finished yet
..
Not finished yet
LongProcess finished
All completed
What's wrong with this? Am I understanding async calls wrong?
|
|
|
|
|
kensai wrote: Am I understanding async calls wrong?
Yes. Obviously you expect the code in the LongProcess to start immediately when you call the BeginInvoke method, but that doesn't happen.
A new thread is started to run the method, and you can't predict exactly when that thread starts to run. Unless you have a multi-core processor, the thread running the LongProcess method will rarely start before you call Tread.Sleep in the main thread.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Alright, thanks for the info.
|
|
|
|
|
Hi,
i am new in c# an i want to write a little program.
My pgm should have a mainforms with buttons an two modules (each modules has 3 masks).
How can make this ?
Each mask as a form or
the mainform as a form and the modules as usercontrols or
the mainform as a from andmodules as panels ??
I learned the basics from books and the samples and exercises were very simple (max 2 masks/forms).
Can somebody give me links to tuts or samples about my problem.
(sorry for my english, im from germany and not so fit )
by jogi
|
|
|
|
|
I really did not understand what you mean by "Module" and "mask".
You said that you are new in C#, I have been written C# for about 5 years and I have never used these words. LOL
|
|
|
|
|
Hi,
i try to explain.
a mask is a window.
My Programm should have a module stockmanagement with 3 Forms/windows.
And a module suppliermanagement with 3 Forms/windows.
|
|
|
|
|
Hi everybody!
I was wondering if anybody could please help me do something. I have a text file, and inside that text file are urls along with other random junk i have written. And what I want to do is create a simple app that searches my text file for each url (http://www.url_example.com/) in the file, put that url into a string and then write that single url to another file. These text files contain over a hundred url's, sometimes more. So basically i want to make something like:
foreach (url in file("c:\\users\\jase\\desktop\\file_example.txt"))
{
streamwriter writeFile = new streamwriter("c:\\users\\jase\\desktop\\file.txt");
writeFile.Write(urlstring);
writeFile.Close
}
I'd appreciate any help i can get from anyone and thanks for takin the time to read this.
regards,
Jason Pezzimenti.
j.t.
|
|
|
|
|
I think using Regex Class[^] would help you in this.
First read the data from file, then use regex to find matches and loop through them.
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
Thanks for your help I had a look at the MSDN link you gave me and here's what I ended up using:
private void ReadFileLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
StreamReader ReadFile = new StreamReader("C:\\Users\\Jase\\Desktop\\File.txt");
textBox1.Text = ReadFile.ReadToEnd();
ReadFile.Close();
}
private void PerformSearchLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Regex rx = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
string text = textBox1.Text;
MatchCollection matches = rx.Matches(text);
Streamwriter jt = new StreamWriter(C:\\Users\\Jase\\Desktop\\NEW_File.txt");
jt.Write("{0} matches found in:\n {1}" + matches.Count + text);
jt.Close();
// Report on each match.
foreach (Match match in matches)
{
GroupCollection groups = match.Groups;
textBox1.Text = ("'{0}' repeated at positions {1} and {2}" + groups["word"].Value + groups[0].Index + groups[1].Index);
}
}
However, it doesn't seem to work. At all. It never finds anything. How would I go about telling Regex to search for any URL, no matter what URL it is inside a text file? This has got me stumped.
Best Regards,
j.t.
j.t.
|
|
|
|
|
The problem is to find a good pattern: how to describe what's url and what isn't. One problem is that when you find an url inside a text, when does it stop? To the next space, punctuation char etc.
I Googled a bit and found several discussions about indentifying url. Have a look: http://www.google.com/search?hl=en&q=regex+find+url&meta=[^]
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
That was also what I was thinking "when does it stop? To the nex..." Thanks for the link I don't know what it is with me and Google, others always seem to find links that I cannot. Haha
j.t.
|
|
|
|
|
Hi
As you know System.Windows.Forms.SendKeys.Send() method has some special codes to specify keys. I want to send Left or Right shift key, but the Send method does not support it. (Send("+"))
How can I send Left/Right shift key in C#?
Any help is appreciated.
|
|
|
|
|
how can i install a converted calendar as an add-on for office 2007?
|
|
|
|
|
Hi,
Is it possible to retrieve image directly from database and displaying it in image control? If so then guide me...
modified on Saturday, November 15, 2008 3:46 AM
|
|
|
|
|
Your question is too large to answer in depth. If your problem is the datatype and the data is stored in a varbinary (or image) column, when you fetch it, you use byte array for the parameter type.
First build the basic communication between client and db. Then add varbinary column to your select statement. If you have specific problems, you should post the code that's causing the problem. This way it would be possible to answer the question.
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
of course my horse
do you want to load it to crystal reports or a form control?
check here for "load image from database", you will get many articles.
i got it from there.
nelsonpaixao@yahoo.com.br
trying to help & get help
|
|
|
|
|
I plan to distribute an application in the near future. One thing I have not been able to find thus far concerns adding code that requires a user to enter a registration key to fully enable an application. I would appreciate any guidance in this regard.
Thank you in advance.
|
|
|
|
|
An idea, I implemented a while ago:
Create a license-file (with the required information), create a hash over the file, sign this hash with a e.g. RSA implementation, include the public key in your app.
To generate a license, the user submits the license information to you, you create the license file and sign it with your private key.
Your app searches for the license file, reads and verifies it, if the signature is ok.
Hendrik
This VB.NET code might help you ...
Option Explicit On
Option Strict On
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography
Imports Microsoft.VisualBasic
Public Class clsLicenseEngine
Public Structure TYPE_LicenseFile
' License Information
Private m_LicenseName As String
Private m_LicenseCompany As String
Private m_LicenseFlags As String
Private m_LicenseNumber As String
Private m_Hash() As Byte
Private m_Signature() As Byte
Public Property LicenseName() As String
Get
Return m_LicenseName
End Get
Set(ByVal value As String)
m_LicenseName = value
End Set
End Property
Public Property LicenseCompany() As String
Get
Return m_LicenseCompany
End Get
Set(ByVal value As String)
m_LicenseCompany = value
End Set
End Property
Public Property LicenseFlags() As String
Get
Return m_LicenseFlags
End Get
Set(ByVal value As String)
m_LicenseFlags = value
End Set
End Property
Public Property LicenseNumber() As String
Get
Return m_LicenseNumber
End Get
Set(ByVal value As String)
m_LicenseNumber = value
End Set
End Property
Public ReadOnly Property Hash() As Byte()
Get
Return m_Hash
End Get
End Property
Public Function LicenseSplitFlags() As String()
Dim retVal() As String
Dim splitter() As Char = {Chr(Asc("~"))}
retVal = m_LicenseFlags.Split(splitter, StringSplitOptions.RemoveEmptyEntries)
Return retVal
End Function
Private Function HashCalculate() As Byte()
Dim SHA1 As New SHA1Managed
Dim utf8 As New UTF8Encoding
Dim retVal() As Byte
Dim LicenseBuffer() As Byte = utf8.GetBytes(m_LicenseName + vbCrLf + m_LicenseCompany + vbCrLf + m_LicenseFlags + vbCrLf + m_LicenseNumber)
retVal = SHA1.ComputeHash(LicenseBuffer)
Return retVal
End Function
Private Sub HashSet()
m_Hash = HashCalculate()
End Sub
Public Function AdminSignLicense(ByVal PrivateKey As TYPE_Key) As Boolean
If PrivateKey.PrivateKeySet = False Then
Return False
End If
HashSet()
Dim m_rsa As New RSACryptoServiceProvider
m_rsa.ImportParameters(PrivateKey.PrivateKey)
Dim Sign As New RSAPKCS1SignatureFormatter(m_rsa)
Sign.SetHashAlgorithm("SHA1")
m_Signature = Sign.CreateSignature(m_Hash)
Return True
End Function
Public Function CheckSignature(ByVal PublicKey As TYPE_Key) As Boolean
Dim m_rsa As New RSACryptoServiceProvider
m_rsa.ImportParameters(PublicKey.PublicKey)
Dim sign As New RSAPKCS1SignatureDeformatter(m_rsa)
sign.SetHashAlgorithm("SHA1")
Return sign.VerifySignature(m_Hash, m_Signature)
End Function
Public Function AdminWriteLicenseFile(ByVal FileName As String) As Boolean
Dim fs As Stream
Dim utf8 As New UTF8Encoding
Dim lines() As String = {m_LicenseName, m_LicenseCompany, m_LicenseFlags, m_LicenseNumber}
Dim buffer() As Byte
ReDim Preserve lines(lines.Length + 1)
lines(lines.Length - 2) = ArrayToHexString(m_Hash)
lines(lines.Length - 1) = ArrayToHexString(m_Signature)
Try
fs = New FileStream(FileName, FileMode.Create)
For Each line As String In lines
buffer = utf8.GetBytes(line + vbLf)
fs.Write(buffer, 0, buffer.Length)
Next
fs.Flush()
fs.Close()
Catch ex As Exception
Return False
End Try
Return True
End Function
Public Function ReadLicenseFile(ByVal FileName As String) As Boolean
Dim fs As Stream
Dim utf8 As New UTF8Encoding
Dim lines() As String
Dim LongLine As String
Dim lf() As Char = {CType(vbLf, Char)}
Try
fs = New FileStream(FileName, FileMode.Open)
Dim buffer(CType(fs.Length - 1, Integer)) As Byte
fs.Read(buffer, 0, CType(fs.Length, Integer))
LongLine = utf8.GetString(buffer)
lines = LongLine.Split(lf, StringSplitOptions.None)
Catch ex As Exception
Return False
End Try
m_LicenseName = lines(0)
m_LicenseCompany = lines(1)
m_LicenseFlags = lines(2)
m_LicenseNumber = lines(3)
m_Hash = HexStringToArray(lines(4))
HashSet()
m_Signature = HexStringToArray(lines(5))
Return True
End Function
Private Function ArrayToHexString(ByVal data() As Byte) As String
Dim retVal As String = ""
Dim i As Integer
For i = 0 To data.Length - 1
If Hex(data(i)).Length = 1 Then
retVal = retVal + "0"
End If
retVal = retVal + Hex(data(i))
Next
Return retVal
End Function
Private Function HexStringToArray(ByVal HexString As String) As Byte()
Dim retVal(CType(((HexString.Length) / 2) - 1, Integer)) As Byte
Dim i As Integer
For i = 1 To HexString.Length Step 2
retVal(CType((i - 1) / 2, Integer)) = CType(Val("&H" + Mid(HexString, i, 2)), Byte)
Next
Return retVal
End Function
End Structure
Public Structure TYPE_Key
Private m_PrivateKey As RSAParameters
Private m_PublicKey As RSAParameters
Private m_PrivateKeySet As Boolean
Public Property PrivateKey() As RSAParameters
Get
Return m_PrivateKey
End Get
Set(ByVal value As RSAParameters)
m_PrivateKey = value
m_PrivateKeySet = True
SetPublicKey()
End Set
End Property
Public Property PublicKey() As RSAParameters
Get
Return m_PublicKey
End Get
Set(ByVal value As RSAParameters)
m_PublicKey = value
m_PrivateKeySet = False
End Set
End Property
Public ReadOnly Property PrivateKeySet() As Boolean
Get
Return m_PrivateKeySet
End Get
End Property
Public Sub Clear()
m_PrivateKey = Nothing
m_PublicKey = Nothing
m_PrivateKeySet = False
End Sub
Private Sub SetPublicKey()
Dim m_rsa As New RSACryptoServiceProvider
m_rsa.ImportParameters(m_PrivateKey)
m_PublicKey = m_rsa.ExportParameters(False)
End Sub
Public Function WritePrivateKey(ByVal FileName As String) As Boolean
If m_PrivateKeySet = False Then
Return False
End If
Dim fs As Stream
Dim m_rsa As New RSACryptoServiceProvider
m_rsa.ImportParameters(m_PrivateKey)
Dim m_privKey() As Byte = m_rsa.ExportCspBlob(True)
Try
fs = New FileStream(FileName, FileMode.Create)
fs.Write(m_privKey, 0, m_privKey.Length)
fs.Flush()
fs.Close()
Catch ex As Exception
Return False
End Try
Return True
End Function
Public Function WritePublicKeyVB(ByVal FileName As String) As Boolean
Dim utf8 As New UTF8Encoding
Dim tmp As String
Dim buffer() As Byte
Dim fs As Stream
Dim m_rsa As New RSACryptoServiceProvider
m_rsa.ImportParameters(m_PublicKey)
Dim m_PubKey() As Byte = m_rsa.ExportCspBlob(False)
Try
fs = New FileStream(FileName, FileMode.Create)
tmp = "Public LicenseEnginePubKey() As Byte = { _" + vbCrLf
buffer = utf8.GetBytes(tmp)
fs.Write(buffer, 0, buffer.Length)
Dim i As Integer
For i = 0 To m_PubKey.Length - 1
tmp = "&H" + Hex(m_PubKey(i))
If i < m_PubKey.Length - 1 Then
tmp = tmp + ", "
If (i Mod 20 = 0) And (i > 0) Then
tmp = tmp + "_" + vbCrLf
End If
Else
tmp = tmp + " _ " + vbCrLf + "}"
End If
buffer = utf8.GetBytes(tmp)
fs.Write(buffer, 0, buffer.Length)
Next
fs.Flush()
fs.Close()
Catch ex As Exception
Return False
End Try
Return True
End Function
Public Function WritePublicKey(ByVal FileName As String) As Boolean
Dim fs As Stream
Dim m_rsa As New RSACryptoServiceProvider
m_rsa.ImportParameters(m_PublicKey)
Dim m_pubKey() As Byte = m_rsa.ExportCspBlob(False)
Try
fs = New FileStream(FileName, FileMode.Create)
fs.Write(m_pubKey, 0, m_pubKey.Length)
fs.Flush()
fs.Close()
Catch ex As Exception
Return False
End Try
Return True
End Function
Public Sub SetKeyBinary(ByVal key() As Byte)
Dim m_rsa As New RSACryptoServiceProvider
m_rsa.ImportCspBlob(key)
Try
PrivateKey = m_rsa.ExportParameters(True)
Catch ex As Exception
PublicKey = m_rsa.ExportParameters(False)
End Try
End Sub
Public Function ReadKey(ByVal FileName As String) As Boolean
Dim fs As Stream
Dim m_rsa As New RSACryptoServiceProvider
Try
fs = New FileStream(FileName, FileMode.Open)
Dim keylenght As Integer = CType(fs.Length, Integer)
Dim key(keylenght - 1) As Byte
fs.Read(key, 0, keylenght)
fs.Close()
Clear()
m_rsa.ImportCspBlob(key)
Catch ex As Exception
Return False
End Try
Try
PrivateKey = m_rsa.ExportParameters(True)
Catch ex As Exception
PublicKey = m_rsa.ExportParameters(False)
End Try
Return True
End Function
Public Sub GenerateNewKey()
Dim m_rsa As New RSACryptoServiceProvider
PrivateKey = m_rsa.ExportParameters(True)
End Sub
End Structure
End Class
|
|
|
|
|
I have written a singleton application as follows:
public static class SingletonApp
{
static Mutex m_Mutex;
public static void Run( Form mainForm )
{
bool first = IsFirstInstance( );
if( first )
{
Application.ApplicationExit += OnExit;
Application.Run( mainForm );
}
else
{
}
}
static bool IsFirstInstance( )
{
Assembly assembly = Assembly.GetEntryAssembly( );
string name = assembly.FullName;
m_Mutex = new Mutex( false , name );
bool owned = false;
owned = m_Mutex.WaitOne( TimeSpan.FromSeconds(0) , false );
return owned;
}
static void OnExit( object sender , EventArgs args )
{
m_Mutex.ReleaseMutex( );
m_Mutex.Close( );
}
}
When this application is running(minimized mode),I hope to open its main window(Form mainForm above in Run method).In order to implement this function(similar to Microsoft Outlook),how should I add other code?
Thank you very much!
|
|
|
|
|
|
You could use for example Process.GetProcessesByName[^] method to find the existing instance and then invoke a method on that instance using reflection. The method could simply set the main forms window state to normal.
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
Mika,Thank you very much!
I have added a line in that else statement above as follows:
Process[] singleInstance = Process.GetProcessesByName( "SingletonApp" );
But in turn ,how shall I write using reflection ?
May you help to write that code followed?
I am looking forward to your reply again!
Thank you very much again!
|
|
|
|
|
I believe that the article Giorgi posted is excellent. Use that and explore the communication. The only thing I would consider in that article is that perhaps IpcChannel could be used instead of TcpChannel.
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|