Cross-thread operation not valid: accessed from a thread other than the thread it was created on - asp.net

I want to remove checked items from checklistbox (winform control) in class file method which i am calling asynchronously using deletegate. but it showing me this error message:-
Cross-thread operation not valid: Control 'checkedListBox1' accessed from a thread other than the thread it was created on.
i have tried invoke required but again got the same error. Sample code is below:
private void button1_Click(object sender, EventArgs e)
{
// Create an instance of the test class.
Class1 ad = new Class1();
// Create the delegate.
AsyncMethodCaller1 caller = new AsyncMethodCaller1(ad.TestMethod1);
//callback delegate
IAsyncResult result = caller.BeginInvoke(checkedListBox1,
new AsyncCallback(CallbackMethod)," ");
}
In class file code for TestMethod1 is : -
private delegate void dlgInvoke(CheckedListBox c, Int32 str);
private void Invoke(CheckedListBox c, Int32 str)
{
if (c.InvokeRequired)
{
c.Invoke(new dlgInvoke(Invoke), c, str);
c.Items.RemoveAt(str);
}
else
{
c.Text = "";
}
}
// The method to be executed asynchronously.
public string TestMethod1(CheckedListBox chklist)
{
for (int i = 0; i < 10; i++)
{
string chkValue = chklist.CheckedItems[i].ToString();
//do some other database operation based on checked items.
Int32 index = chklist.FindString(chkValue);
Invoke(chklist, index);
}
return "";
}

Are you sure you are not getting the error from this line of code?
string chkValue = chklist.CheckedItems[i].ToString();

Related

C# Console.WriteLine() not working in other thread after setting Console.SetOut()?

I'm working on a Windows Form application and within my Form, I've created a cross-thread safe TextWriter class that I use for Console.SetOut() to display writes to the Console in a RichTextBox, like so:
public class TextboxWriter : TextWriter
{
private RichTextBox box;
public TextboxWriter(RichTextBox b)
{
box = b;
}
public override Encoding Encoding => Encoding.ASCII;
public override void Write(string value)
{
Debug.WriteLine(value);
box.Invoke((MethodInvoker)delegate ()
{
box.Text += "[" + DateTime.Now.ToString("HH:mm:ss") + "] " + value + "\n";
});
}
}
Console.SetOut() is called when the DiscordConsole class is created and loaded, and this is evident when I call Console.WriteLine() in the STAThread, or any other Thread, for example:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
DiscordConsole f = new DiscordConsole();
Thread t = new Thread(Separate);
t.Start();
Application.Run(f);
}
private static void Separate()
{
Thread.Sleep(1000);
Console.Write("Loading...");
// loading functions
Console.Write("Load complete!");
}
Those calls to Console.WriteLine() work fine. However, my loading function also creates multiple threads, yet in those threads, the call to Console.WriteLine() does not produce anything on the TextBox? Here's how I'm creating threads and starting them.
public static void LoadRegistry(bool downloadAgain)
{
List <Thread> threads = new List<Thread>();
string[] files = Directory.GetFiles(DataPath, "*.json");
float groupamountload = files.Length / (float)Groups;
for (int i = 0; i < Groups; i++)
{
int tmp = i;
Thread t = new Thread(() =>
{
Console.WriteLine("loading");
Debug.WriteLine("loading");
// loading code
});
threads.Add(t);
}
Application.ApplicationExit += delegate (object info, EventArgs e)
{
threads.ForEach(t => t.Abort());
};
threads.ForEach(t => t.Start());
threads.ForEach(t => t.Join());
}
To verify that my threads are running and loading my files, I placed the Debug.WriteLine() AND Console.WriteLine() statements in the threads. I see the Debug.WriteLine() text outputted to Visual Studio, but the RichTextBox is not updated with my Console.WriteLine(). Does anyone know what I'm doing wrong here?
SOLVED
The issue was almost unnoticeable, but I discovered why it wasn't working. My TextboxWriter class would only override the Write() function, but not the WriteLine() function. Adding an override to WriteLine(string) made it work!
I'm not sure why for some calls to Console.WriteLine() would work, and others would not but now they all work.

