|
Hi,
I need to do a button which has same functionality as PTT (Press to Talk).
So, you press and keep the button pressed - it does some function.
On release of the button, do something else.
while (button.pressed)
do something
on release
do something
ps. I found this code in a Forum, but it is not working. Nothing happens on pressing the button.
///////////////////////////////////
isLooping = false;
//on mouse down
private void myControl_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) {
isLooping = true;
runLoop();
}
//on mouse up event
private void myControl_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) {
isLooping = false;
}
//This is the main loop you care about. Put this in your application
//This should go in its own thread
void runLoop() {
while (isLooping) {
//do stuff
}
}
|
|
|
|
|
asterix5 wrote: Nothing happens on pressing the button.
Because there is no code inside runLoop() method. Put your code inside the while loop.
|
|
|
|
|
Inside the loop, of course is something that I run. I use it to send Skype commands to turn off/on the microphone.
As I was looking, I tried to simplify it, so just dealing simply with the MouseDown and MouseUp events turning OFF/ON the mic.
Here below is the code, but still is not working - what should I execute on the Button Click itself?
_____________________________________________________________
private void button2_Click_1(object sender, EventArgs e)
{
}
public void OnMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
Skype.Attach(6, true);
SKYPE4COMLib.Command command1 = new SKYPE4COMLib.Command();
command1.Command = "SET MUTE OFF";
command1.Blocking = false;
command1.Id = 1000;
Skype.SendCommand(command1);
}
//on mouse up event
private void OnMouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
Skype.Attach(6, true);
SKYPE4COMLib.Command command1 = new SKYPE4COMLib.Command();
command1.Command = "SET MUTE ON";
command1.Blocking = false;
command1.Id = 1000;
Skype.SendCommand(command1);
}
|
|
|
|
|
If I were you, I would log the events and actually see if they're executed or not.
Alternatively, the solution proposed by Wayne Gaylard is excellent. You may want to try that.
|
|
|
|
|
I would use a BackGroundWorker to handle the work while the button is pressed. I created a test app that adds the letter a to a textbox every second while the button is pressed, and stops when button is lifted. Here is the code for the form (assumes the form contains a button(button1) and a textbox(textBox1, with multiline set to true)). I am sure you can convert this to do what you require:
bool isPressed = false;
BackgroundWorker bgw = new BackgroundWorker();
public Form1()
{
InitializeComponent();
button1.MouseDown += new MouseEventHandler(button1_MouseDown);
button1.MouseUp += new MouseEventHandler(button1_MouseUp);
bgw.DoWork+=new DoWorkEventHandler(bgw_DoWork);
bgw.WorkerSupportsCancellation = true;
}
void bgw_DoWork(object sender, DoWorkEventArgs e)
{
while (isPressed)
{
DoWork();
Thread.Sleep(1000);
}
}
void button1_MouseUp(object sender, MouseEventArgs e)
{
bgw.CancelAsync();
isPressed = false;
}
void button1_MouseDown(object sender, MouseEventArgs e)
{
isPressed = true;
bgw.RunWorkerAsync();
}
delegate void doWork();
void DoWork()
{
if (this.InvokeRequired)
{
doWork d = new doWork(DoWork);
this.Invoke(d);
}
else
{
this.textBox1.Text += "a";
}
}
Hope this helps
Live for today. Plan for tomorrow. Party tonight!
|
|
|
|
|
EDIT: I have it solved, have found a similar library as that mine and there it is implemented in Java, so I just looked for a way to do it. It is needed to use tile-overlapping, so I modified my code and now it's working...
This is that page: http://www.jhlabs.com/[^]
Original message:
Hi all, does anybody know some good source to learn apply a fast convolution filter to image? I am looking for kernels with 20x20 an bigger and need to process image fast, so I need to learn how to do that using FFT or DFT.
I like to use GDI+ in C#.
Any suggestion?
Thanks in idvance...
modified on Tuesday, August 30, 2011 4:23 AM
|
|
|
|
|
Have you seen how does Matlab Function fft work?
Source Code available and may be useful...
Regards,
D
|
|
|
|
|
I have not so far, but I'll give it a try... But still, I would prefer any working example programmed in some .Net language or in native C / C++.
Thanks...
|
|
|
|
|
I'm not sure here, but isn't it more typical to either convolve with a kernel in 'normal' (image) space?
A convolution is rather easy to program, so I'm guessing you could readily program a fast implementation yourself.
Just know that C# (and any .NET language for that matter) isn't known for its speed. If you really want the most out of your program, you could try at C++ or another machine-compiled language.
|
|
|
|
|
I am using native C++ to rewrite working stuff, but for testing I use C#. With GDI+ it is nice that BitmapData is same structure in .Net and native GDI+, so it is easy to migrate working code from C# to native C++.
Simple convolution is what I have done already, but it is too slow (even wrote in C++) compared to PhotoShop plugins, so I am searching for FFT transform of tiles...
Thanks...
|
|
|
|
|
|
Well, Andrew Kirillov has in his library FFT filter, but in convolution filters he does not use them and I need exactly that. Also Christion has made good stuff for beginners, but my question is little more about advanced image processing than about beginner tutorial.
|
|
|
|
|
|
Yes, for most part are, but exactly that part, where they are not is in my interest. What I need is to understand how to forward transform image and mask, multiply them together and transform result back to 'normal' image. I have made some algorithms, but on borders of tiles I have some errors, so I am interested in any code to look for inspiration...
modified on Monday, August 29, 2011 3:29 PM
|
|
|
|
|
if your kernels are separable, you can do the operation as a pair of 1-D convolutions (once in X, once in Y). that will change your complexity from N^2 to N.
|
|
|
|
|
Some kernels are not separable, some are separable. I would like to make it using FFT, but do not know how...
|
|
|
|
|
|
This is exactly what I am looking for. I have it implemented, but there is probably any error in my coding, becouse on borders of result image I have processed pixels from opposite sides of tiles.
Do you know about any code that is doing that correctly?
Or is it normal? Must be used overlapping of input tiles?
Thanks
|
|
|
|
|
sorry, i don't know of any code that does this.
but, that document spends a lot of time talking about how to arrange your kernel to handle the wrap-around problem.
|
|
|
|
|
HI all,
I've written an app that tries to have the main thread sleep while one or more (usually 2) threads idle in the background, each one waking up every X number of minutes to do a data transfer.
For some reason the Private bytes of the app are rising in small but constant increments each time each thread wakes up, and I can't see what I can do to make sure there's nothing hanging around in memory so that I can leave the programme unattended.
(I need to leave this program running on a live server for a long period, so proper use of disposal is really important.)
Main()
{
LaunchThreads(syncNodes);
}
static void LaunchThreads(XmlNodeList syncThreads)
{
foreach (XmlNode threadSetting in syncThreads)
{
Sync syncThread = new Sync(threadSettings);
Thread t = new Thread(new ThreadStart(syncThread.Start));
t.Start();
}
}
}
public class Sync
{
private XmlElement settings;
private int period;
public Boolean interrupt
public Sync(XmlElement syncSettings)
{
settings = syncSettings;
interrupt = false;
period = Convert.ToInt16(settings.GetAttribute("period").ToString())*60000;
}
public void Start()
{
while(!interrupt)
{
Execute();
Thread.Sleep(period);
}
}
private void Execute()
{
}
}
modified on Thursday, September 1, 2011 11:05 AM
|
|
|
|
|
c4tchm4tt wrote: For some reason the Private bytes of the app are rising in small but constant increments each time each thread wakes up, and I can't see what I can do to make sure there's nothing hanging around in memory so that I can leave the programme unattended.
Well, .NET does use a non-deterministic garbage collector.
The garbage collector in .NET prefers to do garbage collection rounds as they are needed. This is a good basic read on it: http://msdn.microsoft.com/en-us/library/0xy59wtx.aspx[^]
If you want to test if you are truly leaking, try this: sleep your main thread for a certain amount of time (rather than -1 now), after which the private bytes have gone up quite a bit. Then issue a GC.Collect() and sleep again. See if that works.
|
|
|
|
|
Hm, well tried that and didn't see any change, besides GC doesn't pop up after unmanaged memory which this would be wouldn't it?
|
|
|
|
|
What makes you think the memory is unmanaged?
At any rate, you are sure the GC call didn't help? So you first let the threads wake up a couple of times, building up private bytes, and then you did a GC collect? And that didn't reduce the memory usage? It's quite hard to 'leak' memory in a .NET application.
Are you allocating any unmanaged memory, or calling unmanaged functions and/or keeping handles, etc.?
The worst case for a full managed application is that you are creating a lot of objects and are keeping all the references, meaning the GC can't collect.
Please describe why you believe there seems to be a memory problem, other than private bytes increasing (which is pretty normal). See http://stackoverflow.com/questions/1984186/what-is-private-bytes-virtual-bytes-working-set[^] for instance, the first (highest ranked) answer.
|
|
|
|
|
Unrelated, but... maybe you want a Windows Service?
|
|
|
|
|
Yeh, a windows service is what ti wil become but I wanted to get a hang of threading and memory management before I ran chasing after service bugs!
I looked at the service template (provided on this site) which looks great but a background worker thread which I understand I would use I think eneds more or less demands the same kind of approach to the above code, so I'm predicting I'd still end up with a memory issue, what do you think?
|
|
|
|