Avoiding “Cross-thread operation not valid” Exception With Multithreaded Applications

Unfortunately, enabling thread synchronization for controls is a very hard problem to solve–and even the most elegant solutions have considerable drawbacks. I have tried to develop a demo application in C# that avoids this exception. public partial class Form1 : Form { Thread demothread, demothread1; public delegate void SetTextCallback(string s); public Form1() { InitializeComponent(); } …

Read More

FTP fileupload using C Sharp NET

Here is the simple class file using which you can upload a file to the FTP using .NET class library. This class implements an FTP client. class Upload { public static string uploadFile(string FTPAddress, string filePath, string FTPDir, string username, string password) { try { //Create FTP request FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(FTPAddress + “/” + …

Read More

Sending emails using .NET

You can use the below class to send the emails using asp.net form or any windows based .net applications. This implements the SMTP client to send email. public class MyOwnSMTP { public void SendMail(string From, string To, string Sub, string Body) { MailMessage Email = new MailMessage(From, To, Sub, Body); Email.IsBodyHtml = true; SmtpClient smtp …

Read More

Creating mutually exclusive code block C#

Sometimes, we may need to act on some information(object) which is critical for the application and we need to make sure that the information contained by that object is consistent. The way to keep the information in the object consistent is to avoid the access of that object by more than one thread or program. …

Read More

Fetching URL contents into a string using HttpWebRequest

You can use the below class for fetching the contents of any HTTP URL using the HTTP GET method. This makes use the webrequest classes of .NET class library. public static class GetUrl { public static string FetchURL(string url) { const int bufSizeMax = 65536; // max read buffer size conserves memory const int bufSizeMin …

Read More

add/update windows users – windows user management

Sometimes, we may need to add/update the windows user management functionality in our windows application or windows service. In such cases, we can just use the ‘net user’ functionality of the windows operating system. Refer the below command to perform user operations. NET USER [username [password | *] [options]] [/ username {password | *} /ADD …

Read More