How to pass value from dependency service to shared code

I am trying to get the last call duration on my xamarin.forms app. On android part I am using dependency service.I can get the call duration. How to pass the duration to shared code back?
My Implementation on Android
class Dialer : ICallerDialer
{
public void GetCallLogs()
{
string queryFilter = String.Format("{0}={1}", CallLog.Calls.Type, (int)CallType.Outgoing);
string querySorter = String.Format("{0} desc ", CallLog.Calls.Date);
ICursor queryData1 = Android.App.Application.Context.ContentResolver.Query(CallLog.Calls.ContentUri, null, queryFilter ,null, querySorter);
int number = queryData1.GetColumnIndex(CallLog.Calls.Number);
int duration1 = queryData1.GetColumnIndex(CallLog.Calls.Duration);
if (queryData1.MoveToFirst() == true)
{
String phNumber = queryData1.GetString(number);
String callDuration = queryData1.GetString(duration1);
How to pass this to Shared code back?
}
return;
}
}
My Interface
public interface ICallerDialer
{
void GetCallLogs();
}
Dependency call when button click
async void btnCall_Clicked(object sender, System.EventArgs e)
{
DependencyService.Get<ICallerDialer>().GetCallLogs();
//How to get duration here?
}
Any help is appreciated.
Just change the return type of your method to string type.
class Dialer : ICallerDialer
{
public string GetCallLogs()
{
string queryFilter = String.Format("{0}={1}", CallLog.Calls.Type, (int)CallType.Outgoing);
string querySorter = String.Format("{0} desc ", CallLog.Calls.Date);
ICursor queryData1 = Android.App.Application.Context.ContentResolver.Query(CallLog.Calls.ContentUri, null, queryFilter ,null, querySorter);
int number = queryData1.GetColumnIndex(CallLog.Calls.Number);
int duration1 = queryData1.GetColumnIndex(CallLog.Calls.Duration);
if (queryData1.MoveToFirst() == true)
{
String phNumber = queryData1.GetString(number);
String callDuration = queryData1.GetString(duration1);
return callDuration;
}
return string.Empty;
}
}
Interface
public interface ICallerDialer
{
string GetCallLogs();
}
Dependency call when button click
async void btnCall_Clicked(object sender, System.EventArgs e)
{
var duration = DependencyService.Get<ICallerDialer>().GetCallLogs();
}

Netty: TCP Client Server File transfer: Exception TooLongFrameException:

