By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,054 Members | 902 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,054 IT Pros & Developers. It's quick & easy.

C# - [STAThread] Still throws expection with Clipboard

P: n/a
Hey everyone. I have a program that watches a .txt log file from a chat
client or a game and based on each line, triggers events. One thing I
am trying to do is send the contents of a text file to the clipboard,
then paste it to the program... Here is the code I am using...

namespace Project1
public partial class Form1 : Form

//Variable, fileStreams, FileReaders, Voice thing, and Regexes go here!

public static void Main()
Application.Run(new Form1());

public Form1()

//opens FileStream after making sure the user has opened a file
public void StreamCaller(object sender, EventArgs e)
if (logName!=null)
textBox1.Text = "No Log Selected";

//opens the FileStream, StreamReader, and FileSystemWatcher.
public void FileStreamer()
fs = new FileStream(logName, FileMode.Open,
FileAccess.Read, FileShare.ReadWrite);
sr = new StreamReader(fs);
watch = new FileSystemWatcher(logDir, "*.txt");

if (newsDialog.FileName.Length 2)
// if it exists
newsFile = newsDialog.FileName;
newsStream = new FileStream(newsFile, FileMode.Open,
FileAccess.Read, FileShare.ReadWrite);
newsReader = new StreamReader(newsStream);


//FileSystemWatcher stuff...
watch.IncludeSubdirectories = true;
watch.EnableRaisingEvents = true;
watch.NotifyFilter = NotifyFilters.Size;
watch.Changed += new FileSystemEventHandler(OutputIt);

//Sets position of Stream to the end. This is so it only reads new
fs.Position = fs.Length;

textBox1.Text = "Watching...";
button2.Enabled = true;

//This method is called when the log file is modified. It uses Regexes
and the Contains() //method to see if the new line in the file matches
anything the should trigger something.
public void OutputIt(object source, FileSystemEventArgs e)
//Reads the new line.
string compare = sr.ReadToEnd();

//if statements for different things go here...

//This is the line that gives me trouble. the Clipboard.SetText() line
more specifically.
//It throws the "Current thread must be set to single thread apartment
(STA) mode before //OLE calls can be made. Ensure that your Main
function has STAThreadAttribute marked on //it." Exception. But notice
I do have the attribute on Main()

//if the line contains the string "/news" - which is the command to
bring up the news - the
//text file is read to the end, copied to clipboard, then pasted with
SendKeys. The Stream
//then goes back to the beginning of the text file and waits for the
next command.

else if (compare.Contains("/news"))
newsOutput = newsReader.ReadToEnd();
Clipboard.SetText(newsOutput, TextDataFormat.Text);
newsStream.Position = 0;


//Closes all streams when a button is clicked.
public void EndStream(object sender, EventArgs e)
watch.EnableRaisingEvents = false;
textBox1.Text = "Not Watching";
button2.Enabled = false;
watch.EnableRaisingEvents = false;

//opens file dialogs so user can chose files.
private void openNewsDialog(object sender, EventArgs e)

private void openLogDialog(object sender, EventArgs e)

//sets the files the user chose to strings so the Streams can open
private void logSetter(object sender, CancelEventArgs e)
logName = logDialog.FileName;
fi = new FileInfo(logName);
logDir = fi.DirectoryName;



Thanks for all your help!

Jul 14 '06 #1
Share this Question
Share on Google+
1 Reply

P: n/a

The FileSystemWatcher events run on a thread from the thread pool
unless you set its SynchronizingObject property to a window on your UI
thread. Only the main thread is affected by the STAThread attribute.

Mattias Sjögren [C# MVP] mattias @ |
Please reply only to the newsgroup.
Jul 15 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.