Click here to Skip to main content
15,908,909 members
Home / Discussions / C#
   

C#

 
QuestionTrying to Watermark an Image using ffmpeg Pin
harsimranb23-Apr-10 17:05
harsimranb23-Apr-10 17:05 
AnswerRe: Trying to Watermark an Image using ffmpeg Pin
Alan N23-Apr-10 23:42
Alan N23-Apr-10 23:42 
GeneralRe: Trying to Watermark an Image using ffmpeg Pin
jymitra1-Mar-11 6:32
jymitra1-Mar-11 6:32 
Questionfuncions in C# Windows form Pin
joey_go23-Apr-10 15:48
joey_go23-Apr-10 15:48 
AnswerRe: funcions in C# Windows form Pin
AspDotNetDev23-Apr-10 15:57
protectorAspDotNetDev23-Apr-10 15:57 
GeneralRe: funcions in C# Windows form Pin
joey_go23-Apr-10 17:03
joey_go23-Apr-10 17:03 
GeneralRe: funcions in C# Windows form Pin
mprice21423-Apr-10 18:23
mprice21423-Apr-10 18:23 
AnswerRe: funcions in C# Windows form [modified] Pin
DaveyM6923-Apr-10 22:04
professionalDaveyM6923-Apr-10 22:04 
To answer your direct question, in C# all methods (functions in C) have to be within a class or struct.

To look at a couple of other points, Application.DoEvents is rarely the correct solution. Here you are trying to keep your UI active when in reality it's busy in your while loop.

The correct way to achieve this is to start another thread and do the pause on that, when it completes it can use a callback (event in C#) so it can be acted upon. There are a few controls such as System.Windows.Forms.Timer that have the threading taken care of for you invisibly - it has a property called Interval (measured in milliseconds!) and a Tick event.
C#
using System;
using System.Windows.Forms;

public partial class FormMain : Form
{
    private Timer timer;

    public FormMain()
    {
        InitializeComponent();
        timer = new Timer();
        timer.Enabled = true;
        timer.Tick += new EventHandler(timer_Tick);
    }

    void timer_Tick(object sender, EventArgs e)
    {
        // Use DateTime.Now
    }

    public void Pause(int duration)
    {
        timer.Interval = duration;
        timer.Start();
    }
}
Inside your form call Pause(...). If you want the timer to stop after each pause, call timer.Stop(); in the Tick event handler.

Alternatively you could create a thread yourself and implement what you want, something like:
C#
using System;
using System.Threading;
using System.Windows.Forms;

public partial class FormMain : Form
{
    public event EventHandler<PauseEndedEventArgs> PauseEnded;

    public FormMain()
    {
        InitializeComponent();
        PauseEnded += new EventHandler<PauseEndedEventArgs>(FormMain_PauseEnded);
    }

    void FormMain_PauseEnded(object sender, PauseEndedEventArgs e)
    {
        // e.EndDateTime contains the value
    }

    public void BeginPause(int duration)
    {
        Thread thread = new Thread(new ParameterizedThreadStart(DoPause));
        thread.IsBackground = true;
        thread.Start(duration);
    }
    private void DoPause(object obj)
    {
        int duration = (int)obj;
        Thread.Sleep(duration);
        EndPause();
    }
    private void EndPause()
    {
        if (InvokeRequired)
            Invoke(new MethodInvoker(EndPause));
        else
            OnPauseEnded(new PauseEndedEventArgs(DateTime.Now));
    }
    protected virtual void OnPauseEnded(PauseEndedEventArgs e)
    {
        EventHandler<PauseEndedEventArgs> eh = PauseEnded;
        if (eh != null)
            eh(this, e);
    }
}

public class PauseEndedEventArgs : EventArgs
{
    private DateTime endDateTime;

    public PauseEndedEventArgs(DateTime endDateTime)
    {
        this.endDateTime = endDateTime;
    }

    public DateTime EndDateTime
    {
        get { return endDateTime; }
    }
}

Inside your form you can call BeginPause(...) and it will do what you want.
Dave

If this helped, please vote & accept answer!


Binging is like googling, it just feels dirtier. (Pete O'Hanlon)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

modified on Saturday, April 24, 2010 4:14 AM

AnswerRe: funcions in C# Windows form Pin
#realJSOP24-Apr-10 0:50
professional#realJSOP24-Apr-10 0:50 
AnswerRe: funcions in C# Windows form Pin
joey_go24-Apr-10 18:49
joey_go24-Apr-10 18:49 
QuestionNeed help with Icon/Image Resource in C# VS 2008 .Net 3.5 Pin
USAFHokie8023-Apr-10 12:03
USAFHokie8023-Apr-10 12:03 
AnswerRe: Need help with Icon/Image Resource in C# VS 2008 .Net 3.5 Pin
Henry Minute23-Apr-10 13:54
Henry Minute23-Apr-10 13:54 
QuestionAT Commands Pin
Wamuti23-Apr-10 11:59
Wamuti23-Apr-10 11:59 
AnswerRe: AT Commands Pin
joey_go23-Apr-10 15:55
joey_go23-Apr-10 15:55 
QuestionCheckbox on the header of a Listview Pin
Natural_Demon23-Apr-10 11:00
Natural_Demon23-Apr-10 11:00 
AnswerRe: Checkbox on the header of a Listview Pin
Ravi Bhavnani23-Apr-10 15:37
professionalRavi Bhavnani23-Apr-10 15:37 
GeneralRe: Checkbox on the header of a Listview Pin
Natural_Demon23-Apr-10 16:15
Natural_Demon23-Apr-10 16:15 
Questionvb.net to c# Pin
toto_201023-Apr-10 7:35
toto_201023-Apr-10 7:35 
AnswerRe: vb.net to c# Pin
Luc Pattyn23-Apr-10 7:41
sitebuilderLuc Pattyn23-Apr-10 7:41 
AnswerRe: vb.net to c# Pin
#realJSOP23-Apr-10 7:46
professional#realJSOP23-Apr-10 7:46 
GeneralRe: vb.net to c# Pin
AspDotNetDev23-Apr-10 15:14
protectorAspDotNetDev23-Apr-10 15:14 
GeneralRe: vb.net to c# Pin
toto_201023-Apr-10 23:41
toto_201023-Apr-10 23:41 
QuestionBeginner Question: Distributing Data Driven Applications Pin
Kris Traughber23-Apr-10 4:59
Kris Traughber23-Apr-10 4:59 
AnswerRe: Beginner Question: Distributing Data Driven Applications Pin
dan!sh 23-Apr-10 5:06
professional dan!sh 23-Apr-10 5:06 
GeneralRe: Beginner Question: Distributing Data Driven Applications Pin
Kris Traughber23-Apr-10 5:22
Kris Traughber23-Apr-10 5:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.