I am new to netty and I am trying to design a solution as below for transfer of file from Server to Client over TCP:
1. Zero copy based file transfer in case of non-ssl based transfer (Using default region of the file)
2. ChunkedFile transfer in case of SSL based transfer.
The Client - Server file transfer works in this way:
1. The client sends the location of the file to be transfered
2. Based on the location (sent by the client) the server transfers the file to the client
The file content could be anything (String /image /pdf etc) and any size.
Now, I get this TooLongFrameException: at the Server side, though the server is just decoding the path received from the client, for running the code mentioned below (Server/Client).
io.netty.handler.codec.TooLongFrameException: Adjusted frame length exceeds 65536: 215542494061 - discarded
at io.netty.handler.codec.LengthFieldBasedFrameDecoder.fail(LengthFieldBasedFrameDecoder.java:522)
at io.netty.handler.codec.LengthFieldBasedFrameDecoder.failIfNecessary(LengthFieldBasedFrameDecoder.java:500)
Now, My question is:
Am I wrong with the order of Encoders and Decoders and its configuration? If so, what is the correct way to configure it to receive a file from the server?
I went through few related StackOverflow posts SO Q1,SO Q2 , SO Q3, SO Q4. I got to know about the LengthFieldBasedDecoder, but I didn't get to know how to configure its corresponding LengthFieldPrepender at the Server (Encoding side). Is it even required at all?
Please point me into the right direction.
FileClient:
public final class FileClient {
static final boolean SSL = System.getProperty("ssl") != null;
static final int PORT = Integer.parseInt(System.getProperty("port", SSL ? "8992" : "8023"));
static final String HOST = System.getProperty("host", "127.0.0.1");
public static void main(String[] args) throws Exception {
// Configure SSL.
final SslContext sslCtx;
if (SSL) {
SelfSignedCertificate ssc = new SelfSignedCertificate();
sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
} else {
sslCtx = null;
}
// Configure the client
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.option(ChannelOption.SO_KEEPALIVE, true)
.handler(new ChannelInitializer<SocketChannel>() {
#Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
if (sslCtx != null) {
pipeline.addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT));
}
pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(64*1024, 0, 8));
pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));
pipeline.addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null)));
pipeline.addLast(new ObjectEncoder());
pipeline.addLast( new FileClientHandler()); }
});
// Start the server.
ChannelFuture f = b.connect(HOST,PORT).sync();
// Wait until the server socket is closed.
f.channel().closeFuture().sync();
} finally {
// Shut down all event loops to terminate all threads.
group.shutdownGracefully();
}
}
}
FileClientHandler:
public class FileClientHandler extends ChannelInboundHandlerAdapter{
#Override
public void channelActive(ChannelHandlerContext ctx) {
String filePath = "/Users/Home/Documents/Data.pdf";
ctx.writeAndFlush(Unpooled.wrappedBuffer(filePath.getBytes()));
}
#Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("File Client Handler Read method...");
}
#Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
}
}
FileServer:
/**
* Server that accept the path of a file and echo back its content.
*/
public final class FileServer {
static final boolean SSL = System.getProperty("ssl") != null;
static final int PORT = Integer.parseInt(System.getProperty("port", SSL ? "8992" : "8023"));
public static void main(String[] args) throws Exception {
// Configure SSL.
final SslContext sslCtx;
if (SSL) {
SelfSignedCertificate ssc = new SelfSignedCertificate();
sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
} else {
sslCtx = null;
}
// Configure the server.
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_KEEPALIVE, true).handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SocketChannel>() {
#Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
if (sslCtx != null) {
pipeline.addLast(sslCtx.newHandler(ch.alloc()));
}
pipeline.addLast("frameDecoder",new LengthFieldBasedFrameDecoder(64*1024, 0, 8));
pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));
pipeline.addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null)));
pipeline.addLast(new ObjectEncoder());
pipeline.addLast(new ChunkedWriteHandler());
pipeline.addLast(new FileServerHandler());
}
});
// Start the server.
ChannelFuture f = b.bind(PORT).sync();
// Wait until the server socket is closed.
f.channel().closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
FileServerHandler:
public class FileServerHandler extends ChannelInboundHandlerAdapter {
#Override
public void channelRead(ChannelHandlerContext ctx, Object obj) throws Exception {
RandomAccessFile raf = null;
long length = -1;
try {
ByteBuf buff = (ByteBuf)obj;
byte[] bytes = new byte[buff.readableBytes()];
buff.readBytes(bytes);
String msg = new String(bytes);
raf = new RandomAccessFile(msg, "r");
length = raf.length();
} catch (Exception e) {
ctx.writeAndFlush("ERR: " + e.getClass().getSimpleName() + ": " + e.getMessage() + '\n');
return;
} finally {
if (length < 0 && raf != null) {
raf.close();
}
}
if (ctx.pipeline().get(SslHandler.class) == null) {
// SSL not enabled - can use zero-copy file transfer.
ctx.writeAndFlush(new DefaultFileRegion(raf.getChannel(), 0, length));
} else {
// SSL enabled - cannot use zero-copy file transfer.
ctx.writeAndFlush(new ChunkedFile(raf));
}
}
#Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
System.out.println("Exception server.....");
}
}
I referred Netty In Action and code samples from here
There are multiple things wrong with your server/client. First thing the SSL, for the client you don't need to initialize a SslContext for a server instead you would do something like this:
sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
On the server side of things you use a SelfSignedCertificate which in itself isn't wrong but would like to remind you that it should only be used for debugging purposes and not in production. In addition you use the ChannelOption.SO_KEEPALIVE which isn't recommended since the keepalive interval is OS-dependent. Furthermore you added Object En-/Decoder to your pipeline which in your case don't do anything useful so you can remove them.
Also you configured your LengthFieldBasedFrameDecoder wrong due to an incomplete and wrong parameter list. In the netty docs you need the version of the constructor which defines the lengthFieldLength and initialBytesToStrip. Besides the not stripping the length field you also defined the wrong lengthFieldLength which should be the same as your LengthFieldPrepender's lengthFieldLength which is 4 bytes. In conlusion you could use the constructor like this:
new LengthFieldBasedFrameDecoder(64 * 1024, 0, 4, 0, 4)
In both your handler you don't specify a Charset when en-/decoding your String which could lead to problems because if no ´Charset´ is defined the systems default will be used which could vary. You could do something like this:
//to encode the String
string.getBytes(StandardCharsets.UTF_8);
//to decode the String
new String(bytes, StandardCharsets.UTF_8);
Additionally you tried to use the DefaultFileRegion if no SslHandler was added to the pipeline which would have been fine if you didn't added the LengthFieldHandler since they would need a memory copy of the byte[] to send to added the length field. Moreover I would recommend using the ChunkedNioFile instead of the ChunkedFile because it's nonblocking which is always a good thing. You would do this like that:
new ChunkedNioFile(randomAccessFile.getChannel())
One final thing on how to decode a ChunkedFile as it's split in chunks you can simply assamble them tougether with a simple OutputStream. Here's an old file handler of mine:
public class FileTransferHandler extends SimpleChannelInboundHandler<ByteBuf> {
private final Path path;
private final int size;
private final int hash;
private OutputStream outputStream;
private int writtenBytes = 0;
private byte[] buffer = new byte[0];
protected FileTransferHandler(Path path, int size, int hash) {
this.path = path;
this.size = size;
this.hash = hash;
}
#Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf byteBuf) throws Exception {
if(this.outputStream == null) {
Files.createDirectories(this.path.getParent());
if(Files.exists(this.path))
Files.delete(this.path);
this.outputStream = Files.newOutputStream(this.path, StandardOpenOption.CREATE, StandardOpenOption.APPEND);
}
int size = byteBuf.readableBytes();
if(size > this.buffer.length)
this.buffer = new byte[size];
byteBuf.readBytes(this.buffer, 0, size);
this.outputStream.write(this.buffer, 0, size);
this.writtenBytes += size;
if(this.writtenBytes == this.size && MurMur3.hash(this.path) != this.hash) {
System.err.println("Received file has wrong hash");
return;
}
}
#Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
if(this.outputStream != null)
this.outputStream.close();
}
}

