473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Show dialog in response to message

WP
Disclaimer: I'm new at C# so please be gentle. :-)
I'm writing a simple card game where two players play against each
other through a middle hand they both connect to. In the client (the
player) I read messages from the middle hand in an asynchronous
fashion. I ran into a problem when I tried to implement the player
repsone to the play card message and I need your help.
When the player gets the play card message I want to display a simple
dialog box displaying the cards he or she has available for play.
However, during runtime I get an InvalidOperationException when I
attempt to show the dialog. The more precise error message is, and
here I translate to english: The action between threads is not valid.
The control collected/gathered from another thread than the one it was
created on. Is it because the control (the dialog) tries to use an
object, created by the "main" form, holding the players cards? How
should I solve this in a proper way?

Thanks for reading and thanks for any replies!

- Eric
Mar 14 '08 #1
4 2481
On Thu, 13 Mar 2008 17:11:23 -0700, WP <mi********@gmail.comwrote:
[...]
However, during runtime I get an InvalidOperationException when I
attempt to show the dialog. The more precise error message is, and
here I translate to english: The action between threads is not valid.
[...]
The short answer: you need to use Control.Invoke() or
Control.BeginInvoke().

Do a Google search on those method names or on the English language
version of the text for the exception you're seeing.

Pete
Mar 14 '08 #2
WP
On 14 Mar, 01:32, "Peter Duniho" <NpOeStPe...@nnowslpianmk.comwrote:
On Thu, 13 Mar 2008 17:11:23 -0700, WP <mindcoo...@gmail.comwrote:
[...]
However, during runtime I get an InvalidOperationException when I
attempt to show the dialog. The more precise error message is, and
here I translate to english: The action between threads is not valid.
[...]

The short answer: you need to use Control.Invoke() or
Control.BeginInvoke().

Do a Google search on those method names or on the English language
version of the text for the exception you're seeing.

Pete
Thanks for the quick reply, Pete. After creating dialog and seeing the
exception when calling ShowDialog(), I checked if InvokeRequired was
true and it was false so I didn't think that was the solution. I will
give it another try and post back. Thanks!

- Eric
Mar 14 '08 #3
On Thu, 13 Mar 2008 17:35:44 -0700, WP <mi********@gmail.comwrote:
Thanks for the quick reply, Pete. After creating dialog and seeing the
exception when calling ShowDialog(), I checked if InvokeRequired was
true and it was false so I didn't think that was the solution. I will
give it another try and post back. Thanks!
How did you check InvokeRequired? Did you use an existing control? Or
did you create a dialog form and then check the property on that form?

Basically, without a concise-but-complete code sample from you, it's
impossible to say exactly what you're doing wrong. But your original
description sounds exactly like the cross-thread exception that's thrown
when you try to access GUI components on the wrong thread, and the
solution for that is calling Invoke() or BeginInvoke().

Pete
Mar 14 '08 #4
WP
On 14 Mar, 01:41, "Peter Duniho" <NpOeStPe...@nnowslpianmk.comwrote:
On Thu, 13 Mar 2008 17:35:44 -0700, WP <mindcoo...@gmail.comwrote:
Thanks for the quick reply, Pete. After creating dialog and seeing the
exception when calling ShowDialog(), I checked if InvokeRequired was
true and it was false so I didn't think that was the solution. I will
give it another try and post back. Thanks!

How did you check InvokeRequired? Did you use an existing control? Or
did you create a dialog form and then check the property on that form?
I checked it on the dialog after creating it.
>
Basically, without a concise-but-complete code sample from you, it's
impossible to say exactly what you're doing wrong. But your original
description sounds exactly like the cross-thread exception that's thrown
when you try to access GUI components on the wrong thread, and the
solution for that is calling Invoke() or BeginInvoke().

Pete
Here's the part of the messager handler that responds to the play card
message. I must be
misunderstanding something about Invoke, because it didn't work.

try
{
PlayCardDialog dlg = new PlayCardDialog(this.my_cards,
placed_card);

Trace.WriteLine(dlg.InvokeRequired.ToString());

foo_delegate foo = new foo_delegate(dlg.ShowDialog);

this.tracebox.WriteLine(dlg.InvokeRequired.ToStrin g()); // Prints
False

//if (dlg.ShowDialog(this) == DialogResult.OK)
if ((DialogResult)dlg.Invoke(foo, this) == DialogResult.OK)
{
this.tracebox.WriteLine("play card");
}
}
catch (Exception e)
{
this.tracebox.WriteLine(e.ToString());
}

This code is from the function that is called asynchronously when a
message arrives.
this.my_cards is created in the main form when the program is
launched. It's that's what's causing problems? I have no problem
modifying in response to other messages.

Thanks!
Mar 14 '08 #5

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

Similar topics

5
by: W.Guerlich | last post by:
I've got a Java servlet that delivers large database resultsets transformed to Excel with the HSSF library. In some cases it takes more than 15 minutes before transformation is done and content can...
7
by: theyas | last post by:
How can I get my code to NOT display two "Open/Save/Cancel/More Info" dialog boxes when using the "Response.WriteFile" method to download a file to IE I've asked about this before and didn't get a...
2
by: Rez | last post by:
Hi Guys, I have a problem with exporting to excel from a modal dialog box. Basically, i have an ASPX page that gathers user criteria, and passes it to a dialogbox (showModalDialog), There is a...
3
by: ad | last post by:
I want user can confirm before delete records ? How can I show a confirm dialog when users press a button?
4
by: Jonny | last post by:
Hello Group How do I open a Save File Dialog from an ASPX page behind a browse button? Any help would be fantastic!! I am using ASP.NET 1.1 using VB.NET as the coding language TIA
1
by: Tomas Martinez | last post by:
Hi, In my web I have a proccess of download reports which on first step generates a PDF, then flushes the buffer (the generated file) and finally deletes this file from server. Ok, the problem...
3
by: =?Utf-8?B?YXNkZg==?= | last post by:
Hello. I am making a web application with c# and am using this code: Response.ContentType = "application/x-excel"; Response.AddHeader("Content-Disposition", "attachment;filename=" +...
4
by: GD | last post by:
Hi, I have the following C# code in a page: Response.ContentType = "APPLICATION/OCTET-STREAM"; Response.AppendHeader("Content-Disposition", "Attachment; Filename=\"C:\\Download.txt\"" FileInfo...
7
by: Peter | last post by:
ASP.NET 2.0 I am trying to open a Word document and Excel document from a dialog web page, what's the best way to do that? I have tried the following: Response.Clear();...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

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.