|
Please don't cross post.
---
b { font-weight: normal; }
|
|
|
|
|
Hi all,
How can I open a file (e.g pdf) with its default program (e.g acrobat reader) in C#?
Thx
Ed
|
|
|
|
|
Process.Start(filePath);
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
Hello
I wrote a multithreading program in vs2003 that don't work in vs2005.
Now I want to write multithreading code with backgroundworker.
But all samples in the web confusing me and i can't understand them!
Please help me to write a simple program to do this :
1-Button 2-TextBox 3-ListBox
By pressing Button, ListBox should fill as equal as TextBox's number.
Button1_Click()
{
for (int i=0;i<convert.toint16(TextBox1.text);i++)
{
ListBox1.items.add("Item #" + i.tostring());
}
}
But i want to do this with BackgroundWorker, So i'll have ability to create second set of this controls on my form to work parallely.
Just give me a simple code for first section.
Tank You
|
|
|
|
|
IMO background worker is quite useless if you already know how to do multithreading yourself. You did it in 2003 you can do it in 2005.
freshonlineMax wrote: don't work in vs2005.
Define "don't work"? What error do you get, and what code do you use?
About your issue if you want to manipulate a control from another thread simple use Control.Invoke() method. ie. Put the above code in a method, and call it from the other thread using Invoke() .
Regards
|
|
|
|
|
Hi,
accessing controls from another thread can result in weird errors. You could use Invoke but I think in this case that doesn't make sense. I suppose this operation takes a while. To shorten this time make sure that ListBox.Sorted is set to false. Second first create a array with all your elements and then use AddRange. That should improve your performance to an extend where threading is hopefully not required anymore.
Robert
|
|
|
|
|
Dear robert Rohde
Thanks for advice, but filling ListBox is not my problem. I wanted it, just for an example of BackgroundWorker. Please if you have easy example about BackgroundWorker help me about this. I really want to know it's functionality.
Regards
-- modified at 14:03 Saturday 28th October, 2006
|
|
|
|
|
Hi,
as said you should find an example where no GUI manipulation is involved. You could make some heavy operations in the thread. Make a Form with a ListBox, two Buttons, a BackgroundWorker and a Progressbar and add the following code:
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
progressBar1.Value = 0;
progressBar1.ForeColor = Color.Green;
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
CalcPrimes(10000000, 10005000, e);
}
private void CalcPrimes(int start, int end, DoWorkEventArgs e)
{
for (int i = start; i <= end; i++)
{
if (backgroundWorker1.CancellationPending)
return;
bool isPrime = false;
if (i % 2 == 1)
{
isPrime = true;
for (int j = 3; j < i / 2; j++)
{
if (i % j == 0)
{
isPrime = false;
break;
}
}
}
int p = 100 * (i - start) / (end - start);
backgroundWorker1.ReportProgress(p, isPrime ? i : 0);
}
}
private void button2_Click(object sender, EventArgs e)
{
progressBar1.ForeColor = Color.Red;
backgroundWorker1.CancelAsync();
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
int primeNumber = (int)e.UserState;
progressBar1.Value = e.ProgressPercentage;
if (primeNumber > 0)
listBox1.Items.Insert(0, primeNumber);
}
This should be a typical scenario for the BackgroundWorker. Don't forget to bind all eventhandlers.
Robert
|
|
|
|
|
Hi
Dear Robert
I got error for below line in "CalcPrimes()" :
backgroundWorker1.ReportProgress(p, isPrime ? i : 0);
error says that : "This BackgroundWorker states that it doesn't report progress. Modify WorkerReportsProgress to state that it does report progress."
Anyway, So thanks to spending your time for solving problems.
Regards
|
|
|
|
|
Hi,
there are two properties on the BackgroundWorker you need to set to true. I think they are call SupportsCancelation and ReportsProgress or something similar.
Robert
|
|
|
|
|
Hello professor Robert
Program was solved successfully.
I found my problem and as you said i should set backgroundworker properties to "True".
Thank you;)
|
|
|
|
|
Hello,
In my Win Form, I have We browser control and sometimes I am getting error like following
:
" Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore
function. "
Can you please give me an idea why I am getting this error ?
Thanks
Emran
|
|
|
|
|
Hello
In what line of code do you get this error? And you said "sometimes"? What about "othertimes"? What do you do exactly to get the error -ie. under what conditions-?
PS.
Take a look at this link[^]. Specially the first hit.
Regards
|
|
|
|
|
Hi Nader,
Thanks for your information. Actually this error is coming magically sometimes.... not all the times... and I have no idea from which part of the code is throwing this error. I thought may be this error is some thing common to DataGridView, now it looks like, My guess is wrong. I will do some more research on it.
Thanks again
emran
|
|
|
|
|
I want to be able to hold the down arrow key to continue to scroll on a custom control that I've written.
How do I check for a key being held down? Is there a property or something to let a keydown event repeat itself?
|
|
|
|
|
implement keyDown event in your control and write your code there.
and where ever you are ebedding your control.create a explict handle for keyDown event for this control and link it with keyDown event which you have created in your control
sameer
|
|
|
|
|
sorry, that's not correct in this case.
|
|
|
|
|
Holding the key down and repeat rate is windows related. For example some users might disable this feature in their windows. Your control has nothing to do with it. All you have to do is handle the KeyDown event and implement scrolling for each stroke. When the user holds the arrow key down your program should receive multiple strokes, not just the first.
Regards
|
|
|
|
|
sorry, that's not correct in this case.
|
|
|
|
|
Why is that? I just made a small test:
1- Make any control that accepts focus
2- Make a ListBox for example
3- Handle the KeyDown event in the first control and in that handler call ListBox.Items.Add() for example and see if it will be repeated or not.
Regards
|
|
|
|
|
I found the snippet to handle an arrow key being repeated in a winform control.
The ProcessCmdKey method must be overriden to trap that key and the repeat message.
Normally the keypress event is the one that allows repeat keypresses by holding the key down. (This rate of repeat is controlled by the bios and the OS)
The keypress event does not trap the arrow keys.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
const int WM_KEYDOWN = 256;
const int WM_CHAR = 258;
const int WM_KEYUP = 257;
const int WM_SYSKEYDOWN = 260;
const int WM_SYSKEYUP = 261;
if (FromHandle(msg.HWnd) == this)
{
switch (msg.Msg)
{
case WM_KEYDOWN:
switch (keyData)
{
case Keys.Down:
int tag = (int)msg.LParam;
bool repeat = (tag & (1 << 30)) != 0;
if (repeat == true)
System.Diagnostics.Debug.WriteLine("********************");
if (this.SelectedIndex + 1 >= this.menuItems.Count)
this.SelectedIndex = 0;
else
this.SelectedIndex++;
|
|
|
|
|
Some quote from the MSDN[^] site:
"The KeyPress event is not raised by noncharacter keys; however, the noncharacter keys do raise the KeyDown and KeyUp events." You should do Keydown's instead of ProcessCmdKey, that's what you are doing anyways...
bradsnobar wrote: switch (msg.Msg)
{
case WM_KEYDOWN:
Just do it on the .NET way.
bradsnobar wrote: switch (keyData)
{
case Keys.Down:
You could write your switch statement on OnKeyDown method:
private void yourcontrol_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
switch(e.KeyCode)
{
case Key.Down:
...
}
...
}
Beside, embedding System const's in your code is not a nice thing to do. Microsoft could just change the values and your code will go to the outerspace.
ps.: Some words from MSDN[^]:
Notes to Inheritors When overriding the ProcessCmdKey method in a derived class, a control should return true to indicate that it has processed the key. For keys that are not processed by the control, the result of calling the base class's ProcessCmdKey method should be returned. Controls will seldom, if ever, need to override this method.
Diego Valdevino
|
|
|
|
|
planning to change out the sql statements i had in one of my application i developed but then my boss was wonderinf if there would be any security issue since persons would now have access to our sql statments by simply jus viewing the store procedures. and i was wondering if there was any way to prevent this
which leads me right back to wondering if i should jus make it be hard coded in the application
so basically my two questions are:
1. is there a way to protect my storeprocedures i thought of leting the server ask for the server account and this works in that the db's password protected but when i click to use windows authentication it allowed me to connect which makes me think that this can be used as a way of getting around me protecting the store procedure say any idea or i am way of on my approach
2. whats the difference in performance between hardcodding sql statems or using store procdures
Kenny Edmond
|
|
|
|
|
The difference in performance is typically large. Use the sql stored procedures instead.
If they can see your stored procs then they can see your data too. So which is more valuable? Probably your data.
This is a seperate problem that can be addressed with a good amount of security.
There are typically three layers of security...
IIS --> SQL --> Windows (File System)
Sometimes the SQL auth is integrated so SQL and file system are the same.
Your choice. But, I choose seperated for higher security, and integrated for ease of use.
|
|
|
|
|
1. From Books on line: "you are creating a stored procedure and you want to make sure that the procedure definition cannot be viewed by other users, you can use the WITH ENCRYPTION clause. The procedure definition is then stored in an unreadable form.
After a stored procedure is encrypted, its definition cannot be decrypted and cannot be viewed by anyone, including the owner of the stored procedure or the system administrator."
This is a rather extreme step to take, however. most hardcoded SQL statements can easily be viewed with a utility that dumps the strings tables in and executable, so IMO stored procs, even unencrypted, are no less secure than hard-coded sql statements.
To view your Stored procs, the user would satill need to be able to log in to the server, to view the strings in your exe, all that is needed is read access to the file...
2. As a general rule, stored procedures are more efficient since the excecution plan is precompiled.
|
|
|
|