|
I want to create my own timer but because it's running on another thread than the main-form, the program will throw an InvalidOperationException every time the timer ticks.
The code looks like this:
class MyTimer
{
public delegate void TimerTick();
public event TimerTick TickEvent;
Thread t;
public MyTimer()
{
t = new Thread(new ThreadStart(Run));
}
public void start()
{
t.Start();
}
private void Run()
{
while (true)
{
Thread.Sleep(10);
TickEvent();
}
}
}
I know that there already are a timer in the .NET framework but i want to learn how to make thread-safe calls.
Btw: What's the <code>-tag on this forum good for? It removes all code-indentation ![Hmmm | :|](https://codeproject.global.ssl.fastly.net/script/Forums/Images/smiley_line.gif)
|
|
|
|
|
Erikerikerikerikerassa wrote: want to learn how to make thread-safe calls.
See this[^] article.
Erikerikerikerikerassa wrote: What's the <code>-tag on this forum good for? It removes all code-indentation
<code> is used for inline code. Use <pre></pre> around code blocks.
/ravi
My new year's resolution: 2048 x 1536
Home | Music | Articles | Freeware | Trips
ravib(at)ravib(dot)com
|
|
|
|
|
http://www.codeproject.com/csharp/begininvoke.asp[^]
Thank's, i've read the article and think i understand what it means. But there is one problem, i want the BeginInvoke to be built in in my timer, now i have to put it everywhere the timmer throws an event in the main form, BeginInvoke is only availible in Forms. I want all the thread-checking to be in the timer-class so i don't have to rewrite it every time i use the timer.
Is it a good idea to send a reference of the main form to the timer and then use BeginInvoke on that reference?
Like this:
class MyTimer
{
public delegate void TimerTick();
public event TimerTick tick;
Thread t;
Form main;
public MyTimer(Form f)
{
t = new Thread(new ThreadStart(Run));
main = f;
}
public void start()
{
t.Start();
}
private void Run()
{
while (true)
{
Thread.Sleep(10);
main.BeginInvoke(tick);
}
}
}
|
|
|
|
|
Erikerikerikerikerassa wrote: i want the BeginInvoke to be built in in my timer,
Imho, you should do the opposite - i.e. let the implemented delegate call InvokeRequired() and do the appropriate thing when necessary. The timer should know very little (preferably nothing) about its listeners.
/ravi
My new year's resolution: 2048 x 1536
Home | Music | Articles | Freeware | Trips
ravib(at)ravib(dot)com
|
|
|
|
|
In his case he knows InvokeRequired is true as he is calling from another thread
|
|
|
|
|
Quite right.
But checking InvokeRequired() allows the delegate method to be called from the same thread (if reqd in the future) without the performance penalty of Begin/EndInvoke() .
/ravi
My new year's resolution: 2048 x 1536
Home | Music | Articles | Freeware | Trips
ravib(at)ravib(dot)com
|
|
|
|
|
hello ; I need to code a project that uses a map ; the idea of the project is to find the minimum road between to locations ; given the fact that I have found that path , I need a little help now ; I want to color the path that I have found ; so , what I need to know is how to draw on top of an image ; I have the map (which is actually a jpg) and it is loaded in an picturebox object ; so I need to draw a few lines over that picture ; if you know this , please tell me also how can I clear the picture from those lines (becase I can choose to find the minimum path of other 2 locations) ; thanks
|
|
|
|
|
Take a look at GDI+ tutorials. Drawing lines is very simple and you should find asolution fast.
--------------------------------------------------------
My portfolio & development blog
Q:What does the derived class in C# tell to it's parent?
A:All your base are belong to us!
|
|
|
|
|
Although there are several languages that uses semicolon to separate statements, english is not one of them.
There are basically two ways that you can go:
1. Get the bitmap from the picturebox, draw on that, and put it back.
2. Override the paint event of the picturebox to draw on top of the image whenever the control is redrawn.
The advantage of the first method is that it's somewhat easier, and if you want to save the image, it's ready for saving.
The advantage of the second method is that you are not drawing on the image itself, so you don't have to worry about how to restore the original image.
---
b { font-weight: normal; }
|
|
|
|
|
how to add a vedio file in c# web application
and
how to embed microsot office documents [powerpoint]
thanks for great attention
|
|
|
|
|
I am writing an windows app in C#.
Clicking on a button goes to a shared folder in other machine(ex. \\abc\shared ) and searches a certain file in the folder.
I have used Directory class to get folders for local path, but inputting \\abc\shared path in directory class method invokes an error because it thinks the path is local and can't find it.
ex)
string[] folder = Directory.GetDirectories(searchPath);
string[] strFiles = Directory.GetFiles(folderName);
Do I create session events? and how do I integrate login class and session so that I can access the shared folder in other machine and do searching?
Can any expert help me with this?
-- modified at 4:18 Sunday 30th April, 2006
|
|
|
|
|
System.IO.Directory supports UNC paths - try using "\\\\abc\\shared" .
/ravi
My new year's resolution: 2048 x 1536
Home | Music | Articles | Freeware | Trips
ravib(at)ravib(dot)com
|
|
|
|
|
Hi,
Can any one explain me the difference between Type.Missing and Missing.Value, if there is any?
Where should we use either of them (if there are difference or otherwise)?
Finally,
Please explain what should be used while automating excel in the param to add method of workbook object.
Thanks a lot
|
|
|
|
|
They are equivalent, so it's entirely up to you.
e.g., the following two display the same thing:
MessageBox.Show(System.Reflection.Missing.Value.ToString());
MessageBox.Show(System.Type.Missing.ToString());
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter and VB to C++ converter
Instant J#: VB to J# converter
Clear VB: Cleans up VB.NET code
Clear C#: Cleans up C# code
|
|
|
|
|
Hello Friends
From 2 days ago i'm trying to show icons for treeview nodes programmatically. but i was failed.
I have an ImageList with 2 16*16 icons.
I have a TreeView Control too.
I'm writing codes like below and I can see nodes, But i can't see any icon, I searched so many references in WebPages,but they confused me.
<br />
treeView1.ImageList = imageList1;<br />
for (int i = 0; i < 4; ++i)<br />
{<br />
TreeNode n = new TreeNode("Node with image " + i.ToString());<br />
n.ImageIndex = 0;<br />
n.SelectedImageIndex = 1;<br />
treeView1.Nodes.Add(n);<br />
} <br />
Please help me.
Thank You.
-- modified at 5:12 Sunday 30th April, 2006
|
|
|
|
|
I checked your code .You must fix the line number 4 from
<br />
n.SelectedImageIndex = 4;<br /> to n.SelectedImageIndex = 1;
because your image list has only two images and the treeview can't display image for selected node
DevIntelligence.com - My blog for .Net Developers
|
|
|
|
|
Hello again
Ok, I saw that and that was not the main problem, beacause i just typed wrong imageindex here and in my project it was correct.
Thank you for your advice.
|
|
|
|
|
Perhaps you've forgotten to set the Treeview 's ImageList property?
/ravi
My new year's resolution: 2048 x 1536
Home | Music | Articles | Freeware | Trips
ravib(at)ravib(dot)com
|
|
|
|
|
Does imageList1 actually contain the images? Does setting the property using the Forms Editor work?
Regards
Senthil
_____________________________
My Blog | My Articles | My Flickr | WinMacro
|
|
|
|
|
Hello
Everything is Ok, I have imagelist with icons (*.png,*.ico) and i set imagelist property of TreeView control. I have no problem in wizard mode.
It means when i add and set nodes and icons from properties window there is no problem. Problem accures in programmatically mode.
I think i have problem with pictures size or type, Can yo help me ?
|
|
|
|
|
Hi.
In almost all articles about Remoting, I saw namespaces "using System.Runtime.Remoting.Channels.Tcp" and "Using System.Runtime.Remoting.Channels.Http" and etc.
But when I want to use them, VS says that "A namespace does not directly contain members such as fields or methods".
Why ?
I using VS 2005.
Best wishes
|
|
|
|
|
Your problem is no tied with the question you asked.
You can not put a method inside a namespace like this:
namespace Something
{
public void DoSomething ()
{
}
}
methods can only be contained inside a class or a structure.
--------------------------------------------------------
My portfolio & development blog
Q:What does the derived class in C# tell to it's parent?
A:All your base are belong to us!
|
|
|
|
|
You need to explicitly add a reference to System.Runtime.Remoting.dll. VS 2005 automatically adds reference to System.dll and certain other dlls depending on the project type, that's why you might not have done that before.
Regards
Senthil
_____________________________
My Blog | My Articles | My Flickr | WinMacro
|
|
|
|
|
hi everyone,
i'm developing 2 applications that can communicate to each other. application1 is using a tcp socket to send a message to application2. application2 should receive the message through its webservice. now, my problem is i'm not getting the message in application2. i tried running ethereal to check the packets and i can see that the message is going to the computer where application2 is running but i'm getting a Null reference exception on application2. i tried using stacktrace
but it's only giving me the line where the null reference occured no further information. so, i need advise what's going on. why am i not getting the message from the application? i have been working on this problem for a week now. so, please advice.
thanks.
|
|
|
|
|
Near to impossible to answer from your description. Please post the relevant receiver code and probably also the sending code (and don't forget to mark the line where the exception occurs).
|
|
|
|