PC Review


Reply
Thread Tools Rate Thread

Clipboard still throws exception with [STAThread]

 
 
renfes@gmail.com
Guest
Posts: n/a
 
      14th Jul 2006
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!

[STAThread]
public static void Main()
{
Application.Run(new Form1());
}

public Form1()
{
InitializeComponent();
}

//opens FileStream after making sure the user has opened a file
public void StreamCaller(object sender, EventArgs e)
{
if (logName!=null)
{
FileStreamer();
}
else
{
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);
}

else
{
}

//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
lines.
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);
SendKeys.SendWait("^v");
newsStream.Position = 0;
}
else
{
}

}

//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;
fs.Close();
sr.Close();
watch.EnableRaisingEvents = false;
newsStream.Close();
newsReader.Close();
}

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

private void openLogDialog(object sender, EventArgs e)
{
logDialog.ShowDialog();
}

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


}
}

Thanks for all your help!

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
InvokeMember throws an exception Ralfeus Microsoft C# .NET 0 16th May 2007 10:31 PM
C# - [STAThread] Still throws expection with Clipboard Sefner Microsoft C# .NET 1 15th Jul 2006 08:03 AM
Clipboard, no-touch deployment, STAThread error Erik Microsoft Dot NET Framework 4 27th Jan 2004 05:16 PM
ServiceController throws exception Strider Microsoft Dot NET Framework 1 3rd Nov 2003 02:39 PM
STAThread Exception sonu Microsoft Dot NET 0 15th Sep 2003 01:15 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:01 PM.