BizTalk Custom Pipeline Component doesn't load overridden properties

I have a custom BizTalk 2013 R2 pipeline component that has several design-time properties defined. For some reason, BizTalk will load the design-time property values set in the VS pipeline designer but it ignores run-time values set in the BizTalk Admin Console. My component implements IPersistPropertyBag and I have verified that it is not throwing any exceptions.
While debugging the pipeline (attached to Isolated Host Instance), I noticed that BizTalk is only calling the Load method when the pipeline is instantiated. This only loads the VS designer values and BizTalk is supposed to then call the Load method again before calling Execute. Unfortunately, this is not happening.
[Edit] I did some more debugging and figured out that this only seems to be happening on the send pipeline for a two-way receive port. The receive pipeline loads both the design-time and run-time properties as expected.
Here is a sample of my code:
[ComponentCategory(CategoryTypes.CATID_PipelineComponent)]
[ComponentCategory(CategoryTypes.CATID_Encoder)]
[System.Runtime.InteropServices.Guid(COMPONENT_GUID)]
public class RhapsodyMessageEncoder : BasePipelineComponent, IBaseComponent, IComponentUI,
IPersistPropertyBag, Microsoft.BizTalk.Component.Interop.IComponent
{
...
public void Load(IPropertyBag propertyBag, int errorLog)
{
try
{
this.Enabled = Convert.ToBoolean(this.ReadPropertyBag(propertyBag, "Enabled"));
this.UsernameSSOKey = this.ReadPropertyBag(propertyBag, "UsernameSSOKey") as string;
this.PasswordSsoKey = this.ReadPropertyBag(propertyBag, "PasswordSsoKey") as string;
this.AffiliateAppName = this.ReadPropertyBag(propertyBag, "AffiliateAppName") as string;
}
catch (Exception e) { this.WriteErrorLog(e); }
}
public void Save(IPropertyBag propertyBag, bool clearDirty, bool saveAllProperties)
{
try
{
this.WritePropertyBag(propertyBag, "Enabled", this.Enabled);
this.WritePropertyBag(propertyBag, "UsernameSSOKey", this.UsernameSSOKey);
this.WritePropertyBag(propertyBag, "PasswordSsoKey", this.PasswordSsoKey);
this.WritePropertyBag(propertyBag, "AffiliateAppName", this.AffiliateAppName);
}
catch (Exception e) { this.WriteErrorLog(e); }
}
...
}
Read / Write Property bag helper methods:
protected virtual object ReadPropertyBag(IPropertyBag pb, string propName)
{
PropertyInfo pInfo = this.GetType().GetProperty(propName);
object currentValue = null;
object val = null;
if (pInfo != null)
currentValue = pInfo.GetValue(this, null);
try
{
pb.Read(propName, out val, 0);
}
catch (System.ArgumentException e)
{
System.Diagnostics.Trace.WriteLine(
"Argument Exception encountered: " + e.Message,
this.Name
);
}
catch (System.Exception e)
{
throw new System.ApplicationException("Can't read design time Properties", e);
}
return val ?? currentValue;
}
protected virtual void WritePropertyBag(IPropertyBag pb, string propName, object val)
{
try
{
object obj = val;
pb.Write(propName, ref obj);
}
catch (System.Exception e)
{
throw new System.ApplicationException("Can't write design time properties", e);
}
}

