473,781 Members | 2,683 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Clipboard still throws exception with [STAThread]

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()
{
InitializeCompo nent();
}

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

//opens the FileStream, StreamReader, and FileSystemWatch er.
public void FileStreamer()
{
fs = new FileStream(logN ame, FileMode.Open,
FileAccess.Read , FileShare.ReadW rite);
sr = new StreamReader(fs );
watch = new FileSystemWatch er(logDir, "*.txt");

if (newsDialog.Fil eName.Length 2)
// if it exists
{
newsFile = newsDialog.File Name;
newsStream = new FileStream(news File, FileMode.Open,
FileAccess.Read , FileShare.ReadW rite);
newsReader = new StreamReader(ne wsStream);
}

else
{
}

//FileSystemWatch er stuff...
watch.IncludeSu bdirectories = true;
watch.EnableRai singEvents = true;
watch.NotifyFil ter = NotifyFilters.S ize;
watch.Changed += new FileSystemEvent Handler(OutputI t);

//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, FileSystemEvent Args 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.SetTe xt() 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 STAThreadAttrib ute 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.Contai ns("/news"))
{
newsOutput = newsReader.Read ToEnd();
Clipboard.SetTe xt(newsOutput, TextDataFormat. Text);
SendKeys.SendWa it("^v");
newsStream.Posi tion = 0;
}
else
{
}

}

//Closes all streams when a button is clicked.
public void EndStream(objec t sender, EventArgs e)
{
watch.EnableRai singEvents = false;
textBox1.Text = "Not Watching";
button2.Enabled = false;
fs.Close();
sr.Close();
watch.EnableRai singEvents = false;
newsStream.Clos e();
newsReader.Clos e();
}

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

private void openLogDialog(o bject sender, EventArgs e)
{
logDialog.ShowD ialog();
}

//sets the files the user chose to strings so the Streams can open
them.
private void logSetter(objec t sender, CancelEventArgs e)
{
logName = logDialog.FileN ame;
fi = new FileInfo(logNam e);
logDir = fi.DirectoryNam e;
}
}
}

Thanks for all your help!

Jul 14 '06 #1
0 2978

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
4978
by: L | last post by:
Hi guys, I am writing some interop code to print graphics to an eps file and I use adobe printer driver. The piece of code I have written is working fine. but if I loop for more than 200 times, it is throwing an error. Can anybody tell me why it throws an error or am I having memory problems? I debuged through it and I see that createDC is returning 0 on 203rd loop and throws an exception. Why is createDC returning a 0?
1
1060
by: Joshi | last post by:
I want to know how to use the clipboard class. The class take dataobjects. Does that mean that U cannot add controls to the clipboard. I am designing a Form Designer and I want to implement cut,copy and paste for that I need to use the clipboard. But it throws me exception when I try to add any control to clipboard through setdataobject. Please help me with this Thanks for ur help
9
5954
by: Tim Frawley | last post by:
I have converted a VB6 application to VB.NET. The old application made extensive use of the Clipboard for copying an Image Name so that it could be pasted into the image capture app when the user scans the image. My applications run as compiled assemblies from our Intranet server and I have setup a Main sub routine in the application with the following line: <STAThread()> Public Sub Main() Note: There is no timer in this application.
1
4549
by: Sefner | last post by:
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 {
15
12305
by: Peter Duniho | last post by:
I'm trying to use .NET and C# to draw a metafile copied to the clipboard by another application (Word 2003 in this case, but it shouldn't matter). I naively thought that I'd be able to use the Clipboard class to get an EnhancedMetafile or MetafilePict object from the Clipboard, use that to create a new Metafile object, and then draw that Metafile object using Graphics.DrawImage. It doesn't seem to be that simple.
1
18671
by: Figmo | last post by:
Wow.....this is darned odd.... I have an app that integrates with other applications. It registers a global hotkey with Windows. When the hotkey executes it sends a CTRL-C to the active window (any application) to copy whatever text is selected to the clipboard. It then issues a C# Clipboard.GetText(...) and does some processing with this string. All is good, no errors thus far.
8
2505
by: active | last post by:
Guess I'm looking for someone who likes to work difficult puzzles. I can't seem to ever retrieve a palette handle from the clipboard. Below is a simple test program that demonstrates the problem. The program uses windows api to get a handle to a palette from the clipboard. The palette needs to be put there by another program - maybe Photoshop.
5
4186
by: Sin Jeong-hun | last post by:
class Engine { Thread Worker; public event ... EngineMessage; public void Start() { Worker=new Thread(new ThreadStart(Run)); Worker.Start(); } private Run()
5
9784
by: Max2006 | last post by:
Hi, No matter what I have in clipboard Clipboard.GetDataObject(); returns null. I suspect it might be a problem with Vista or VS2008 IDE debug mode problem? Is there any way to track why the following statement is always null? IDataObject iData = Clipboard.GetDataObject(); Thank you,
0
9474
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10308
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10076
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9939
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7486
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6729
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5375
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4040
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.