|
Good thing you asked then.
|
|
|
|
|
Im writing a c# appliction that makes use of threading.timer. According to the msdn i can specify a period The time interval between invocations of callback
,
Does this means the timer will not t\ck until i finish handeling the tick event?
I tried it out and I got two threads in the callback method… how can I solve this?
Im using only one timer so there is no problem of more than one timer .
Sample code will be greate help
thanks
|
|
|
|
|
I beleive that the threading timer runs in its own thread, so you get a tick event every "interval" milliseconds, regardless of what processing you are doing. So if your interval is 20 msec, and it takes you 50 msec to perform the code in your event handler, then you will get more than one tick event processing at a time. I think this explains your comment, "I tried it out and I got two threads in the callback method", but am not certain. If you want the timer event handler to run in the main thread, try using a System.Windows.Forms.Timer, as it calls the handler on the main thread. I'm not sure what System.Timers.Timer does, but you could try to use that as well.
Jeff
|
|
|
|
|
I agree. Use the System.Windows.Forms.Timer.
I've used a couple of options to keep the timer from firing while the previous timer has not completed.
a) Turn off the timer at the beginning of the timer callback then turn it back on as you complete your callback processing (this will affect your interval time, though, so keep that in mind - if your timer is 60 seconds and it takes 2 seconds to process then your effective interval would be 62 seconds in this case)
b) Use a "busy" variable. Set it to true at the start of the callback and false at the end. That way if a subsequent instance of the timer is fired before the previous one is completed then you can simply exit the callback without processing and wait until it fires again.
Darryl Borden
Principal IT Analyst
dborden@eprod.com
|
|
|
|
|
Hi at all.
I made a program which is able to modify images. Now I want it like this:
You choose an image in your explorer, click the right button of your mouse and click "open with" and select my program, which I called PhotoClient.
Ok, so the PhotoClient is starting but the selected image is not in the picturebox1 of my PhotoClient.
So how can I make this come true that I can load an image like this ?
|
|
|
|
|
The file path to the image file will be passed to your program's arguments inside your main method. Inside Program.cs, you probably have a method like this:
static void Main()
{
...
} Change that to
static void Main(string[] arguments)
{
...
} When your app is launched, look at the arguments parameter. It should contain the file path to the image that Windows Explorer passed to you.
|
|
|
|
|
Thanks for your help.
I put the "string[] arguments" in the Main, and now ? What do you mean with "look at the arguments" ?
How can I transfer the file path into my Form1 ?
|
|
|
|
|
Change the default constructor to take a string array, then change the line "new Form1();" to "new Form1(arguments);". Then in your constructor, use a foreach to try and open the files specified.
Jeff
|
|
|
|
|
sorry Jeff, I do not really understand, I am new in C#.
So, this is my Program.CS
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace PhotoClient
{
static class Program
{
///
/// Der Haupteinstiegspunkt für die Anwendung.
///
[STAThread]
static void Main(string[] arguments)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
And here comes the Form1
// Photo Client 20 //
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
namespace PhotoClient
{
public partial class Form1 : Form
{
string[] arguments;
public Form1()
{
InitializeComponent();
}
#region variables
Bitmap original_image, rot_image, carry, image;
bool fill = false;
string extension = "";
float resize = 1;
OpenFileDialog load = new OpenFileDialog();
BrightnessAdjuster ba = new BrightnessAdjuster();
Rotation rt = new Rotation();
Draw dr = new Draw();
Save_percent sav = new Save_percent();
#endregion variables
#region load n initialize
private void load_Click(object sender, EventArgs e)
{
load.Filter = " jpg files (*.jpg)|*.jpg| All files (*.*)|*.*| bmp files (*.bmp)|*.bmp| gif files (*.gif)|*.gif ";
if (load.ShowDialog() == DialogResult.OK)
{
initialize();
}
}
public void initialize()
{
image = new Bitmap(load.FileName);
FileInfo original = new FileInfo(load.FileName);
extension = original.Name;
trackBar1.Maximum = image.Width;
trackBar2.Maximum = image.Width;
...
...
...
So what is a "constructor" ?
|
|
|
|
|
Apoll
The contructor is run when the a class is instantiated. A constructor has the same name as the class. In your code the constructor is
<br />
public Form1(){<br />
InitializeComponent();<br />
}<br />
What you will want to do is pass in the file name from the arguments list into this constructor. Modify the constructor to accept the path as a string like so:
<br />
public Form1(string ImagePath){<br />
InitializeComponent();<br />
}<br />
Then in the main method pass the path to the new form like so:
<br />
static void Main(string[] arguments)<br />
{<br />
Application.EnableVisualStyles();<br />
Application.SetCompatibleTextRenderingDefault(false);<br />
Application.Run(new Form1(arguments[0]));<br />
}<br />
I have assumed that the path is stored as the first item in the array.
You can then call the initialize method and pass in the path to the file (you will of course have to modify your initialize method to accept the file path as a parameter.
Hope this helps.
|
|
|
|
|
Ok, thank you very much, it is working.
Apoll
|
|
|
|
|
Be careful with that, because it REQUIRES at least one argument, and ignores all others. If you want to be able to run from the command line OR open the application through explorer, you need to either...
1. Send the entire argument array into the constructor, or
2. Put an if, else in your static main method to determine whether to call the initializer with parameters or without
If you only want this to run when you double click a file (or specify a file at the command line) then ignore this post.
Jeff
|
|
|
|
|
oh, yeprs, this is exactly my problem now.
The thing is, when I make it with if else like this:
public partial class Form1 : Form
{
Bitmap original_image;
string path;
public Form1(string[] ImagePath)
{
InitializeComponent();
if (ImagePath[0] != null)
{
path = ImagePath[0];
original_image = new Bitmap(path);
pictureBox1.Image = original_image;
}
}
it says that "if (ImagePath[0] != null)" is outside the bounds of the array
|
|
|
|
|
Just do this...
static class Program
{
[STAThread]
static void Main(string[] arguments)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1(arguments));
}
}
public partial class Form1 : Form
{
Bitmap m_Img null;
public Form1(string[] arguments)
{
InitializeComponent();
if (arguments != null)
{
foreach (string filePath in arguments)
{
if (File.Exists(filePath)) {
try {
m_Img = new Bitmap(filePath);
} catch {
// Do something if error reading file as bitmap
}
}
}
}
}
}
Jeff
|
|
|
|
|
Hey Jeff, you are great, now it is just as i wanted. Thank you very much for your help and your time.
Best regards
Alexander
|
|
|
|
|
Dear all,
I have a file which is on server.
Now I want to read this file's content for my application but I got the problem with providing username and password for accessing this file.
I can not find the above info in FileStream class.
Please tell me how I can do that.
Thanks in advance!
|
|
|
|
|
If your server is an HTTP or FTP server, you won't be able to do it with a file stream.
Instead, look at the WebRequest class. It has a Credentials property to provide username and password. It has a GetResponseStream which returns a Stream from which you can read the bytes, just like FileStream.
|
|
|
|
|
WebRequest ?
Can I do it with Window Form. My application is wind form.
Could you give me the explanation.
Thanks so much
|
|
|
|
|
If my computer connect to the server by Window Explorer, my application can read file normally.
But If I move application to another computer or restart my computer => Error.
I know that I must specify username and password to access to that computer for reading file but I do not know how to provide username and password for the operation.
Please help me to do that.
Thanks a lot.
|
|
|
|
|
I have done with reading file by using LogOnUser API function.
Thanks for your support.
|
|
|
|
|
Oh, you didn't mention the file is on a network share. Had you mentioned that, I could have pointed you in the right direction.
|
|
|
|
|
I have a custom control I'm building that has a custom drawing. However, I'm allowing the control to be moved around and I want to prevent the control from drawing itself because appareantly, when it does, it's drawing the transparent background and it's looking rather odd. I know in the old WinAPI 32 you could simple validate the invalid region and be done with it. Is there anything similiar to that in .NET?
|
|
|
|
|
I don't know about doing it the way you suggested, but you can override the OnPaint method and simply not call base.OnPaint when you want it to not do so. Does this help?
Jeff
|
|
|
|
|
Hi,
it's my first post here and I hope that someone can help me.
I'm developing an application in C#, using VisualStudio .NET 2005, which calls same functions from an ANSI C library.
My problems involve out of memory exceptions :S, because i don't really understand how to use the methods DestroyStructure() and FreeCoTaskMem().
I had 3 different kind of C# classes:
<br />
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]<br />
public class myClassA<br />
{<br />
public Int32 n_elem;<br />
<br />
public IntPtr temperature = IntPtr.Zero;<br />
<br />
public IntPtr length = IntPtr.Zero;<br />
}<br />
<br />
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]<br />
public class myClassB<br />
{<br />
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]<br />
public char[] identifier = new char[8];<br />
<br />
public Double weight = 0F;<br />
<br />
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]<br />
public Double[] coeffs = new Double[3];<br />
}<br />
<br />
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]<br />
public myClassC<br />
{<br />
public IntPtr ptrClassA;<br />
<br />
public IntPtr ptrClassB;<br />
}<br />
First of all I create an object of myClassA and one of myClassB; then I correctly fill each members with appropriate data, then using Marshal.StructureToPtr() let ptrClassA point the myClassA object just created and the same for ptrClassB.
The line before calling the C dll I let a new IntPtr to reference the myClassC object, and then call the library
<br />
myClassA classAObject = new myClassA();<br />
myClassB classBObject = new myClassB();<br />
myClassC classCObject = new myClassC();<br />
<br />
classAObject.n_elem = rowsCount;
Double[] temp = new Double[rowsCount];<br />
Double[] len = new Double[rowsCount];<br />
{...}<br />
<br />
classAObject.temperature = ConvertEx.ArrayToPtr(temp) ;<br />
classAObject.length = ConvertEx.ArrayToPtr(len); <br />
<br />
classCObject.ptrClassA = ConvertEx.StructToPtr(classAObject );<br />
classCObject.ptrClassB = ConvertEx.StructToPtr(classBObject );<br />
<br />
IntPtr ptrIn = ConvertEx.StructToPtr(classCObject);<br />
<br />
CLibrary(ptrIn);<br />
<br />
StructToPtr and ArrayToPtr are static methods of a class ConvertEx which boxes the basic step to allocate unmanaged memory
<br />
public static IntPtr StructToPtr(Object oInstance)<br />
{<br />
IntPtr ret = IntPtr.Zero;<br />
Int32 size = Marshal.SizeOf(oInstance.GetType());<br />
ret = Marshal.AllocCoTaskMem(size);<br />
Marshal.StructureToPtr(oInstance, ret, true);<br />
return ret;<br />
}<br />
<br />
public static IntPtr ArrayToPtr(Int32[] oInstance)<br />
{<br />
Int32 elems = oInstance.GetLength(0);<br />
IntPtr ret = IntPtr.Zero;<br />
Int32 size = Marshal.SizeOf(oInstance[0].GetType())*elems;<br />
ret = Marshal.AllocCoTaskMem(size);<br />
Marshal.Copy(oInstance, 0, ret, elems);<br />
return ret;<br />
}<br />
<br />
Then i just call FreeCoTaskMem() for each IntPtr variable, but in fact memory is not correctly released, I think but i cannot find the right way i had to use DestroyStructure() and FreeCoTaskMem() .
Can anyone explain me the difference between DestroyStructure and FreeCoTaskMem or how to use in my application because in some MSDN examples it uses both methods: first DesroyStructure is called and then FreeCoTaskMem.
thanks in advance
cicciollo
|
|
|
|
|
how to access remote information but they dontknow. who accessing our information
plz send me the code in c# using windows application
|
|
|
|