PDA

View Full Version : C#: Make program run even though it's minimized


Pytagoras
26th February 2009, 09:21 PM
I'm programming an application (WM 6.1) that should do things regardless if the program is active or minimized (user pressed the X button). This is where my problem occurs, my program is set on "pause" when the program is minimized. I need it to run even though it's in the background. How to solve this?

rogeriodeluca
27th February 2009, 02:20 PM
Hi Pytagoras,

I havenīt made a lot of C# programs, but I think that only the main thread is pause when the program is minimized, but the additional threads donīt. So if you need something running on background you need to implement it on an additional thread...

See you

I'm programming an application (WM 6.1) that should do things regardless if the program is active or minimized (user pressed the X button). This is where my problem occurs, my program is set on "pause" when the program is minimized. I need it to run even though it's in the background. How to solve this?

Pytagoras
27th February 2009, 05:08 PM
Thanks for your reply

I'm new to threads, haven't really needed them before. So I found some examples:

class ThreadTest {
private void frmProgram_Load(object sender, EventArgs e)
{
Thread t = new Thread (WriteY);
t.Start(); // Run WriteY on the new thread
}

static void WriteY() {
while (true) Console.Write ("y"); // Write 'y' forever
}
}

Of course I loaded my own method instead of the WriteY example method described here. The thread get started in the Load event of the main form, so it should execute. However, it doesn't work when the program is minimized, and with this thread way of doing it, it doesn't work when it's opened either. (the program should notice a sms ticking in, and do something based on it).

Any help would be greatly appreciated (there should be a dedicated forum on xda for this kind of things / different programming languages..just a comment)

rogeriodeluca
27th February 2009, 05:16 PM
Try something like this... should work...

class ThreadTest {
private Thread t;

private void frmProgram_Load(object sender, EventArgs e)
{
t = new Thread(new ThreadStart(WriteY));
t.IsBackGround = true;
t.Start();
}

static void WriteY() {
while (true) Console.Write ("y"); // Write 'y' forever
}
}

Pytagoras
27th February 2009, 05:35 PM
Strange, it still don't work. I even tried with the code you posted without changing any of it, and still it don't run. It won't even write to the console :confused:

rogeriodeluca
27th February 2009, 05:54 PM
The issue is that the console.write is not showing on "Output Window"... see the code below... that it keeping update the file even if minimized...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace ThreadTest
{
public partial class Form1 : Form
{
private Thread t;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
t = new Thread(new ThreadStart(WriteY));
t.IsBackground = true;
t.Start();
}

private void WriteY()
{
while (true)
{
FileStream fs = new FileStream("\\test.txt", FileMode.Create);

StreamWriter w = new StreamWriter(fs, Encoding.UTF8);

w.WriteLine(DateTime.Now.ToString());
w.Flush();

w.Close();
fs.Close();
}
}
}
}

Pytagoras
27th February 2009, 06:41 PM
Thank you for your help, but I get this when I run your code:

System.IO.IOException:
"The process can not access the file '\\test.txt' because it is being used by another process."

rogeriodeluca
27th February 2009, 06:46 PM
The possibility for that is that you are running the example more than once...

Thank you for your help, but I get this when I run your code:

System.IO.IOException:
"The process can not access the file '\\test.txt' because it is being used by another process."

Pytagoras
27th February 2009, 07:11 PM
Ok, I don't know that went wrong, but I've created a new project, and here it worked.

However, it doesn't work with the sms detection I try to implement.
I'll post the code hoping anyone have some hints:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using Microsoft.WindowsMobile.PocketOutlook;
using Microsoft.WindowsMobile.PocketOutlook.MessageInter ception;


namespace BackgroundTestApp
{
public partial class Form1 : Form
{
private Thread t;


public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
// Start a new thread hoping that this will detect sms even if it's minimized
t = new Thread(new ThreadStart(startMessageDetection));
t.IsBackground = true;
t.Start();

// This works, but only when the program isn't minimized
//startMessageDetection();
}


/// <summary>
/// Load the message detection (sms)
/// </summary>
private void startMessageDetection()
{
MessageInterceptor messageInterceptor = new MessageInterceptor();
messageInterceptor.InterceptionAction = InterceptionAction.NotifyAndDelete;
messageInterceptor.MessageReceived += new MessageInterceptorEventHandler(messageRecieved);


}

void messageRecieved(object sender, MessageInterceptorEventArgs e)
{
if (e.Message is SmsMessage)
{
MessageBox.Show("Message recieved!");
}
}
}
}

rogeriodeluca
27th February 2009, 07:14 PM
Hi Pytagoras,

Sorry, with this part I canīt help you... never coded using this class...

See you,

Ok, I don't know that went wrong, but I've created a new project, and here it worked.

However, it doesn't work with the sms detection I try to implement.
I'll post the code hoping anyone have some hints:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using Microsoft.WindowsMobile.PocketOutlook;
using Microsoft.WindowsMobile.PocketOutlook.MessageInter ception;


namespace BackgroundTestApp
{
public partial class Form1 : Form
{
private Thread t;


public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
// Start a new thread hoping that this will detect sms even if it's minimized
t = new Thread(new ThreadStart(startMessageDetection));
t.IsBackground = true;
t.Start();

// This works, but only when the program isn't minimized
//startMessageDetection();
}


/// <summary>
/// Load the message detection (sms)
/// </summary>
private void startMessageDetection()
{
MessageInterceptor messageInterceptor = new MessageInterceptor();
messageInterceptor.InterceptionAction = InterceptionAction.NotifyAndDelete;
messageInterceptor.MessageReceived += new MessageInterceptorEventHandler(messageRecieved);


}

void messageRecieved(object sender, MessageInterceptorEventArgs e)
{
if (e.Message is SmsMessage)
{
MessageBox.Show("Message recieved!");
}
}
}
}

Mole22
2nd March 2009, 08:39 AM
I don't know what's happening, but I don't like the fact that messageInterceptor is local to startMessageDetection(). The thread will not continue forever, since it is not looping... the three operations in startMessageDetection() will be performed and the thread will cease. I would make messageInterceptor a member of the form, assign it in startMessageDetection(), and try it without the thread. Does this work?

Star-Lite
2nd March 2009, 05:25 PM
try making it a process instead of an application. Processes are ment to be running in the background.