interesting service behaviour in silverlight

I have a Silverlight project which takes some encrypted string thru its Service Reference: DataService (service which is done in an ASP.NET project).
The method from TransactionServices.cs to get the encrypted string is:
public void GetEncryptedString(string original)
{
DataService.DataServiceClient dataSvc = WebServiceHelper.Create();
dataSvc.GetEncryptedStringCompleted += new EventHandler<SpendAnalyzer.DataService.GetEncryptedStringCompletedEventArgs>(dataSvc_GetEncryptedStringCompleted);
dataSvc.GetEncryptedStringAsync(original);
}
On completing, put the result in encodedString var (which is initialized with an empty value):
void dataSvc_GetEncryptedStringCompleted(object sender, SpendAnalyzer.DataService.GetEncryptedStringCompletedEventArgs e)
{
if (e.Error == null)
{
try
{
if (e.Result == null) return;
this.encodedString = e.Result;
}
catch (Exception ex)
{
Logger.Error("TransactionService.cs: dataSvc_GetEncryptedStringCompleted: {0} - {1}",
ex.Message, ex.StackTrace);
MessageBox.Show(ex.ToString());
}
}
}
Now I want to get the encoded string from my MainPage.xaml like:
TransactionService ts = new TransactionService();
ts.GetEncryptedString(url);
Console.WriteLine(ts.encodedString);
I do not uderstand why ts.encodedString is empty. When I do the debug I see that it actually prints out empty and AFTER that it goes to the void dataSvc_GetEncryptedStringCompleted to take the result and fill it.
Can you point me what I've done wrong? Is there a way to wait for the encodedString to be fetched and only after that to continue?
Thanks a lot.
When you call the ts.GetEncryptedString(url); you just started async operation. And therefor the value you are accessing is will be set only in the callback method.
But you access it before the value is modified by the callback.
The solution which I am using will looks similar to folowing:
Redefine the GetEncryptedString method signature.
public void GetEncryptedString(string original, Action callback)
{
DataService.DataServiceClient dataSvc = WebServiceHelper.Create();
dataSvc.GetEncryptedStringCompleted += (o,e) =>
{
dataSvc_GetEncryptedStringCompleted(o,e);
callback();
}
dataSvc.GetEncryptedStringAsync(original);
}
Call it like this:
ts.GetEncryptedString(url, OtherLogicDependantOnResult);
where
OtherLogicDependantOnResult is
void OtherLogicDependantOnResult()
{
//... Code
}

Resources